Paperclip: Upload from Url with Extension

Paperclip: upload from url with extension

Good timing. I just sent a pull request that was patched in a few hours ago (Jul 20 2012) which should make your life real easy.

self.asset = URI.parse("http://s3.amazonaws.com/blah/blah/blah.jpg")

This will download your jpeg image, ensure that the filename is blah.jpg and the content type is 'image/jpg'

Paperclip version > 3.1.3 (you'll need to link it to github repo until it gets released).

UPDATE: confirmed working with paperclip version >= 3.1.4

Save image from URL by paperclip

Here is a simple way:

require "open-uri"

class User < ActiveRecord::Base
has_attached_file :picture

def picture_from_url(url)
self.picture = open(url)
end
end

Then simply :

user.picture_from_url "http://www.google.com/images/logos/ps_logo2.png"

Paperclip add image from URL in has_many association

Here is a great gist (that I did not write). It should get you there: https://gist.github.com/jgv/1502777

require 'open-uri'

class Photo < ActiveRecord::Base

has_attached_file :image # etc...

before_validation :download_remote_image, :if => :image_url_provided?

validates_presence_of :image_remote_url, :if => :image_url_provided?, :message => 'is invalid or inaccessible'

private

def image_url_provided?
!self.image_url.blank?
end

def download_remote_image
io = open(URI.parse(image_url))
self.original_filename = io.base_uri.path.split('/').last
self.image = io
self.image_remote_url = image_url
rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
end

end

All credit to the author.

In the future, it's usually best to post the code with your attempt to solve the problem.

Rails paperclip image URL uploading not working

I was in the middle of writing a pretty through response, and then I found someone already wrote the same thing!!

Here you go: http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/

This should be exactly what you are looking for. If you stumble, let me know.

Good luck

How to use original image url in paperclip

<%= photo.attachment.url(:original) %>

That will give you the path to the original file, you can also get the other styles if needed:

<%= photo.attachment.url(:thumbnail) %>
<%= photo.attachment.url(:profile_gallery) %>


Related Topics



Leave a reply



Submit