technoweenie / attachment_fu

Treat an ActiveRecord model as a file attachment, storing its patch, size, content type, etc.
http://weblog.techno-weenie.net
MIT License
1.02k stars 339 forks source link

in Windows, uploading to Amazon S3 and Rackspace Cloud Files fails #14

Open diegolopez opened 14 years ago

diegolopez commented 14 years ago

You have to change the save_to_storage from s3 and rackspace backends so instead of: (temp_path ? File.open(temp_path) : temp_data) you do: (temp_path ? File.open(temp_path, "rb") : temp_data)

Example for rackspace:

      def save_to_storage
        if save_attachment?
          @object = @@container.create_object(full_filename)
          @object.write((temp_path ? File.open(temp_path, "rb") : temp_data))
        end

        @old_filename = nil
        true
      end
technoweenie commented 14 years ago

Does it work with just File.open(temp_path, "rb")? #read will read it all into memory, which you do not want for large files. The S3Object#write method is designed to stream IO objects.

diegolopez commented 14 years ago

Yes!

So the example is: def save_to_storage if save_attachment? @object = @@container.create_object(full_filename)

FIXME: ugly hack to get it running on windows

      #somehow when You give file handle it won't read it
      if RUBY_PLATFORM =~ /(:?mswin|mingw)/
        @object.write((temp_path ? File.open(temp_path, "rb") : temp_data))
      else
        @object.write((temp_path ? File.open(temp_path) : temp_data))
      end
    end
    @old_filename = nil
    true
  end

Thanks!! I will edit the previous post.

technoweenie commented 14 years ago

Is this from the master branch? I haven't used this plugin in years. Also, you don't need the RUBY_PLATFORM check. You can pass 'rb' to it on unix systems. Other non-windows systems happily ignore the 'b'.

diegolopez commented 14 years ago

Does linux works ok with File.open(temp_path, "rb") ? So we don't need to do that ugly condition!

technoweenie commented 14 years ago

Correct.

diegolopez commented 14 years ago

I haven't commited my code...

technoweenie commented 14 years ago

You should do that...?

diegolopez commented 14 years ago

ok. thanks. I'll do it.

technoweenie commented 14 years ago

Ah yes, a pull request would be great if you want credit :)