marcel 1.0.4 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a51f606ee622330ffe46d2163caca9452ad4b9c704f6733628bb5c228bbdd15c
4
- data.tar.gz: f661a5a357dd426328906391a0997f1919d4f36da4120006bb3be0e4a32b87e2
3
+ metadata.gz: 4631da96baa71a625b60ec1f66ed05c4f6966f62340a0e6c522f67133dd39e48
4
+ data.tar.gz: 61c1b75b32db1aa7ab69716fb42f0eb7b12d05090d5e56738a29254311fa1a21
5
5
  SHA512:
6
- metadata.gz: 5e4cf80453f5f398a4012dafcd7c60f4c0badebf5850c64d318d3705c2de486ddb14015965f9d067f45afa5ddf4e22fd12d01d5069959b49114c5fe7c219278a
7
- data.tar.gz: e03fffed012927c64dcfff9cc37a61db4d66ff7f0eb39517d8eefe1c5a0089cdcc630dc7385fd7dd95a6760b7b5b855c0999f1182a63cae75e8b12f9aabc44d2
6
+ metadata.gz: f8e1a5b1fce489a8ea20dccbf88b2771bb9441f94631377155e30b8bb5be04738beb13688e0b007c75112283323cd2b577e398825b070195d0bf3f4ef770b140
7
+ data.tar.gz: cfa87feac79dfc6c1f462a85533295c51f837a7ba9423f461309e8b6c41690a2dc163bba8da60608ccea3a860e14c9d554e40a0edd94a5116cb9565b0d23d4e3
data/README.md CHANGED
@@ -1,8 +1,20 @@
1
1
  # Marcel
2
2
 
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
4
15
 
5
16
  ```ruby
17
+ # Magic bytes sniffing alone
6
18
  Marcel::MimeType.for Pathname.new("example.gif")
7
19
  # => "image/gif"
8
20
 
@@ -11,37 +23,26 @@ File.open "example.gif" do |file|
11
23
  end
12
24
  # => "image/gif"
13
25
 
26
+ # Magic bytes with filename fallback
14
27
  Marcel::MimeType.for Pathname.new("unrecognisable-data"), name: "example.pdf"
15
28
  # => "application/pdf"
16
29
 
30
+ # File extension alone
17
31
  Marcel::MimeType.for extension: ".pdf"
18
32
  # => "application/pdf"
19
33
 
34
+ # Magic bytes, declared type, and filename together
20
35
  Marcel::MimeType.for Pathname.new("unrecognisable-data"), name: "example", declared_type: "image/png"
21
36
  # => "image/png"
22
37
 
38
+ # Safe fallback to application/octet-stream
23
39
  Marcel::MimeType.for StringIO.new(File.read "unrecognisable-data")
24
40
  # => "application/octet-stream"
25
41
  ```
26
42
 
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
43
44
 
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`:
45
46
 
46
47
  ```ruby
47
48
  Marcel::MimeType.extend "text/custom", extensions: %w( customtxt )
@@ -51,17 +52,20 @@ Marcel::MimeType.for name: "file.customtxt"
51
52
 
52
53
  ## Motivation
53
54
 
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
+
55
57
 
56
58
  ## Contributing
57
59
 
58
- 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`.
60
+ Marcel generates MIME lookup tables with `bundle exec rake update`. 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`.
59
61
 
