Sha256: 5f3bf6a56fc75b1ef70a76a7563380eb6bced6391616c6e654fc8861589341fa

Contents?: true

Size: 878 Bytes

Versions: 4

Compression:

Stored size: 878 Bytes

Contents

require "cocaine"
require "mime-types"

module Anyfetch
  module OriginalFilename
    module ContentType
      def original_filename
        if meta.include? 'content-disposition'
          match = meta['content-disposition'].match(/filename=(\"?)(.+)\1/)
          return match[2] unless match.nil?
        end

        filename = ::File.basename(base_uri.path)
        ext = ::File.extname(filename)

        cmd = Cocaine::CommandLine.new('/usr/bin/file', '--mime-type -b :file')
        begin
          mime_type = cmd.run(:file => path)
        rescue
          mime_type = meta['content-type']
        end

        if mime = MIME::Types[mime_type.to_s.strip].first
          mime_ext = mime.extensions.first
          ext != mime_ext ? [::File.basename(filename, ext), '.', mime_ext].join : filename
        else
          filename
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
anyfetch-0.1.4 lib/anyfetch/original_filename/content_type.rb
anyfetch-0.1.3 lib/anyfetch/original_filename/content_type.rb
anyfetch-0.1.2 lib/anyfetch/original_filename/content_type.rb
anyfetch-0.1.1 lib/anyfetch/original_filename/content_type.rb