https://github.com/rafbm/fileinfo
FileInfo detects file encodings and MIME types using the wonderful Unix `file` command.
https://github.com/rafbm/fileinfo
Last synced: over 1 year ago
JSON representation
FileInfo detects file encodings and MIME types using the wonderful Unix `file` command.
- Host: GitHub
- URL: https://github.com/rafbm/fileinfo
- Owner: rafbm
- License: mit
- Created: 2013-09-08T20:21:58.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2017-02-11T12:54:41.000Z (over 9 years ago)
- Last Synced: 2025-03-25T15:35:15.192Z (over 1 year ago)
- Language: Ruby
- Homepage:
- Size: 43.9 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# FileInfo
[](http://badge.fury.io/rb/fileinfo)
[](https://travis-ci.org/rafBM/fileinfo)
[](https://codeclimate.com/github/rafBM/fileinfo)
[](https://coveralls.io/r/rafBM/fileinfo?branch=master)
[](https://gemnasium.com/rafBM/fileinfo)
FileInfo detects file encodings and MIME types using the [wonderful Unix `file` command](http://en.wikipedia.org/wiki/File_\(command\)).
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'fileinfo'
```
And then execute:
```sh
$ bundle
```
Or install it yourself as:
```sh
$ gem install fileinfo
```
## Usage
### Detect encoding
Use `FileInfo.parse` with a string or `FileInfo.load` with a filename.
```ruby
FileInfo.parse('foo bar baz').encoding # => #
FileInfo.parse('föø bår bàz').encoding # => #
filename = '/Users/rafbm/Downloads/some_crap_coming_from_windows.csv'
FileInfo.load(filename).encoding # => #
```
### Detect MIME type
A bunch of methods are available depending on your needs.
```ruby
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.
```ruby
info = FileInfo.parse('Hello world')
info.mime_type # => #
```
**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`.
```ruby
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](http://rafbm.com). FileInfo is released under the MIT license.