Class: MIME::Typer

Inherits:
Object
  • Object
show all
Defined in:
lib/mime-typer.rb

Overview

Public: A simple class to detect mime type of a file, using libmagic You can detect both local and remote file types

Examples

MIME::Typer.detect('/my/awesome/image/path.jpg')
# => 'image/jpeg'

Class Method Summary collapse

Class Method Details

.detect(file) ⇒ Object

Public: Detects the MIME type of a file, given a path or a file

file - The String path or the File to be detected

Examples

detect('/my/awesome/image/path.jpg')
# => 'image/jpeg'

Returns a String containing the MIME Type.



27
28
29
30
# File 'lib/mime-typer.rb', line 27

def self.detect(file)
  path = file.is_a?(File) ? file.path : file
  `file --mime-type #{path} | awk '{ print $(NF) }'`.strip
end

.remote(uri) ⇒ Object

Public: Detects the MIME type of a remote file, given an url

uri - The String uri of the file

Examples

remote('http://my.awesome.site.com/my_image.jpg')
# => 'image/jpeg'

Returns a String containing the MIME Type.



42
43
44
45
46
47
48
# File 'lib/mime-typer.rb', line 42

def self.remote(uri)
  temp = Tempfile.new('mime-typer')
  temp.binmode
  temp.write(load_remote(uri))
  temp.close
  detect temp.path
end