Skip to content

Commit 2bbfd78

Browse files
committed
Update README to clarify the detection heuristic
1 parent 36a94f2 commit 2bbfd78

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

README.md

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
# Marcel
22

3-
Marcel attempts to choose the most appropriate content type for a given file by looking at the binary data, the filename, and any declared type (perhaps passed as a request header). This is done via the `Marcel::MimeType.for` method, and is used like this:
3+
Marcel chooses the most appropriate content type for a file by inspecting its contents, the declared MIME type (perhaps passed as a Content-Type header), and the file extension.
4+
5+
Marcel checks, in order:
6+
7+
1. The "magic bytes" sniffed from the file contents.
8+
2. The declared type, typically provided in a Content-Type header on an uploaded file, unless it's the `application/octet-stream` default.
9+
3. The filename extension.
10+
4. Safe fallback to the indeterminate `application/octet-stream` default.
11+
12+
At each step, the most specific MIME subtype is selected. This allows the declared type and file extension to refine the parent type sniffed from the file contents, but not conflict with it. For example, if "file.csv" has declared type `text/plain`, `text/csv` is returned since it's a more specific subtype of `text/plain`. Similarly, Adobe Illustrator files are PDFs internally, so magic byte sniffing indicates `application/pdf` which is refined to `application/illustrator` by the `ai` file extension. But a PDF named "image.png" will still be detected as `application/pdf` since `image/png` is not a subtype.
13+
14+
## Usage
415

516
```ruby
17+
# Magic bytes sniffing alone
618
Marcel::MimeType.for Pathname.new("example.gif")
719
# => "image/gif"
820

@@ -11,37 +23,26 @@ File.open "example.gif" do |file|
1123
end
1224
# => "image/gif"
1325

26+
# Magic bytes with filename fallback
1427
Marcel::MimeType.for Pathname.new("unrecognisable-data"), name: "example.pdf"
1528
# => "application/pdf"
1629

30+
# File extension alone
1731
Marcel::MimeType.for extension: ".pdf"
1832
# => "application/pdf"
1933

34+
# Magic bytes, declared type, and filename together
2035
Marcel::MimeType.for Pathname.new("unrecognisable-data"), name: "example", declared_type: "image/png"
2136
# => "image/png"
2237

38+
# Safe fallback to application/octet-stream
2339
Marcel::MimeType.for StringIO.new(File.read "unrecognisable-data")
2440
# => "application/octet-stream"
2541
```
2642

27-
By preference, the magic number data in any passed in file is used to determine the type. If this doesn't work, it uses the type gleaned from the filename, extension, and finally the declared type. If no valid type is found in any of these, "application/octet-stream" is returned.
28-
29-
Some types aren't easily recognised solely by magic number data. For example Adobe Illustrator files have the same magic number as PDFs (and can usually even be viewed in PDF viewers!). For these types, Marcel uses both the magic number data and the file name to work out the type:
30-
31-
```ruby
32-
Marcel::MimeType.for Pathname.new("example.ai"), name: "example.ai"
33-
# => "application/illustrator"
34-
```
35-
36-
This only happens when the type from the filename is a more specific type of that from the magic number. If it isn't the magic number alone is used.
37-
38-
```ruby
39-
Marcel::MimeType.for Pathname.new("example.png"), name: "example.ai"
40-
# => "image/png"
41-
# As "application/illustrator" is not a more specific type of "image/png", the filename is ignored
42-
```
43+
## Extending
4344

44-
Custom file types not supported by Marcel can be added using `Marcel::MimeType.extend`.
45+
Custom file types may be added with `Marcel::MimeType.extend`:
4546

4647
```ruby
4748
Marcel::MimeType.extend "text/custom", extensions: %w( customtxt )
@@ -51,17 +52,20 @@ Marcel::MimeType.for name: "file.customtxt"
5152

5253
## Motivation
5354

54-
Marcel was extracted from Basecamp 3, in order to make our file detection logic both easily reusable but more importantly, easily testable. Test fixtures have been added for all of the most common file types uploaded to Basecamp, and other common file types too. We hope to expand this test coverage with other file types as and when problems are identified.
55+
Marcel was extracted from Basecamp's file detection heuristics. The aim is provide sensible, safe, "do what I expect" results for typical file handling. Test fixtures have been added for many common file types, including those typically encountered by Basecamp.
56+
5557

5658
## Contributing
5759

5860
Marcel generates MIME lookup tables with `bundle exec rake tables`. MIME types are seeded from data found in `data/*.xml`. Custom MIMEs may be added to `data/custom.xml`, while overrides to the standard MIME database may be added to `lib/marcel/mime_type/definitions.rb`.
5961

6062
Marcel follows the same contributing guidelines as [rails/rails](https://github.com/rails/rails#contributing).
6163

64+
6265
## Testing
6366

64-
The main test fixture files are split into two folders, those that can be recognised by magic numbers, and those that can only be recognised by name. Even though strictly unnecessary, the fixtures in both folders should all be valid files of the type they represent.
67+
The main test fixture files are split into two folders, those that can be recognised by magic bytes, and those that can only be recognised by name. Even though strictly unnecessary, the fixtures in both folders should all be valid files of the type they represent.
68+
6569

6670
## License
6771

0 commit comments

Comments
 (0)