Problems attaching a file from a string source

100 views
Skip to first unread message

Tony

unread,
Jun 6, 2009, 10:59:37 PM6/6/09
to papercli...@googlegroups.com
I'm indexing a music library, and trying to use Paperclip to manage the cover art associated with each album, as well as generating some various sized thumbnails, which seems like an ideal use for Paperclip.

I'm extracting the album art using the mp3info library, which returns a "blob" of the image data in the form of a string.

I have an Album class which descends from ActiveRecord::Base, and I've done the standard:

  has_attached_file :cover_art, :styles => { :medium => "300x300>", :thumb => "100x100>" }

...referenced in the examples.

I'm at a bit of a loss as to how to try to attach this blob with Paperclip.  I'd like to avoid writing the blob out to a tempfile if possible.  I've tried various approaches using a StringIO, such as:

update_attributes :cover_art => StringIO.new(blob)
cover_art.assign StringIO.new(blob)
self.cover_art = StringIO.new(blob)

as far as I can tell, none of these seem to be working.  I'm also at a loss as to how to test if the fine actually got attached.  The RDoc at:

http://github.com/thoughtbot/paperclip/blob/master/README.rdoc

Describes the following method:

file?

Returns true if a file has been assigned.

However when I try to use this in my tests to verify the file was attached successfully:

undefined method `file?' for #<Paperclip::Attachment:0x2522f04>

This is with Paperclip 2.1.2.

Looking at the source I see file? checks original_filename is not nil.  Trying that out with all the above approaches, and something like:

cover_art.assign StringIO.new(blob)
cover_art.save

The cover_art.save call returns true, and yet when I check if original_filename is nil:

expected nil? to return false, got true

I'm confused... help?

--
Tony Arcieri
medioh.com

Tony

unread,
Jun 7, 2009, 6:12:47 PM6/7/09
to papercli...@googlegroups.com
On Sat, Jun 6, 2009 at 8:59 PM, Tony <bas...@gmail.com> wrote:
I'm confused... help?

Okay, I did a bit of spelunking through the source and discovered I need to have a class that responds to original_filename as well as content_type

That seems to have fixed my problem, except for the nonexistent Paperclip::Attachment#file? method

--
Tony Arcieri
medioh.com

DarrinW

unread,
Jul 1, 2009, 8:54:55 AM7/1/09
to Paperclip Plugin
I need to do something very similar.

Can you please provide an example of how you got this to work?

Regards,

Darrin.

Lee Hambley

unread,
Jul 1, 2009, 9:10:24 AM7/1/09
to papercli...@googlegroups.com
I worked this by writing the string to a TempFile and then attaching the tempfile, I can try and dig out some code, but you should give it a shot... I used the resources about attaching files in background scripts, because that's essentially the same thing... attaching a file from the `server` filesystem to a paperclipped object.... works quite neatly, actually.

- Lee

2009/7/1 DarrinW <dwort...@googlemail.com>
Reply all
Reply to author
Forward
0 new messages