subimage interactive
2006-Jun-22 03:15 UTC
[Rails] 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. -- seth at subimage interactive http://www.subimage.com/sublog/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060622/9942cc31/attachment.html
Ezra Zygmuntowicz
2006-Jun-22 04:58 UTC
[Rails] Importing images with file_column over http?
On Jun 21, 2006, at 8:15 PM, subimage interactive wrote:> 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. > > -- > seth at subimage interactive > http://www.subimage.com/sublog/ > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/railsTry 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060622/82cc7ea1/attachment.html
subimage interactive
2006-Jun-22 05:38 UTC
[Rails] Importing images with file_column over http?
Thanks EZ, but that''s the easy part :) 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. On 6/21/06, Ezra Zygmuntowicz <ezmobius@gmail.com> wrote:> > > On Jun 21, 2006, at 8:15 PM, subimage interactive wrote: > > 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. > > -- > seth at subimage interactive > http://www.subimage.com/sublog/ > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- seth at subimage interactive http://www.subimage.com/sublog/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060622/8be0d925/attachment-0001.html
subimage interactive
2006-Jun-22 08:38 UTC
[Rails] Importing images with file_column over http?
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 !@item
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
On 6/21/06, subimage interactive <subimage@gmail.com>
wrote:>
> Thanks EZ, but that''s the easy part :)
>
> 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.
>
>
> On 6/21/06, Ezra Zygmuntowicz <ezmobius@gmail.com> wrote:
> >
> >
> > On Jun 21, 2006, at 8:15 PM, subimage interactive wrote:
> >
> > 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.
> >
> > --
> > seth at subimage interactive
> > http://www.subimage.com/sublog/
> > _______________________________________________
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > 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
> >
> > _______________________________________________
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
> >
> >
>
>
> --
> seth at subimage interactive
> http://www.subimage.com/sublog/
>
--
seth at subimage interactive
http://www.subimage.com/sublog/
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060622/dbaa975d/attachment.html