DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Make A Remote URL Work Like A File Upload (in Rails)

05.11.2007
| 29228 views |
  • submit to reddit
        Want to load a remote URL into an acts_as_attachment/attachment_fu model?  Use this little utility class.

  class UrlUpload
    EXTENSIONS = {
      "image/jpeg" => ["jpg", "jpeg", "jpe"],
      "image/gif" => ["gif"],
      "image/png" => ["png"]
    }
    attr_reader :original_filename, :attachment_data
    def initialize(url)
      @attachment_data = open(url)
      @original_filename = determine_filename
    end

    # Pass things like size, content_type, path on to the downloaded file
    def method_missing(symbol, *args)
      if self.attachment_data.respond_to? symbol
        self.attachment_data.send symbol, *args
      else
        super
      end
    end
    
    private
      def determine_filename
        # Grab the path - even though it could be a script and not an actual file
        path = self.attachment_data.base_uri.path
        # Get the filename from the path, make it lowercase to handle those
        # crazy Win32 servers with all-caps extensions
        filename = File.basename(path).downcase
        # If the file extension doesn't match the content type, add it to the end, changing any existing .'s to _
        filename = [filename.gsub(/\./, "_"), EXTENSIONS[self.content_type].first].join(".") unless EXTENSIONS[self.content_type].any? {|ext| filename.ends_with?("." + ext) }
        # Return the result
        filename
      end
  end

Now when you have the URL you want to load, do something like this:

@model.uploaded_data = UrlUpload.new(url)

Or better yet, make a pseudo-accessor on your aaa/attachment_fu model so you can stay "model-heavy".

def url=(value)
  self.uploaded_data = UrlUpload.new(value)
end
    

Comments

Snippets Manager replied on Fri, 2009/09/18 - 1:06pm

Thanks, I had part of this code written and your saved me loads of time finishing it off. Great job! Regards, The Toy Trains Guy

Levi Kopsen replied on Wed, 2009/09/09 - 8:03am

Thanks for working out and making it available to us. I was looking for it from a very long time. Thanks nokia 5310

Snippets Manager replied on Fri, 2009/09/18 - 1:06pm

Do you need to include OpenURL? Model Trains

fjkkj jkldsjfklj replied on Mon, 2009/07/06 - 5:48am

I followed all that you said but I didnt got any proper results out of it and I am going to earn just B grade in my online paralegal certificate. Regards,

Snippets Manager replied on Thu, 2009/06/25 - 6:40am

The technology use additionally facilitates better time management. It is and more effective and time efficient for students and teachers to use this software to create outlines and maps than would be on paper. regards, Opel Radiohalterung

gfdgf fgds replied on Tue, 2009/06/16 - 5:44am

Truly amazing !!! I love this one. Thanks very much for the post. Now I need to upload this. Regards, bingo reviews

gfdgf fgds replied on Thu, 2009/06/18 - 5:41am

Even though I couldn't get proper info on this post I agree that still it is a good one. Regards, steering wheel control

gfdgf fgds replied on Tue, 2009/06/16 - 5:44am

I think you should let the surfers to know more about this post. Thanks for the share. Regards, free games

kjh;kljh hkjhkjg replied on Mon, 2009/06/15 - 7:18am

I agree with it.These are the great moments after two protesters wearing coloured wigs disrupted the start of Mahmoud Ahmadinejad’s speech.Thanks. Regards, remove asbestos

Snippets Manager replied on Wed, 2009/06/03 - 4:31am

Good post.But needed much info to understand completely.I had an interest to learn more.Really good post for sharing.Online games

Snippets Manager replied on Tue, 2009/06/02 - 11:06am

Good work.We must need to include the open URL source.This is a great providing for us.Thanks,dating services

gfgfg fgfgf replied on Mon, 2009/06/01 - 11:49pm

What a great possibility available.....the cool thing about recursion is that it can be super-fast and mind-bending.I like this to share in Lighting Fixtureswritings. I just couldn't resist the potential here.

Snippets Manager replied on Sun, 2007/12/16 - 4:43pm

@seanscribbs Where in open-uri.rb do I put that snippet?

Snippets Manager replied on Tue, 2007/11/20 - 5:21am

Yes you need to include the OpenURI library. Use this: require 'open-uri'

Snippets Manager replied on Mon, 2006/11/27 - 11:49pm

How do i use this script? It doesn´t seem to work. Do I need to include OpenURI or something?

Snippets Manager replied on Wed, 2006/03/29 - 10:25pm

One additional thing I had to do to make it fail-safe is open up the open-uri library and set it to always use Tempfile downloads, rather than StringIO. Here's a monkey-patch for it: # Make it always write to tempfiles, never StringIO OpenURI::Buffer.module_eval { remove_const :StringMax const_set :StringMax, 0 }