Paperclip: Assign an image programmatically and specify its name

Using "Clip", I want to get an image from a URL, for example:

require 'open-uri'

user.photo = open(url)

The problem is that I am terminating with the file name open-uri20110915-4852-1o7k5uw. Is there a way to change the file name to user.photo?

As an added twist, Paperclip stores my files on S3, so it would be even better if I could set the file name that I want in the original destination, so the images are uploaded to the right S3 key. Something like that:

user.photo = open(url), :filename => URI.parse(url).path
+5
source share
2 answers

You can write the file name to your instance with:

 photo.instance_write(:file_name, new_file_name)

So if your source url looks like:

 http://example.com/foo.jpg

URL- ( foo.jpg), , instance_write.

+6
io = open(url)
def io.original_filename; base_uri.path.split('/').last; end

URL-.

+5

All Articles