# Attachment\_fu on Windows?

**URL:** <https://www.ruby-forum.com/t/attachment-fu-on-windows/87524>\
**Category:** Rails\
**Created:** [March 3, 2007, 12:49pm UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524 "2007-03-03T12:49:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 3, 2007, 12:49pm UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/1 "2007-03-03T12:49:25Z")

</div>

Has anyone had any success using Rick O.'s “attachment\_fu” plugin  
while developing on a Windows machine?

I’ve been looking for solutions to the issue described here  
[RailsNotes — The Ruby on Rails guides you wished you had.](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

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 4, 2007, 1:27pm UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/2 "2007-03-04T13:27:53Z")

</div>

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/](http://rubyforge.org/projects/rmagick/)

2. Installed AttachmentFU via

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

[http://blog.teksol.info/articles/2006/12/08/rmagick-and-jpeg-datastream-contains-no-image](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](http://clarkware.com/cgi/blosxom/2007/02/24)  
to see if you haven’t missed anything.

Eric

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 4, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/3 "2007-03-04T15:01:54Z")

</div>

Eric N. 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

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 4, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/4 "2007-03-04T15:32:23Z")

</div>

Jeff C.man wrote:

> Eric N. 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

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 5, 2007, 4:56pm UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/5 "2007-03-05T16:56:53Z")

</div>

Eric N. 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

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 5, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/6 "2007-03-05T22:34:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 4, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/7 "2007-03-04T17:51:21Z")

</div>

How about rolling back to 1.8.4 just to verify whether it’s the ruby  
release?

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 22, 2007, 3:04am UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/8 "2007-03-22T03:04:11Z")

</div>

Jeff,

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

Eric

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 6, 2007, 8:05am UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/9 "2007-03-06T08:05:24Z")

</div>

Adam R. 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

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 24, 2007, 1:58pm UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/10 "2007-03-24T13:58:08Z")

</div>

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/](http://blog.72bit.com/2007/03/12/attachment_fu-on-windows/)

Wes

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 31, 2007, 12:36am UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/11 "2007-03-31T00:36:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 25, 2007, 10:49am UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/12 "2007-03-25T10:49:15Z")

</div>

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/](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](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

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 31, 2007, 12:48am UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/13 "2007-03-31T00:48:46Z")

</div>

Eric N. 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

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 31, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/14 "2007-03-31T20:49:53Z")

</div>

Eric N. 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

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 31, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/15 "2007-03-31T20:47:11Z")

</div>

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](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 C.man [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [April 10, 2007, 1:34am UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/16 "2007-04-10T01:34:07Z")

</div>

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 C.man wrote:

> Eric N. 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

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [July 7, 2007, 9:44am UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/17 "2007-07-07T09:44:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [July 27, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/18 "2007-07-27T21:35:44Z")

</div>

This solution worked for me. I was using :db\_file

What an ordeal.

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [June 6, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/19 "2007-06-06T13:15:38Z")

</div>

Good gravy Eric N.'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.

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [September 14, 2007, 4:34pm UTC](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524/20 "2007-09-14T16:34:39Z")

</div>

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 🙂

[Next page](https://www.ruby-forum.com/t/attachment-fu-on-windows/87524.md?page=2)
