Search code examples
rubyopen-uri

In Ruby how can I tell what the extension is to an image file that's hosted on the internet?


I want to download some images from the web, however some of the URLs do not specify the file extension such as:

http://sportslabs-webproxy.imgix.net/http%3A%2F%2Fkty-platform-prod.silverchalice.co%2Fv3%2Fimages%2Fcontents%2F55bbe945e4b073340d3851fb?fit=clip&h=532&w=800&s=61b00197aca130a83de011484841158e

I was going to use the following method mentioned in "How do I download a picture using Ruby?" to download the files, but as I said wasn't sure how to tell the script what file extension to save it as.


Solution

  • Look into the ruby-filemagic gem.

    For example:

    require 'open-uri'
    require 'filemagic'
    
    url = 'http://sportslabs-webproxy.imgix.net/http%3A%2F%2Fkty-platform-prod.silverchalice.co%2Fv3%2Fimages%2Fcontents%2F55bbe945e4b073340d3851fb?fit=clip&h=532&w=800&s=61b00197aca130a83de011484841158e'
    
    open('raw_file', 'wb') do |file|
      file << open(url).read
    end
    
    puts FileMagic.new(FileMagic::MAGIC_MIME).file( 'raw_file' )
    # => 'image/jpeg; charset=binary'
    

    UPDATE: To find the extension to save the file as you can use mime-types

    content_type = FileMagic.new(FileMagic::MAGIC_MIME).file( 'raw_file' ).split( ';' ).first
    
    require 'mime/types'
    puts MIME::Types[content_type].first.extensions.first 
    # => 'jpeg'