attachment_fu on Windows?

4 views
Skip to first unread message

Jeff Coleman

unread,
Mar 3, 2007, 6:49:25 AM3/3/07
to rubyonra...@googlegroups.com
Has anyone had any success using Rick Olson's "attachment_fu" plugin
while developing on a Windows machine?

I've been looking for solutions to the issue described here
http://railsweenie.com/forums/3/topics/1257 but haven't heard anything
yet.

The issue is that attachment_fu fails to record the uploaded file's size
value properly, causing its validations to fail.

I’ve been adding logger messages into the attachment_fu code to see
what’s causing my problems on Windows. In my case it’s not properly
assigning the “size” attribute.

In “attachment_fu.rb”, there is the following method:

def set_size_from_temp_path
self.size = File.size(temp_path) if save_attachment?
end

Based on logger messages, I’ve determined that save_attachment? returns
true, the temporary file does exist on my system, but that file’s
File.size parameter returns zero, so it saves self.size as zero, thus
failing the validations.

In my case, the uploaded_data IS properly assigned, and the image is
saved to the disk in the appropriate place. The only problem I’ve found
is with the size parameter. To complicate matters, the behavior is not
consistent. Occasionally it saves a size value, but most of the time the
size is zero.

I’ve created a brand new Rails app and model which uses only
attachment_fu’s required fields to test these conclusions.

On my system I’m using:
- Rails 1.2.2
- Windows XP
- MySQL 5.0
- ImageMagick-6.3.2-Q16
- mini_magick 1.2.0

When attachment_fu creates thumbnails, the thumbnail files ARE saved
with the correct size in their model. I don’t know why the original
files aren’t.

Does this have to do with Windows “Tempfile” issues? I tried out a
“windows_tempfile_fix” plugin but it required Ruby 1.8.4 where I have
1.8.5.

Does anyone have any ideas? Thanks,

Jeff

--
Posted via http://www.ruby-forum.com/.

Eric Northam

unread,
Mar 4, 2007, 7:27:32 AM3/4/07
to Ruby on Rails: Talk
My guess is it's the Ruby version and the temp file issue that is your
problem. I took the following steps and was able to get it to work on
Windows

1. Installed rmagick via the instructions on http://rubyforge.org/projects/rmagick/

2. Installed AttachmentFU via

ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/attachmen_fu/

3. In C:\ruby\lib\ruby\1.8\tempfile.rb

# Modified per http://blog.teksol.info/articles/2006/12/08/rmagick-and-jpeg-datastream-contains-no-image
# for the "JPEG datastream contains no image" bug
# @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|
File::EXCL, 0600)
@tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL|
File::BINARY, 0600)

You might also want to review http://clarkware.com/cgi/blosxom/2007/02/24
to see if you haven't missed anything.

Eric

Jeff Coleman

unread,
Mar 4, 2007, 9:01:54 AM3/4/07
to rubyonra...@googlegroups.com
Eric Northam wrote:
> My guess is it's the Ruby version and the temp file issue that is your
> problem. I took the following steps and was able to get it to work on
> Windows
> ...snip...

Eric, thanks.

I looked over the steps and it does seem that I've done all those
things, I confirmed that I've already made that change to tempfile.rb.

I'm using mini_magick, though--I had been using file_column and Rmagick
and the reason I switched to attachment_fu was to take advantage of the
streamlined mini_magick library.

I'll give it a try with Rmagick and see how it goes.

Jeff Coleman

unread,
Mar 4, 2007, 9:32:23 AM3/4/07
to rubyonra...@googlegroups.com
Jeff Coleman wrote:
> Eric Northam wrote:
>> My guess is it's the Ruby version and the temp file issue that is your
>> problem. I took the following steps and was able to get it to work on
>> Windows
>> ...snip...

I just did a fresh installation with a new Rails app, a new model and a
new install of attachment_fu using Rmagick as the image processor, and I
received the same results. The file's "size" attribute is consistently
defined as zero.

I swapped out attachment_fu for acts_as_attachment and it works
successfully, the file's "size" attribute sets properly. Unfortunately
it looks like acts_as_attachment only works with Rmagick.

I've been exploring through the workings of attachment_fu but I don't
understand it completely yet, so I don't know why the temp file's size
attribute doesn't get set properly.

Anyone have any ideas? Thanks,

Eric Northam

unread,
Mar 4, 2007, 11:51:00 AM3/4/07
to Ruby on Rails: Talk
How about rolling back to 1.8.4 just to verify whether it's the ruby
release?

Jeff Coleman

unread,
Mar 5, 2007, 10:56:53 AM3/5/07
to rubyonra...@googlegroups.com
Eric Northam wrote:
> How about rolling back to 1.8.4 just to verify whether it's the ruby
> release?

Good idea--tried it, installed Ruby 1.8.4 and fresh installs of Rails
and Rmagick, and got the same results.

