Batch uploading data and using file_column

Hi I have a site which is working quite nicely at the moment but i want
to batch upload a large amount of data from a csv file. Up until now i
have added new items by hand through the forms, now i have a script to
do it. The script is fine up until i try to run the images through
file_column. The problem being that the images are stored as url’s. So
question is how can i present to url’s to file column in a way that it
understands how to follow them and get the files or alternatively how
can i get the images pointed to by the url and then present them to
file_column as if they were uploaded data.

thanks alot
regards
c

On 11 October 2006 18:34, Clare wrote:

Hi I have a site which is working quite nicely at the moment but i want
to batch upload a large amount of data from a csv file. Up until now i
have added new items by hand through the forms, now i have a script to
do it. The script is fine up until i try to run the images through
file_column. The problem being that the images are stored as url’s. So
question is how can i present to url’s to file column in a way that it
understands how to follow them and get the files or alternatively how
can i get the images pointed to by the url and then present them to
file_column as if they were uploaded data.

I think the best way is to handle such cases manually and not alter the
file_column. Something like that:

require ‘open-uri’

uri = “http://somewhere.example.com/myimage.gif

open(uri) do |remote|
uploadedfile = StringIO.new(remote.read)
end

filename = File.basename(uri)
(class << uploadedfile; self; end).class_eval do
define_method(:local_path) { “” }
define_method(:original_filename) { filename }
define_method(:content_type) { “application/octet-stream” }
end

Then assign this uploadedfile to file_column attribute.

Dear Maxim
Thank you very much for replying but sadly I’m not yet good enough to
understand your fancy code.

I used it as follows but recieced the following error:

Sorry i usually have a better crack at solving issues but I don’t
understand what is going on in the section that begins (class <<
uploadedfile; self; end).class_eval. If you get time to reply that would
be excellent, if not thanks for giving me something to investigate
further.

regards
c

NameError: undefined local variable or method uploadedfile' for VoObject:Class from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1129:in method_missing’

if imageLink.size > 0
uri =
http://www.desperateseller.co.uk/DespSellPhotos/101946825798.jpg
open(uri) do |remote|
uploadedfile = StringIO.new(remote.read)
end

filename = File.basename(uri)
(class << uploadedfile; self; end).class_eval do
define_method(:local_path) { “” }
define_method(:original_filename) { filename }
define_method(:content_type) { “application/octet-stream” }
end

vobject.primary_image = uploadedfile
end

Maxim K. wrote:

On 11 October 2006 18:34, Clare wrote:

Hi I have a site which is working quite nicely at the moment but i want
to batch upload a large amount of data from a csv file. Up until now i
have added new items by hand through the forms, now i have a script to
do it. The script is fine up until i try to run the images through
file_column. The problem being that the images are stored as url’s. So
question is how can i present to url’s to file column in a way that it
understands how to follow them and get the files or alternatively how
can i get the images pointed to by the url and then present them to
file_column as if they were uploaded data.

I think the best way is to handle such cases manually and not alter the
file_column. Something like that:

require ‘open-uri’

uri = “http://somewhere.example.com/myimage.gif

open(uri) do |remote|
uploadedfile = StringIO.new(remote.read)
end

filename = File.basename(uri)
(class << uploadedfile; self; end).class_eval do
define_method(:local_path) { “” }
define_method(:original_filename) { filename }
define_method(:content_type) { “application/octet-stream” }
end

Then assign this uploadedfile to file_column attribute.

Clare wrote:

Thank you very much for replying but sadly I’m not yet good enough to
understand your fancy code.

Sorry i usually have a better crack at solving issues but I don’t
understand what is going on in the section that begins (class <<
uploadedfile; self; end).class_eval. If you get time to reply that would
be excellent, if not thanks for giving me something to investigate
further.
“class << uploadedfile; self; end” is a common technique to get
singleton class for that very instance to be able to extend it. I think
it could be done in a more simple way, I just took a code from
file_column’s test method that simulate an uploaded file.
In Rails, uploaded file is an IO object or the like (which has “read”
method) extended with some instance methods (local_path,
original_filename, content_type).

Regarding your error, I think you haven’t required ‘open-uri’ library
and this line
uploadedfile = StringIO.new(remote.read)
haven’t got executed. That’s probably why you’ve got “undefined
variable” error. I suggest you to put all the code inside open(uri)
block so you’ll ensure that nothing will happen if it is unable to open
that uri.

Thanks again,
got it working
really silly error

uploadedfile = nil

needs to be added before the open(uri) block.

really should have spotted that.
cheers
c