Forum: Ruby on Rails attachment_fu on Windows?

59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2007-03-03 12:49
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
Ddf9c98db9ac40c6875fd2a5b2d4fa72?d=identicon&s=25 Eric Northam (Guest)
on 2007-03-04 13:27
(Received via mailing list)
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/rmagic...
      # 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
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2007-03-04 15:01
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
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2007-03-04 15:32
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,

Jeff
Ddf9c98db9ac40c6875fd2a5b2d4fa72?d=identicon&s=25 Eric Northam (Guest)
on 2007-03-04 17:51
(Received via mailing list)
How about rolling back to 1.8.4 just to verify whether it's the ruby
release?
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2007-03-05 16:56
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?

Jeff
36fae7e206fc480f2cef2736887ae7ef?d=identicon&s=25 Adam Rails (Guest)
on 2007-03-05 22:34
(Received via mailing list)
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
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2007-03-06 08:05
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.

Jeff
Ddf9c98db9ac40c6875fd2a5b2d4fa72?d=identicon&s=25 Eric Northam (Guest)
on 2007-03-22 03:04
(Received via mailing list)
Jeff,

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

Eric
813679171280808f051b5754c9db45a6?d=identicon&s=25 Wes (Guest)
on 2007-03-24 13:58
(Received via mailing list)
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
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2007-03-25 10:49
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.

Jeff
Ddf9c98db9ac40c6875fd2a5b2d4fa72?d=identicon&s=25 Eric Northam (Guest)
on 2007-03-31 00:36
(Received via mailing list)
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
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2007-03-31 00:48
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.

Jeff
0fcceb8cbefc2b99369fe7ba676d4384?d=identicon&s=25 unknown (Guest)
on 2007-03-31 20:47
(Received via mailing list)
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>
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2007-03-31 20:49
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!

Jeff
570c298e58afb154a4e6a24ce17bec71?d=identicon&s=25 Adam (Guest)
on 2007-04-10 01:34
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.

Jeff Coleman wrote:
> 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!
>
> Jeff
110c7df9fbc932cfe55c6317aedd04b3?d=identicon&s=25 Nolgthorn (Guest)
on 2007-06-06 13:15
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.
52dc325df2627a80a5c5a0678f5b3e2e?d=identicon&s=25 Aswin Anand (aswinanand)
on 2007-07-07 09:44
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
5241688de828aae4efd2b89fc3dff00b?d=identicon&s=25 Brandon (Guest)
on 2007-07-27 21:35
(Received via mailing list)
This solution worked for me.  I was using :db_file

What an ordeal.
18f3c72889f89ca665b3aedc1c3d9835?d=identicon&s=25 Jonas Wa (jowa)
on 2007-09-14 16:34
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 :-)
Ff142d292091f205657e5d566321b68c?d=identicon&s=25 George Sharonov (sharonov)
on 2007-12-21 16:58
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...
0a496328d87dbf5d6a890bdb0baa609d?d=identicon&s=25 Emmanuel Pirsch (epirsch)
on 2008-01-18 17:32
Attachment: attachment_fu_patch.rb (1 KB)
I've posted a fix on my blog :
http://epirsch.blogspot.com/2008/01/fixing-attachm...

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.
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2008-11-11 03:50
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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.