I'm a little unclear on the usage of the Tempfile class. What would be
a good way to run a simple discrete test case of Tempfile, to see if
it's working properly on my system?

Adam Rails

unread,
Mar 5, 2007, 4:32:44 PM3/5/07
to rubyonra...@googlegroups.com
I also encountered this problem with attachment_fu.  I tested it with the tempfile patch as well.

Quite maddening actually since I don't have a solution for it.

Adam

Jeff Coleman

unread,
Mar 6, 2007, 2:05:24 AM3/6/07
to rubyonra...@googlegroups.com
Adam Rails wrote:
> I also encountered this problem with attachment_fu. I tested it with
> the
> tempfile patch as well.
>
> Quite maddening actually since I don't have a solution for it.
>
> Adam

Glad I'm not the only one! If I could just figure out why the file's
size is returning zero, it seems it would clear up the issue. I'm
trying to work out how to test the Tempfile class itself right now, to
see if that's the root of the problem.

Eric Northam

unread,
Mar 21, 2007, 10:01:21 PM3/21/07
to Ruby on Rails: Talk
Jeff,

If you want, email me your zipped app and I'll check if it works in my
environment.

Eric

Wes

unread,
Mar 24, 2007, 8:57:32 AM3/24/07
to Ruby on Rails: Talk
I'm stuck like everyone else on the "0" filesize. Everything else
seems to work however.

Here is a step-by-step blog post on how my dev environment is setup:

http://blog.72bit.com/2007/03/12/attachment_fu-on-windows/

Wes

Jeff Coleman

unread,
Mar 25, 2007, 4:49:15 AM3/25/07
to rubyonra...@googlegroups.com
Wes wrote:
> I'm stuck like everyone else on the "0" filesize. Everything else
> seems to work however.
>
> Here is a step-by-step blog post on how my dev environment is setup:
>
> http://blog.72bit.com/2007/03/12/attachment_fu-on-windows/
>
> Wes

It's not a perfect solution, but Rails Weenie user wfn found a
workaround:

http://railsweenie.com/forums/3/topics/1257#posts-4895

The problem seems to be that, on Windows, the temporary file's size
doesn't register fast enough when the file is created, so adding a 1 or
2 second delay before processing it seems to work.

Not ideal, but at least it's something.

Eric Northam

unread,
Mar 30, 2007, 6:35:36 PM3/30/07
to Ruby on Rails: Talk
Well I finally was able to reproduce the infamous zero size error. I'm
not sure if it's the same one your talking about but if so I think I
have some more clues as to the cause and a possible workaround.

First, I didn't have any problems before when I save the new image via
the parent object via

house.images << new_image
# Saved via the parent
house.save

I also modified the attachment_fu a bit to accept .bmp files as images
and everything continued to work. When I removed the parent from the
save operation via

new_image.house = house
new_image.save

all the sudden the .bmp files stopped working with the zero size
error. So I started looking through the code and noticed that for some
reason the .bmp files are being sent as Files and not StringIOs. I
haven't looked into why one is a StringIO and the other is a File but
I'm guessing it has something to do with the content type and Mongrel.
Regardless if it's a file it took a different path through the code.

Here's the uploaded_data=(file_data) method where the the path split.
By adding in the read and commenting out setting the path I was able
to fix it on my machine.

def uploaded_data=(file_data)
return nil if file_data.nil? || file_data.size == 0
self.content_type = file_data.content_type
self.filename = file_data.original_filename if respond_to?
(:filename)
if file_data.is_a?(StringIO)
file_data.rewind
self.temp_data = file_data.read
else
self.temp_data = file_data.read
#self.temp_path = file_data.path
end
end

Unfortunately I don't know what repercussions the fix might have.
Could the prior posters with the error verify if this fixes the zero
size error? Remember to restart your server after the change.

Eric


Jeff Coleman

unread,
Mar 30, 2007, 6:48:46 PM3/30/07
to rubyonra...@googlegroups.com
Eric Northam wrote:
>
> Unfortunately I don't know what repercussions the fix might have.
> Could the prior posters with the error verify if this fixes the zero
> size error? Remember to restart your server after the change.

Thanks Eric, I'll test this in my app and let you know what happens.

ebe...@gmail.com

unread,
Mar 31, 2007, 2:45:56 PM3/31/07
to Ruby on Rails: Talk
Hmm....

I had a different, but maybe related, problem with attachment_fu on
XP. Coincidentally, I just sent an email to Rick about five minutes
ago.

To rephrase my email to him>>

I added this line of code at line 161 in attachment_fu.rb to fix a
problem on Windows machines where all incoming images are corrupted by
extraneous line-feed characters. I am developing on windows
currently, so this stung me.

At line 161 of attachment_fu.rb:
tmp.binmode

