Tempfile + Paperclip + ImageMagick

53 views
Skip to first unread message

Greg Moreno

unread,
Oct 1, 2009, 9:59:28 PM10/1/09
to Philippine Ruby Users Group (PRUG/PhRUG)
Hi guys,

I have this (strange) requirement in our simple avatar upload. I am
using paperclip but I must not create the temp files. Instead, the
uploaded file's content must be passed to ImageMagick as string. I
have no problem patching up paperclip (I already patched it to avoid
'convert') but I'm having problem telling Rails not to use a tempfile.
I have traced the code here
vendor/rails/actionpack/lib/action_controller/vendor/rack-1.0/rack/utils.rb.

When I replaced Tempfile with StringIO, it gets into an infinite loop.

Any ideas/workarounds? Removing paperclip is also an option it that
would make things better.


Regards,

Greg


--
Starting From Scratch
http://gregmoreno.ca

I'm on Twitter too!
http://twitter.com/gregmoreno

Katherine Giron Pe

unread,
Oct 1, 2009, 11:44:00 PM10/1/09
to ruby...@googlegroups.com
How about not using it...
That seems like a challenging task.
Even Carrierwave (http://github.com/jnicklas/carrierwave) seems to treat saving image or files the same way. Temporary files are stored in public/uploads/tmp..
--
-----
BridgeUtopia Web
http://blog.bridgeutopiaweb.com

Mark John Buenconsejo

unread,
Oct 3, 2009, 10:02:41 PM10/3/09
to ruby...@googlegroups.com
Greg, you mean both Rack and Paperclip should not use TempFile?

Since by default, Rack returns an UploadedFile object when someone
uploads a file.

(1) You can proably override this by adding a 'middleware' that parses
the request before the default Rack handler can process it. And then
using your own UploadedFile class (sub-classed)

(2) Then, you'll probably also gonna need to either subclass or
monkey-patch (extend) the UploadedFile class, to not use TempFile.

(3) The reason to keep using the UploadedFile, since other parts of
Rack or Rails or other plugins, might expect an uploaded object in
params to be of class UploadedFile

(4) Also note, that Paperclip always converts all assigned objects to
the attachment attribute to a TempFile. (via .to_tempfile method he
added to IO, StringIO andTempFile objects).

Perhaps, looking back to the reason why you should not use TempFile
will help, given the amount of stuffs needed to work without using
TempFile. :)

Thanks!

Mark

On Fri, Oct 2, 2009 at 9:59 AM, Greg Moreno <greg....@gmail.com> wrote:
>
--
------------------------------------
Mark John S. Buenconsejo
http://markjeee.com

Mark John Buenconsejo

unread,
Oct 3, 2009, 10:03:25 PM10/3/09
to ruby...@googlegroups.com
If you don't have access to the default /tmp folder, i think you can
specify some other directory as a repo for TempFiles, if this is what
you're trying to do.

Thanks!

Mark

On Fri, Oct 2, 2009 at 9:59 AM, Greg Moreno <greg....@gmail.com> wrote:
>

Greg Moreno

unread,
Oct 3, 2009, 10:18:05 PM10/3/09
to ruby...@googlegroups.com
I beginning to think this is a lot of work with very little benefits :(

- Greg

Greg Moreno

unread,
Oct 3, 2009, 10:18:30 PM10/3/09
to ruby...@googlegroups.com
Thanks Mark. The objective is not to hit to the disk

Mark John Buenconsejo

unread,
Oct 3, 2009, 10:43:35 PM10/3/09
to ruby...@googlegroups.com
I agree, this looks like a bit of work to do.

Tomorrow, at the office, i'll see how it can be done. I'm also working
on this area, with mogilefs and paperclip, and would be nice to have
an option that don't hit the disk at all, for small file sizes.

Thanks!

Mark

Greg Moreno

unread,
Oct 5, 2009, 12:28:36 AM10/5/09
to ruby...@googlegroups.com
Nice! We're also going to use mogilefs.

On Sat, Oct 3, 2009 at 7:43 PM, Mark John Buenconsejo
Reply all
Reply to author
Forward
0 new messages