1.18.2008

Fixing attachment_fu on Windows

Like many others, I've encountered issue when developing Rails applications using attachment_fu on Windows. After doing some research, I've come up with the following solution to the problem.

The problem has two parts :

Size is not included in the list error message,

Timeout error when uploading to S3.

Fixing "Size is not included in the list" error message

Some people have reported that there is a timing issue, when trying to get the file size, with Tempfile on Windows. It seems that the size of the file is not properly reported by Windows after writing data to it. Proposed solutions for this problem include :

Sleeping in a loop as long as the file size is 0,

Reading back the entire file in memory.

I think I found a better and less patchy solution for this issue: forcing the OS to flush the file to disk before reading it's size.Here is the code to do it :

require 'tempfile'

class Tempfile

def size

if @tmpfile

@tmpfile.fsync # added this line@tmpfile.flush@tmpfile.stat.size

else

0

end

end

end

Doing a flush is not enough... flush will flush the Ruby buffer but the file may not be immediately written to the disk by the OS. Doing the fsync ensure that the file is written to disk by the OS before continuing. After that, Windows will properly report the actual file size.

Fixing the Timeout error when uploading to S3

This issue is related to opening files for reading on Windows. On Windows, you have to open the file in binary mode. So patching attachment_fu is simple :

As I know this issue is only on Windows. Because my deploy env is linux and my development machine is Windows... how can I patch my Rails code to only affect development env?. Currently I have the patch on environment.rb file. I did the patch on development.rb but server crashes.Thanks a lot.

I'm having a similar problem using the April 2, 2007 version of attachment_fu on Rails 1.2.3 - does this patch require any adjustment for that environment? Also, where does the 'tempfile' library (as in `require 'tempfile'`) come from?