The binary mode issue on Windows is discussed on this wiki page:
http://wiki.rubyonrails.org/rails/pages/HowtoUploadFiles if you're
interested in more info. This should not harm the app on linux based
on my prior experience with this bug (in my previous photo handling
model). Thanks again,

Eric


On Mar 30, 6:48 pm, Jeff Coleman <rails-mailing-l...@andreas-s.net>
wrote:

Jeff Coleman

unread,
Mar 31, 2007, 2:49:53 PM3/31/07
to rubyonra...@googlegroups.com
Eric Northam wrote:
> Unfortunately I don't know what repercussions the fix might have.
> Could the prior posters with the error verify if this fixes the zero
> size error? Remember to restart your server after the change.

Eric,

Your solution seems to have done the trick! I took out the "sleep" fix
that I'd previously added, entered your change to attachment_fu.rb and
the files upload properly with the correct size now. Thanks!

Adam

unread,
Apr 9, 2007, 7:34:07 PM4/9/07
to rubyonra...@googlegroups.com
To get things working with S3 and windows, I changed:

def temp_data
if save_attachment?
f = File.new( temp_path )
f.binmode
return f.read
else
return nil
end
end
(before, the method body was save_attachment? ? File.read( temp_path ) :
nil )

It's kind of ugly as it assumes attachments are binary, but it works in
my situation.

Nolgthorn

unread,
Jun 6, 2007, 7:15:38 AM6/6/07
to rubyonra...@googlegroups.com
Good gravy Eric Northam's solution works! I had tried everything else,
thank you, it only remains to see how this will affect the application
when it's time to deploy to the server.

Aswin Anand

unread,
Jul 7, 2007, 3:44:50 AM7/7/07
to rubyonra...@googlegroups.com
Adam wrote:
> To get things working with S3 and windows, I changed:
>
> def temp_data
> if save_attachment?
> f = File.new( temp_path )
> f.binmode
> return f.read
> else
> return nil
> end
> end
> (before, the method body was save_attachment? ? File.read( temp_path ) :
> nil )

I'm not having any problems with attachment_fu on windows and all is
well. But when I move over the code to my hosting space at dreamhost, I
get an error saying,

"You have a nil object when you didn't expect it!
The error occurred while evaluating nil.symbolize_keys"

This error happens only when the :storage option of "has_attachment"
method is set to :s3. Any ideas?


--
Aswin

Jonas Wa

unread,
Sep 14, 2007, 10:34:39 AM9/14/07
to rubyonra...@googlegroups.com
I’m a newbie in Ruby and using attachment_fu on Windows Vista. I first
had some issues to have it working properly. The size of the file was
incorrect set to 0, the empty file was created (and a row was added in
my attachment table). However, I notice that this was caused by
incorrect file permissions. I got "Permission denied". When I explicit
increased rights (+r+w+x) on the folder where the file is stored, it
seems to work just fine.

Since I wasted some time there, I hope to save those hours for someone
else :-)

George Sharonov

unread,
Dec 21, 2007, 10:58:58 AM12/21/07
to rubyonra...@googlegroups.com
I've spent two days in trying to attachment_fu and MiniMagic get work on
Windows.

My solution may be a specific solution in my case, but maybe somebody
will find something usefull....

Finaly I've came up with these three steps that were neccecary for me:

1. little hack in attachment_fu to avoid 'size is not included in the
list' message. Note that this works with MiniMagic, but does not work
with RMagic.

2. Install MiniMagic as Plugin (just copying /mini_magick-1.2.0 folder
in my /vendor/plugin folder with no change to environment.rb)

3. Finally I've mentioned that ImageMagic can't do anything with files
with long pathes like "Documents and setting/users/My complicated user
account/Local Settings/.......", but temporary files are stored there!!!
I've changed local TMP and TEMP variables to c:/TEMP and restart the
computer and after that everethyng became fine.

Maybe this will helps for somebody, good luck!

PS: sorry for my English, my wife is waiting for me and I have no time
for spell check...

Emmanuel Pirsch

unread,
Jan 18, 2008, 11:32:48 AM1/18/08
to rubyonra...@googlegroups.com
I've posted a fix on my blog :
http://epirsch.blogspot.com/2008/01/fixing-attachmentfu-on-windows-like.html

I believe that my fix for the "Size is not included in the list" issue
is better than the timing loop or other fixes I've seen on the web.

Also attached is the actual fix that you just need to require to fix the
issue.


Attachments:
http://www.ruby-forum.com/attachment/1324/attachment_fu_patch.rb

Roger Pack

unread,
Nov 10, 2008, 9:50:24 PM11/10/08
to rubyonra...@googlegroups.com
Eric Northam wrote:
> Well I finally was able to reproduce the infamous zero size error. I'm
> not sure if it's the same one your talking about but if so I think I
> have some more clues as to the cause and a possible workaround.
>
How do you recreate this error? I would like to submit a patch to ruby
core :)
-=R
Reply all
Reply to author
Forward
0 new messages