The purpose of gem filedesc is to help the developers to get information about local files such as mime-type,size,gid,description so on. It works as a module of File and the unix command ‘file’ developer.apple.com/documentation/Darwin/Reference/Manpages/man1/file.1.html i.e. it doesn’t uses static mapping to determine mime-type instead of it uses a couple of ‘magic files’ (/usr/share/file/magic.mgc, /usr/share/file/magic, etc) that gives to the system the way to get file info.
This project just works for unix-like.
If you have been using some api to get mime-type by extension… it doesn’t reflects the truth… see an example:
A file called foo.pdf with an extension pdf and the content is a pdf(with header %PDF-1.4ˆM) then your api will work fine but whether the user renames foo.pdf to foo.txt your api will fail or better it will determine incompatible data with the file extension.
Using the command ‘file’ this will work the way that you are expecting. See into your shell.
shairon -> tmp: file foo.pdf foo.pdf: PDF document, version 1.4 shairon -> tmp: mv foo.pdf foo.txt shairon -> tmp: file foo.txt foo.txt: PDF document, version 1.4
Got it?
sudo gem install filedesc
The filedesc is a module that is included into class File, so you can use it the same way but don’t forget the require ‘filedesc’.
require 'filedesc' file=File.new('Desktop/Picture 1.png') file.mime #=> 'image/png' file.description #=> 'PNG image data, 773 x 519, 8-bit/color RGB, non-interlaced'
file.is_a_mime?('image/png') #=> true file.is_a_mime?(/^image/) #=> true file.has_description?(/8-bit\/color.*RGB/) #=> true file.extension #=> "png"
And finally the method File#info that gets relevant information about the file and returns a Hash.
file.info
It will return something like
{ :last_modified=>Wed Jul 29 18:42:42 -0400 2009, :filename=>"Picture 1.png", :last_access=>Wed Jul 29 18:45:11 -0400 2009, :mode=>33188, :path=>"/Users/shairon/Desktop/Picture 1.png", :executable=>false, :extension=>"png", :mime=>"image/png", :uid=>501, :description=>"PNG image data, 773 x 519, 8-bit/color RGB, non-interlaced", :gid=>20, :size=>154504, :directory=>"/Users/shairon/Desktop" }
The file inspect with new format with mime-type, description and extension.
Dir.glob('*'){|f| puts File.open(f).inspect }
Returns
#<File:foo.doc, mime: 'application/msword', description: 'Microsoft Office Document', extension: 'doc' > #<File:foo.htm, mime: 'text/html', description: 'HTML document text', extension: 'htm' > #<File:foo.rtf, mime: 'text/rtf', description: 'Rich Text Format data, version 1,', extension: 'rtf' > #<File:foo.pdf, mime: 'application/pdf', description: 'PDF document, version 1.3', extension: 'pdf' > #<File:foo.txt, mime: 'text/plain; charset=unknown', description: 'Non-ISO extended-ASCII text, with CR line terminators', extension: 'txt' > #<File:foo.mp3, mime: 'audio/mpeg', description: 'MP3 file with ID3 version 2.3.0 tag', extension: 'mp3' > #<File:foo.png, mime: 'image/png', description: 'PNG image data, 773 x 417, 8-bit/color RGBA, non-interlaced', extension: 'png' > #<File:foo.jpg, mime: 'image/jpeg', description: 'JPEG image data, JFIF standard 1.01', extension: 'jpg' > #<File:foo.zip, mime: 'application/x-zip', description: 'Zip archive data, at least v2.0 to extract', extension: 'zip' >