Project

fileinfo

0.01
No commit activity in last 3 years
No release in over 3 years
FileInfo detects file encodings and MIME types using the wonderful Unix `file` command.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
>= 0
 Project Readme

FileInfo

Gem Version Build Status Code Climate Coverage Status Dependency Status

FileInfo detects file encodings and MIME types using the wonderful Unix file command.

Installation

Add this line to your application's Gemfile:

gem 'fileinfo'

And then execute:

$ bundle

Or install it yourself as:

$ gem install fileinfo

Usage

Detect encoding

Use FileInfo.parse with a string or FileInfo.load with a filename.

FileInfo.parse('foo bar baz').encoding # => #<Encoding:US-ASCII>
FileInfo.parse('föø bår bàz').encoding # => #<Encoding:UTF-8>

filename = '/Users/rafbm/Downloads/some_crap_coming_from_windows.csv'
FileInfo.load(filename).encoding # => #<Encoding:ISO-8859-1>

Detect MIME type

A bunch of methods are available depending on your needs.

info = FileInfo.load('picture.jpg')
info.type       # => "image/jpeg"
info.media_type # => "image"
info.sub_type   # => "jpeg"

The #mime_type method can also return a MIME::Type instance.

info = FileInfo.parse('Hello world')
info.mime_type # => #<MIME::Type:0x007f81a1a36090 @content_type="text/plain" ...>

NOTE: You must install the mime-types gem yourself as it is not a dependency of fileinfo.

Finally, #content_type can be used to get the full string returned by file --mime --brief.

info = FileInfo.parse('Hëllø wõrld')
info.content_type # => "text/plain; charset=utf-8"

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

© 2017 Rafaël Blais Masson. FileInfo is released under the MIT license.