60
62
  Marcel follows the same contributing guidelines as [rails/rails](https://github.com/rails/rails#contributing).
61
63
 
64
+
62
65
  ## Testing
63
66
 
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
+
65
69
 
66
70
  ## License
67
71
 
data/lib/marcel/magic.rb CHANGED
@@ -117,10 +117,7 @@ module Marcel
117
117
  def self.magic_match(io, method)
118
118
  return magic_match(StringIO.new(io.to_s), method) unless io.respond_to?(:read)
119
119
 
120
- io.binmode if io.respond_to?(:binmode)
121
- io.set_encoding(Encoding::BINARY) if io.respond_to?(:set_encoding)
122
- buffer = "".encode(Encoding::BINARY)
123
-
120
+ buffer = "".b
124
121
  MAGIC.send(method) { |type, matches| magic_match_io(io, matches, buffer) }
125
122
  end
126
123
 
@@ -128,7 +125,9 @@ module Marcel
128
125
  matches.any? do |offset, value, children|
129
126
  match =
130
127
  if value
131
- if Range === offset
128
+ if value.is_a?(Regexp)
129
+ match_regex(io, offset, value, buffer)
130
+ elsif Range === offset
132
131
  io.read(offset.begin, buffer)
133
132
  x = io.read(offset.end - offset.begin + value.bytesize, buffer)
134
133
  x && x.include?(value)
@@ -143,6 +142,15 @@ module Marcel
143
142
  end
144
143
  end
145
144
 
146
- private_class_method :magic_match, :magic_match_io
145
+ def self.match_regex(io, offset, regexp, buffer)
146
+ start = offset.is_a?(Range) ? offset.begin : offset
147
+ io.read(start, buffer) if start > 0
148
+ data = io.read(256, buffer)
149
+ return false unless data
150
+
151
+ data.match?(regexp)
152
+ end
153
+
154
+ private_class_method :magic_match, :magic_match_io, :match_regex
147
155
  end
148
156
  end
@@ -2,6 +2,28 @@
2
2
 
3
3
  Marcel::MimeType.extend "text/plain", extensions: %w( txt asc )
4
4
 
5
+ Marcel::Magic.remove("text/html")
6
+ Marcel::MimeType.extend "text/html",
7
+ extensions: %w( html htm ),
8
+ magic: [
9
+ [0, "<!DOCTYPE html"],
10
+ [0, "<!DOCTYPE HTML"],
11
+ [0, "<!doctype html"],
12
+ [0, "<!doctype HTML"],
13
+ [0, "<html"],
14
+ [0, "<HTML"],
15
+ [0, " <!DOCTYPE html"],
16
+ [0, "\n<!DOCTYPE html"],
17
+ [0, "\r<!DOCTYPE html"],
18
+ [0, "\r\n<!DOCTYPE html"],
19
+ [0, "\t<!DOCTYPE html"],
20
+ [0, " <html"],
21
+ [0, "\n<html"],
22
+ [0, "\r<html"],
23
+ [0, "\r\n<html"],
24
+ [0, "\t<html"]
25
+ ]
26
+
5
27
  Marcel::MimeType.extend "application/illustrator", parents: "application/pdf"
6
28
  Marcel::MimeType.extend "image/vnd.adobe.photoshop", magic: [[0, "8BPS"]], extensions: %w( psd psb )
7
29
 
@@ -33,9 +55,11 @@ Marcel::MimeType.extend "application/vnd.apple.numbers", extensions: %w( numbers
33
55
  Marcel::MimeType.extend "application/vnd.apple.keynote", extensions: %w( key ), parents: "application/zip"
34
56
 
35
57
  Marcel::MimeType.extend "audio/aac", extensions: %w( aac ), parents: "audio/x-aac"
58
+ Marcel::MimeType.extend("audio/ogg", extensions: %w( opus ), magic: [[0, 'OggS', [[28, 'OpusHead']]]])
36
59
  Marcel::MimeType.extend("audio/ogg", extensions: %w( ogg oga ), magic: [[0, 'OggS', [[29, 'vorbis']]]])
37
60
 
38
61
  Marcel::MimeType.extend "image/vnd.dwg", magic: [[0, "AC10"]]
62
+ Marcel::MimeType.extend "application/pkcs8", magic: [[0, '-----BEGIN PRIVATE KEY-----']], extensions: %w( p8 )
39
63
 
40
64
  Marcel::MimeType.extend "application/x-x509-ca-cert", magic: [[0, '-----BEGIN CERTIFICATE-----']], extensions: %w( pem ), parents: "application/x-x509-cert;format=pem"
41
65
 
@@ -43,6 +67,9 @@ Marcel::MimeType.extend "image/avif", magic: [[4, "ftypavif"]], extensions: %w(
43
67
  Marcel::MimeType.extend "image/heif", magic: [[4, "ftypmif1"]], extensions: %w( heif )
44
68
  Marcel::MimeType.extend "image/heic", magic: [[4, "ftypheic"]], extensions: %w( heic )
45
69
 
70
+ Marcel::MimeType.extend "image/x-raw-sony", magic: [[0, "II*\000", [[0..4096, 'SONY']]], [0, "MM\000*", [[0..4096, 'SONY']]]], extensions: %w( arw ), parents: "image/tiff"
71
+ Marcel::MimeType.extend "image/x-raw-canon", extensions: %w( cr2 crw ), parents: "image/tiff"
72
+
46
73
  Marcel::MimeType.extend "video/mp4", magic: [[4, "ftypisom"], [4, "ftypM4V "]], extensions: %w( mp4 m4v )
47
74
 
48
75
  Marcel::MimeType.extend "audio/flac", magic: [[0, 'fLaC']], extensions: %w( flac ), parents: "audio/x-flac"