Using Paperclip, I want to grab an image from a URL like this:
require 'open-uri'
user.photo = open(url)
The problem is that I then wind up with a filename like 'open-uri20110915-4852-1o7k5uw'. Is there any way I can change the filename on user.photo?
As an added twist, Paperclip is storing my files on S3, so it'd be even better if I could set the filename I want in the initial assignment, so images get uploaded to the right S3 key. Something like this:
user.photo = open(url), :filename => URI.parse(url).path
You can write the filename back to your instance via:
photo.instance_write(:file_name, new_file_name)
So if your source URL is like:
http://example.com/foo.jpg
You can first parse the name from the URL (to get foo.jpg
) and then write it back to the instance using the above instance_write
method.
io = open(url)
def io.original_filename; base_uri.path.split('/').last; end
This will set the filename to the filename in the url.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With