Importing images with file_column over http?

I haven’t cracked the source of file_column just yet - but has anyone
worked
out a way to snag images via URL instead of a file upload?

Optimally I’d like to have my clients be able to upload a file, or enter
in
a URL to store as file on the system.

Any/all help would be cool.

On Jun 21, 2006, at 8:15 PM, subimage interactive wrote:

seth at subimage interactive
http://www.subimage.com/sublog/


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Try something like this:

require ‘open-uri’

def fetch_resource(options={})
File.open(options[:to], “wb”) { |f| f.write open(options
[:from]).read }
end

fetch_resource :from => “http://example.com/foo.gif”,
:to => “#{RAILS_ROOT}/public/assets/
foo.gif”

Cheers-
-Ezra

Thanks EZ, but that’s the easy part :slight_smile:

The hard part is getting file_column to take it and do its magic. Guess
I
need to look into the source. I saw a BaseUploadedFile::assign that
might be
useful in combination with your code.

That got me on the right track…here’s my full controller method.
image_url
is pre-populated most of the time by my data import scripts.

Hopefully someone else finds this snippet useful.

Imports an image from the item’s url and feeds it into the image

upload

field.

file_column automagically resizes it, and we set the item to use the

newly

uploaded image

def import_image
require ‘open-uri’

@item = Item.find(params[:id])
if [email protected]
  redirect_to :back and return
end

import_from = @item.image_url.clone
chop_position = import_from.rindex("/")

file_name = import_from[chop_position+1..import_from.size-1]

save_to = "#{RAILS_ROOT}/public/item/image_upload/test/#{file_name}"
File.open(save_to, "wb") { |f| f.write open(import_from).read }

@item.image_upload = File.open(save_to)
@item.image_use_option = 2

@item.save

redirect_to :action => 'edit', :id => @item

end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs