File_column do download from URL

Is there a simple way of forcing file_column to download file from given
(ex. in form) url instead of uploading it manualy?

I know:
http://blog.caboo.se/articles/2006/01/09/file_column-magick-and-versions

require ‘open-uri’
Attachment.find_first.filename = open(url)
if not working as expected… what is interesting is that:

@a = open(‘http://www.google.com/intl/pl_ALL/images/logo.gif’)
=> #StringIO:0x3c22390
@a = open(‘http://research.microsoft.com/~dcr/art/lamps/collage/P0002190.JPG’)
=> #<File:C:/DOCUME~1/VALUED~1/LOCALS~1/Temp/open-uri356.2>

smaller files return StringIO
larger - File (Tempfile to be specific)

Any hints?

using your example

look at
@a =
open('http://www.google.com/intl/pl_ALL/images/logo.gifhttp://www.google.com/intl/pl_ALL/images/logo.gif’
)
@a.instance_values

you can extract what you need from base_uri, which does make my way look
a
lttle obtuse

let me know how you get on

On 8/18/06, Wiktor — [email protected] wrote:


Posted via http://www.ruby-forum.com/.


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

www.blogsaic.com
search, post, blog

Dion,

Thanks!

Here is how I fixed it:

In model:

attr_accessor :url
before_save :check_url

private
def check_url
return if self.url.nil? or self.url.empty?
file = open(self.url)
if file.is_a? StringIO
file.extend MyIOCompat
else
file.extend FileColumn::FileCompat
end
self.filename = file
end

end

module MyIOCompat
def original_filename; ‘from_url’; end
end

There is no error checking so there may be some 500 errors but I use it
only for admin purposes so it is ok.

this is quite easy I have done it at http://www.blogsaic.com/

sign up and upload a tile to see how it works


you can’t send the url to file column

but, what you can do is upload a file from a url to the same table
column
using open-uri

you have to do a bit more than just Attachment.find_first.filename =
open(url)

my way is not very optimised, but it does the trick…for now because I
still want to retain the original file name, and open(url) only uses
some
cgi generated name

in the tmp directory

but basically the below code checks if the url is formatted correctly in
the
http// format

then it strips the file name from the full file path posted in the
upload
form

then it copies the temp file to the file_column with @tile.img_name =
File.new(ff.path)

you’ll need to add these to your environment.rb to use the system/kernel
commands

#Needed for url uploads
require ‘open-uri’
require ‘ftools’
require ‘fileutils’

this is my upload action

@tile = Tile.new(params[:tile])
@tile.fk_tbl_user_id = @session[:user].id

  orig = params[:tile][:img_path]   ----------------- this is from 

the
form at http://www.blogsaic.com/tile/

raise if not orig =~
/^(http|https)://[a-z0-9]+([-.]{1}[a-z0-9]+).[a-z]{2,5}(([0-9]{1,5})?/.)?$/ix

   logger.error "???????????? VALUE >> #{orig}"
   response = false
      #if good url format, get the file
      tn = open(orig)
      filename = orig.split("/")

      tp = tn.path
      nf = File.new(tp)
      nf_path = File.split(nf.path)

      f = "#{nf_path[0]}" << "/" << "#{filename.last}"

      FileUtils.touch f

      #sleep 2
      ff = File.new("#{f}")
      FileUtils.copy nf.path, ff.path

      @tile.img_name = File.new(ff.path) #gold

   rescue
      if response == false
        logger.error "???????????? BAD URL >> #{orig}"
      end
   end
 else

etc etc then do your normal save stuff…

@tile.save etc etc

http://www.blogsaic.com/

I have some extra stuff that checks if the file is from teh file system
or a
URL…but I hope this is enough to get you in the right direction

cheers

dion

On 8/18/06, Wiktor — [email protected] wrote:


Posted via http://www.ruby-forum.com/.


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

www.blogsaic.com
search, post, blog