mimemagic 0.3.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.travis.yml +21 -0
- data/.yardopts +2 -0
- data/CHANGELOG.md +111 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +65 -0
- data/Rakefile +16 -0
- data/ext/mimemagic/Rakefile +33 -0
- data/lib/mimemagic.rb +142 -0
- data/lib/mimemagic/overlay.rb +0 -0
- data/lib/mimemagic/tables.rb +146 -0
- data/lib/mimemagic/version.rb +5 -0
- data/mimemagic.gemspec +32 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f6da483b1c1d4378db22986ec169a3ec569d138a8ab29f5daeb065af66d39fbd
|
4
|
+
data.tar.gz: e987b92b51834ade1b3529b6cb7761d444d6e7089c070e2324ce97e9a22d135c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 250747be3d00f5deb7d0fef8cb6682b8f7589c26b640192aa4f53d6ee4025119063a59d64e2eea20aafb31e4abc231042ce3d1bca35555f8b6e94be220205aa8
|
7
|
+
data.tar.gz: 24a9b36f0ff3c788ec4f62e97e9526ec93b4b71625e3f722bc0b1d234fa879ee4ce7287913618471d8c8dedf8693eebfa1081afbd3933233ff5a9a7214018dd7
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
language: ruby
|
2
|
+
jobs:
|
3
|
+
include:
|
4
|
+
- rvm: 1.9.3
|
5
|
+
- rvm: 2.0.0
|
6
|
+
- rvm: 2.1
|
7
|
+
- rvm: 2.2
|
8
|
+
- rvm: 2.3
|
9
|
+
- rvm: 2.4
|
10
|
+
env: RUBYOPT="--enable-frozen-string-literal"
|
11
|
+
- rvm: 2.5
|
12
|
+
env: RUBYOPT="--enable-frozen-string-literal"
|
13
|
+
- rvm: ruby-head
|
14
|
+
env: RUBYOPT="--enable-frozen-string-literal"
|
15
|
+
before_install:
|
16
|
+
# 1. The pre-installed Bundler version on Travis is very old; causes 1.9.3 build issues
|
17
|
+
# 2. Bundler 2.0 is not supported by the whole matrix
|
18
|
+
- gem install bundler -v'< 2'
|
19
|
+
|
20
|
+
script:
|
21
|
+
- bundle exec rake
|
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## Unreleased
|
4
|
+
|
5
|
+
### Breaking Changes
|
6
|
+
|
7
|
+
## 0.3.7 (2021-03-25)
|
8
|
+
|
9
|
+
You will now need to ensure you have a copy of the fd.o shared MIME
|
10
|
+
types information available before installing this gem. More details
|
11
|
+
can be found in the readme.
|
12
|
+
|
13
|
+
### Added
|
14
|
+
|
15
|
+
None
|
16
|
+
|
17
|
+
### Fixed
|
18
|
+
|
19
|
+
None
|
20
|
+
|
21
|
+
## 0.3.7 (2021-03-25)
|
22
|
+
|
23
|
+
Add a dependency on having a preinstalled version of the fd.o shared
|
24
|
+
MIME types info to resolve licensing concerns, and allow this gem to
|
25
|
+
remain MIT licensed.
|
26
|
+
|
27
|
+
See the readme for details on ensuring you have a copy of the database
|
28
|
+
available at install time.
|
29
|
+
## 0.3.6 (2021-03-23)
|
30
|
+
|
31
|
+
Yanked release, relicensing to GPL due to licensing concerns.
|
32
|
+
## 0.3.5 (2020-05-04)
|
33
|
+
|
34
|
+
Mimetype extensions are now ordered by freedesktop.org's priority
|
35
|
+
|
36
|
+
## 0.3.4 (2020-01-28)
|
37
|
+
|
38
|
+
Added frozen string literal comments
|
39
|
+
|
40
|
+
## 0.3.3 (2018-12-20)
|
41
|
+
|
42
|
+
Upgrade to shared-mime-info-1.10
|
43
|
+
|
44
|
+
## 0.3.2 (2016-08-02)
|
45
|
+
|
46
|
+
### Breaking Changes
|
47
|
+
|
48
|
+
None
|
49
|
+
|
50
|
+
### Added
|
51
|
+
|
52
|
+
- [#37](https://github.com/minad/mimemagic/pull/37)
|
53
|
+
A convenient way to get all possible mime types by magic
|
54
|
+
|
55
|
+
### Fixed
|
56
|
+
|
57
|
+
- [#40](https://github.com/minad/mimemagic/pull/40),
|
58
|
+
[#41](https://github.com/minad/mimemagic/pull/41)
|
59
|
+
Performance improvements
|
60
|
+
- [#38](https://github.com/minad/mimemagic/pull/38)
|
61
|
+
Updated to shared-mime-info 1.6
|
62
|
+
|
63
|
+
## 0.3.1 (2016-01-04)
|
64
|
+
|
65
|
+
No release notes yet. Contributions welcome.
|
66
|
+
|
67
|
+
## 0.3.0 (2015-03-25)
|
68
|
+
|
69
|
+
No release notes yet. Contributions welcome.
|
70
|
+
|
71
|
+
## 0.2.1 (2013-07-29)
|
72
|
+
|
73
|
+
No release notes yet. Contributions welcome.
|
74
|
+
|
75
|
+
## 0.2.0 (2012-10-19)
|
76
|
+
|
77
|
+
No release notes yet. Contributions welcome.
|
78
|
+
|
79
|
+
## 0.1.9 (2012-09-20)
|
80
|
+
|
81
|
+
No release notes yet. Contributions welcome.
|
82
|
+
|
83
|
+
## 0.1.8 (2009-05-08)
|
84
|
+
|
85
|
+
No release notes yet. Contributions welcome.
|
86
|
+
|
87
|
+
## 0.1.7 (2009-05-08)
|
88
|
+
|
89
|
+
No release notes yet. Contributions welcome.
|
90
|
+
|
91
|
+
## 0.1.5 (2009-05-08)
|
92
|
+
|
93
|
+
No release notes yet. Contributions welcome.
|
94
|
+
|
95
|
+
## 0.1.4 (2009-05-08)
|
96
|
+
|
97
|
+
No release notes yet. Contributions welcome.
|
98
|
+
|
99
|
+
## 0.1.3 (2009-05-08)
|
100
|
+
|
101
|
+
No release notes yet. Contributions welcome.
|
102
|
+
|
103
|
+
## 0.1.2 (2009-05-08)
|
104
|
+
|
105
|
+
No release notes yet. Contributions welcome.
|
106
|
+
|
107
|
+
## 0.1.1 (2009-05-08)
|
108
|
+
|
109
|
+
No release notes yet. Contributions welcome.
|
110
|
+
|
111
|
+
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2011 Daniel Mendler
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
MimeMagic is a library to detect the mime type of a file by extension or by content. It uses the mime database
|
2
|
+
provided by freedesktop.org (see http://freedesktop.org/wiki/Software/shared-mime-info/).
|
3
|
+
|
4
|
+
[![Gem Version](https://img.shields.io/gem/v/mimemagic.svg)](http://rubygems.org/gems/mimemagic)
|
5
|
+
|
6
|
+
Dependencies
|
7
|
+
============
|
8
|
+
|
9
|
+
You will require a copy of the Freedesktop.org shared-mime-info database to be available. If you're on Linux,
|
10
|
+
it's probably available via your package manager, and will probably be in the location it's being looked for
|
11
|
+
when the gem is installed.
|
12
|
+
|
13
|
+
macOS users can install the database via Homebrew with `brew install shared-mime-info`.
|
14
|
+
|
15
|
+
Should you be unable to use a package manager you can obtain a copy of the needed file by extracting it from
|
16
|
+
the Debian package. This process will also work on a Windows machine.
|
17
|
+
|
18
|
+
1. Download the package from https://packages.debian.org/sid/amd64/shared-mime-info/download
|
19
|
+
2. Ensure the command line version of 7-Zip is installed
|
20
|
+
3. `7z x -so shared-mime-info_2.0-1_amd64.deb data.tar | 7z e -sidata.tar "./usr/share/mime/packages/freedesktop.org.xml"`
|
21
|
+
|
22
|
+
|
23
|
+
Place the file `freedesktop.org.xml` in an appropriate location, and then set the environment variable
|
24
|
+
`FREEDESKTOP_MIME_TYPES_PATH` to that path. Once that has been done the gem should install successfully. Please
|
25
|
+
note that the gem will depend upon the file remaining in that location at run time.
|
26
|
+
|
27
|
+
Usage
|
28
|
+
=====
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require 'mimemagic'
|
32
|
+
MimeMagic.by_extension('html').text?
|
33
|
+
MimeMagic.by_extension('.html').child_of? 'text/plain'
|
34
|
+
MimeMagic.by_path('filename.txt')
|
35
|
+
MimeMagic.by_magic(File.open('test.html'))
|
36
|
+
# etc...
|
37
|
+
```
|
38
|
+
|
39
|
+
You can add your own magic with `MimeMagic.add`.
|
40
|
+
|
41
|
+
API
|
42
|
+
===
|
43
|
+
|
44
|
+
http://www.rubydoc.info/github/mimemagicrb/mimemagic
|
45
|
+
|
46
|
+
Tests
|
47
|
+
=====
|
48
|
+
|
49
|
+
```
|
50
|
+
bundle install
|
51
|
+
|
52
|
+
rake test
|
53
|
+
```
|
54
|
+
|
55
|
+
Authors
|
56
|
+
=======
|
57
|
+
|
58
|
+
* Daniel Mendler
|
59
|
+
* Jon Wood
|
60
|
+
* [MimeMagic Contributors](https://github.com/mimemagicrb/mimemagic/graphs/contributors)
|
61
|
+
|
62
|
+
LICENSE
|
63
|
+
=======
|
64
|
+
|
65
|
+
MIT
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
task :default => %w(test)
|
4
|
+
|
5
|
+
desc 'Run tests with minitest'
|
6
|
+
Rake::TestTask.new do |t|
|
7
|
+
t.libs << 'test'
|
8
|
+
t.pattern = 'test/*_test.rb'
|
9
|
+
end
|
10
|
+
|
11
|
+
desc 'Generate mime tables'
|
12
|
+
task :tables => 'lib/mimemagic/tables.rb'
|
13
|
+
file 'lib/mimemagic/tables.rb' => FileList['script/freedesktop.org.xml'] do |f|
|
14
|
+
sh "script/generate-mime.rb #{f.prerequisites.join(' ')} > #{f.name}"
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "rake/clean"
|
3
|
+
|
4
|
+
def locate_mime_database
|
5
|
+
possible_paths = [
|
6
|
+
(File.expand_path(ENV["FREEDESKTOP_MIME_TYPES_PATH"]) if ENV["FREEDESKTOP_MIME_TYPES_PATH"]),
|
7
|
+
"/usr/local/share/mime/packages/freedesktop.org.xml",
|
8
|
+
"/opt/homebrew/share/mime/packages/freedesktop.org.xml",
|
9
|
+
"/usr/share/mime/packages/freedesktop.org.xml"
|
10
|
+
].compact
|
11
|
+
path = possible_paths.find { |candidate| File.exist?(candidate) }
|
12
|
+
|
13
|
+
return path unless path.nil?
|
14
|
+
raise(<<~ERROR)
|
15
|
+
Could not find MIME type database in the following locations: #{possible_paths}
|
16
|
+
|
17
|
+
Ensure you have either installed the shared-mime-types package for your distribution, or
|
18
|
+
obtain a version of freedesktop.org.xml and set FREEDESKTOP_MIME_TYPES_PATH to the location
|
19
|
+
of that file.
|
20
|
+
ERROR
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "Build a file pointing at the database"
|
24
|
+
task :default do
|
25
|
+
mime_database_path = locate_mime_database
|
26
|
+
open("../../lib/mimemagic/path.rb", "w") do |f|
|
27
|
+
f.print(%Q{
|
28
|
+
class MimeMagic
|
29
|
+
DATABASE_PATH="#{mime_database_path}"
|
30
|
+
end
|
31
|
+
})
|
32
|
+
end
|
33
|
+
end
|
data/lib/mimemagic.rb
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'mimemagic/tables'
|
4
|
+
require 'mimemagic/version'
|
5
|
+
|
6
|
+
require 'stringio'
|
7
|
+
|
8
|
+
MimeMagic.parse_database
|
9
|
+
|
10
|
+
# Mime type detection
|
11
|
+
class MimeMagic
|
12
|
+
attr_reader :type, :mediatype, :subtype
|
13
|
+
|
14
|
+
# Mime type by type string
|
15
|
+
def initialize(type)
|
16
|
+
@type = type
|
17
|
+
@mediatype, @subtype = type.split('/', 2)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Add custom mime type. Arguments:
|
21
|
+
# * <i>type</i>: Mime type
|
22
|
+
# * <i>options</i>: Options hash
|
23
|
+
#
|
24
|
+
# Option keys:
|
25
|
+
# * <i>:extensions</i>: String list or single string of file extensions
|
26
|
+
# * <i>:parents</i>: String list or single string of parent mime types
|
27
|
+
# * <i>:magic</i>: Mime magic specification
|
28
|
+
# * <i>:comment</i>: Comment string
|
29
|
+
def self.add(type, options)
|
30
|
+
extensions = [options[:extensions]].flatten.compact
|
31
|
+
TYPES[type] = [extensions,
|
32
|
+
[options[:parents]].flatten.compact,
|
33
|
+
options[:comment]]
|
34
|
+
extensions.each {|ext| EXTENSIONS[ext] = type }
|
35
|
+
MAGIC.unshift [type, options[:magic]] if options[:magic]
|
36
|
+
end
|
37
|
+
|
38
|
+
# Removes a mime type from the dictionary. You might want to do this if
|
39
|
+
# you're seeing impossible conflicts (for instance, application/x-gmc-link).
|
40
|
+
# * <i>type</i>: The mime type to remove. All associated extensions and magic are removed too.
|
41
|
+
def self.remove(type)
|
42
|
+
EXTENSIONS.delete_if {|ext, t| t == type }
|
43
|
+
MAGIC.delete_if {|t, m| t == type }
|
44
|
+
TYPES.delete(type)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Returns true if type is a text format
|
48
|
+
def text?; mediatype == 'text' || child_of?('text/plain'); end
|
49
|
+
|
50
|
+
# Mediatype shortcuts
|
51
|
+
def image?; mediatype == 'image'; end
|
52
|
+
def audio?; mediatype == 'audio'; end
|
53
|
+
def video?; mediatype == 'video'; end
|
54
|
+
|
55
|
+
# Returns true if type is child of parent type
|
56
|
+
def child_of?(parent)
|
57
|
+
MimeMagic.child?(type, parent)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Get string list of file extensions
|
61
|
+
def extensions
|
62
|
+
TYPES.key?(type) ? TYPES[type][0] : []
|
63
|
+
end
|
64
|
+
|
65
|
+
# Get mime comment
|
66
|
+
def comment
|
67
|
+
(TYPES.key?(type) ? TYPES[type][2] : nil).to_s
|
68
|
+
end
|
69
|
+
|
70
|
+
# Lookup mime type by file extension
|
71
|
+
def self.by_extension(ext)
|
72
|
+
ext = ext.to_s.downcase
|
73
|
+
mime = ext[0..0] == '.' ? EXTENSIONS[ext[1..-1]] : EXTENSIONS[ext]
|
74
|
+
mime && new(mime)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Lookup mime type by filename
|
78
|
+
def self.by_path(path)
|
79
|
+
by_extension(File.extname(path))
|
80
|
+
end
|
81
|
+
|
82
|
+
# Lookup mime type by magic content analysis.
|
83
|
+
# This is a slow operation.
|
84
|
+
def self.by_magic(io)
|
85
|
+
mime = magic_match(io, :find)
|
86
|
+
mime && new(mime[0])
|
87
|
+
end
|
88
|
+
|
89
|
+
# Lookup all mime types by magic content analysis.
|
90
|
+
# This is a slower operation.
|
91
|
+
def self.all_by_magic(io)
|
92
|
+
magic_match(io, :select).map { |mime| new(mime[0]) }
|
93
|
+
end
|
94
|
+
|
95
|
+
# Return type as string
|
96
|
+
def to_s
|
97
|
+
type
|
98
|
+
end
|
99
|
+
|
100
|
+
# Allow comparison with string
|
101
|
+
def eql?(other)
|
102
|
+
type == other.to_s
|
103
|
+
end
|
104
|
+
|
105
|
+
def hash
|
106
|
+
type.hash
|
107
|
+
end
|
108
|
+
|
109
|
+
alias == eql?
|
110
|
+
|
111
|
+
def self.child?(child, parent)
|
112
|
+
child == parent || TYPES.key?(child) && TYPES[child][1].any? {|p| child?(p, parent) }
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.magic_match(io, method)
|
116
|
+
return magic_match(StringIO.new(io.to_s), method) unless io.respond_to?(:read)
|
117
|
+
|
118
|
+
io.binmode if io.respond_to?(:binmode)
|
119
|
+
io.set_encoding(Encoding::BINARY) if io.respond_to?(:set_encoding)
|
120
|
+
buffer = "".encode(Encoding::BINARY)
|
121
|
+
|
122
|
+
MAGIC.send(method) { |type, matches| magic_match_io(io, matches, buffer) }
|
123
|
+
end
|
124
|
+
|
125
|
+
def self.magic_match_io(io, matches, buffer)
|
126
|
+
matches.any? do |offset, value, children|
|
127
|
+
match =
|
128
|
+
if Range === offset
|
129
|
+
io.read(offset.begin, buffer)
|
130
|
+
x = io.read(offset.end - offset.begin + value.bytesize, buffer)
|
131
|
+
x && x.include?(value)
|
132
|
+
else
|
133
|
+
io.read(offset, buffer)
|
134
|
+
io.read(value.bytesize, buffer) == value
|
135
|
+
end
|
136
|
+
io.rewind
|
137
|
+
match && (!children || magic_match_io(io, children, buffer))
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
private_class_method :magic_match, :magic_match_io
|
142
|
+
end
|
File without changes
|
@@ -0,0 +1,146 @@
|
|
1
|
+
# -*- coding: binary -*-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
# Generated from script/freedesktop.org.xml
|
4
|
+
require 'nokogiri'
|
5
|
+
require 'mimemagic/path'
|
6
|
+
|
7
|
+
class MimeMagic
|
8
|
+
EXTENSIONS = {}
|
9
|
+
TYPES = {}
|
10
|
+
MAGIC = []
|
11
|
+
|
12
|
+
def self.str2int(s)
|
13
|
+
return s.to_i(16) if s[0..1].downcase == '0x'
|
14
|
+
return s.to_i(8) if s[0..0].downcase == '0'
|
15
|
+
s.to_i(10)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.get_matches(parent)
|
19
|
+
parent.elements.map {|match|
|
20
|
+
if match['mask']
|
21
|
+
nil
|
22
|
+
else
|
23
|
+
type = match['type']
|
24
|
+
value = match['value']
|
25
|
+
offset = match['offset'].split(':').map {|x| x.to_i }
|
26
|
+
offset = offset.size == 2 ? offset[0]..offset[1] : offset[0]
|
27
|
+
case type
|
28
|
+
when 'string'
|
29
|
+
value.gsub!(/\\(x[\dA-Fa-f]{1,2}|0\d{1,3}|\d{1,3}|.)/) { eval("\"\\#{$1}\"") }
|
30
|
+
when 'big16'
|
31
|
+
value = str2int(value)
|
32
|
+
value = ((value >> 8).chr + (value & 0xFF).chr)
|
33
|
+
when 'big32'
|
34
|
+
value = str2int(value)
|
35
|
+
value = (((value >> 24) & 0xFF).chr + ((value >> 16) & 0xFF).chr + ((value >> 8) & 0xFF).chr + (value & 0xFF).chr)
|
36
|
+
when 'little16'
|
37
|
+
value = str2int(value)
|
38
|
+
value = ((value & 0xFF).chr + (value >> 8).chr)
|
39
|
+
when 'little32'
|
40
|
+
value = str2int(value)
|
41
|
+
value = ((value & 0xFF).chr + ((value >> 8) & 0xFF).chr + ((value >> 16) & 0xFF).chr + ((value >> 24) & 0xFF).chr)
|
42
|
+
when 'host16' # use little endian
|
43
|
+
value = str2int(value)
|
44
|
+
value = ((value & 0xFF).chr + (value >> 8).chr)
|
45
|
+
when 'host32' # use little endian
|
46
|
+
value = str2int(value)
|
47
|
+
value = ((value & 0xFF).chr + ((value >> 8) & 0xFF).chr + ((value >> 16) & 0xFF).chr + ((value >> 24) & 0xFF).chr)
|
48
|
+
when 'byte'
|
49
|
+
value = str2int(value)
|
50
|
+
value = value.chr
|
51
|
+
end
|
52
|
+
children = get_matches(match)
|
53
|
+
children.empty? ? [offset, value] : [offset, value, children]
|
54
|
+
end
|
55
|
+
}.compact
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.open_mime_database
|
59
|
+
path = MimeMagic::DATABASE_PATH
|
60
|
+
File.open(path)
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.parse_database
|
64
|
+
file = open_mime_database
|
65
|
+
|
66
|
+
doc = Nokogiri::XML(file)
|
67
|
+
extensions = {}
|
68
|
+
types = {}
|
69
|
+
magics = []
|
70
|
+
(doc/'mime-info/mime-type').each do |mime|
|
71
|
+
comments = Hash[*(mime/'comment').map {|comment| [comment['xml:lang'], comment.inner_text] }.flatten]
|
72
|
+
type = mime['type']
|
73
|
+
subclass = (mime/'sub-class-of').map{|x| x['type']}
|
74
|
+
exts = (mime/'glob').map{|x| x['pattern'] =~ /^\*\.([^\[\]]+)$/ ? $1.downcase : nil }.compact
|
75
|
+
(mime/'magic').each do |magic|
|
76
|
+
priority = magic['priority'].to_i
|
77
|
+
matches = get_matches(magic)
|
78
|
+
magics << [priority, type, matches]
|
79
|
+
end
|
80
|
+
if !exts.empty?
|
81
|
+
exts.each{|x|
|
82
|
+
extensions[x] = type if !extensions.include?(x)
|
83
|
+
}
|
84
|
+
types[type] = [exts,subclass,comments[nil]]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
magics = magics.sort {|a,b| [-a[0],a[1]] <=> [-b[0],b[1]] }
|
89
|
+
|
90
|
+
common_types = [
|
91
|
+
"image/jpeg", # .jpg
|
92
|
+
"image/png", # .png
|
93
|
+
"image/gif", # .gif
|
94
|
+
"image/tiff", # .tiff
|
95
|
+
"image/bmp", # .bmp
|
96
|
+
"image/vnd.adobe.photoshop", # .psd
|
97
|
+
"image/webp", # .webp
|
98
|
+
"image/svg+xml", # .svg
|
99
|
+
|
100
|
+
"video/x-msvideo", # .avi
|
101
|
+
"video/x-ms-wmv", # .wmv
|
102
|
+
"video/mp4", # .mp4, .m4v
|
103
|
+
"video/quicktime", # .mov
|
104
|
+
"video/mpeg", # .mpeg
|
105
|
+
"video/ogg", # .ogv
|
106
|
+
"video/webm", # .webm
|
107
|
+
"video/x-matroska", # .mkv
|
108
|
+
"video/x-flv", # .flv
|
109
|
+
|
110
|
+
"audio/mpeg", # .mp3
|
111
|
+
"audio/x-wav", # .wav
|
112
|
+
"audio/aac", # .aac
|
113
|
+
"audio/flac", # .flac
|
114
|
+
"audio/mp4", # .m4a
|
115
|
+
"audio/ogg", # .ogg
|
116
|
+
|
117
|
+
"application/pdf", # .pdf
|
118
|
+
"application/msword", # .doc
|
119
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", # .docx
|
120
|
+
"application/vnd.ms-powerpoint", # .pps
|
121
|
+
"application/vnd.openxmlformats-officedocument.presentationml.slideshow", # .ppsx
|
122
|
+
"application/vnd.ms-excel", # .pps
|
123
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", # .ppsx
|
124
|
+
]
|
125
|
+
|
126
|
+
common_magics = common_types.map do |common_type|
|
127
|
+
magics.find { |_, type, _| type == common_type }
|
128
|
+
end
|
129
|
+
|
130
|
+
magics = (common_magics.compact + magics).uniq
|
131
|
+
|
132
|
+
extensions.keys.sort.each do |key|
|
133
|
+
EXTENSIONS[key] = extensions[key]
|
134
|
+
end
|
135
|
+
types.keys.sort.each do |key|
|
136
|
+
exts = types[key][0]
|
137
|
+
parents = types[key][1].sort
|
138
|
+
comment = types[key][2]
|
139
|
+
|
140
|
+
TYPES[key] = [exts, parents, comment]
|
141
|
+
end
|
142
|
+
magics.each do |priority, type, matches|
|
143
|
+
MAGIC << [type, matches]
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
data/mimemagic.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.dirname(__FILE__) + '/lib/mimemagic/version'
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'mimemagic'
|
7
|
+
s.version = MimeMagic::VERSION
|
8
|
+
|
9
|
+
s.authors = ['Daniel Mendler', 'Jon Wood']
|
10
|
+
s.date = Date.today.to_s
|
11
|
+
s.email = ['mail@daniel-mendler.de', 'jon@blankpad.net']
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n").reject { |f| f.match(%r{^(test|script)/}) }
|
14
|
+
s.require_paths = %w(lib)
|
15
|
+
s.extensions = %w(ext/mimemagic/Rakefile)
|
16
|
+
|
17
|
+
s.summary = 'Fast mime detection by extension or content'
|
18
|
+
s.description = 'Fast mime detection by extension or content in pure ruby (Uses freedesktop.org.xml shared-mime-info database)'
|
19
|
+
s.homepage = 'https://github.com/mimemagicrb/mimemagic'
|
20
|
+
s.license = 'MIT'
|
21
|
+
|
22
|
+
s.add_dependency('nokogiri', '~> 1.11.2')
|
23
|
+
|
24
|
+
s.add_development_dependency('minitest', '~> 5.14')
|
25
|
+
s.add_development_dependency('rake', '~> 13.0')
|
26
|
+
|
27
|
+
if s.respond_to?(:metadata)
|
28
|
+
s.metadata['changelog_uri'] = "https://github.com/mimemagicrb/mimemagic/blob/master/CHANGELOG.md"
|
29
|
+
s.metadata['source_code_uri'] = "https://github.com/mimemagicrb/mimemagic"
|
30
|
+
s.metadata['bug_tracker_uri'] = "https://github.com/mimemagicrb/mimemagic/issues"
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mimemagic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.7
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Mendler
|
8
|
+
- Jon Wood
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2021-03-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nokogiri
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 1.11.2
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 1.11.2
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: minitest
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '5.14'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '5.14'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '13.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '13.0'
|
56
|
+
description: Fast mime detection by extension or content in pure ruby (Uses freedesktop.org.xml
|
57
|
+
shared-mime-info database)
|
58
|
+
email:
|
59
|
+
- mail@daniel-mendler.de
|
60
|
+
- jon@blankpad.net
|
61
|
+
executables: []
|
62
|
+
extensions:
|
63
|
+
- ext/mimemagic/Rakefile
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- ".gitignore"
|
67
|
+
- ".travis.yml"
|
68
|
+
- ".yardopts"
|
69
|
+
- CHANGELOG.md
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- ext/mimemagic/Rakefile
|
75
|
+
- lib/mimemagic.rb
|
76
|
+
- lib/mimemagic/overlay.rb
|
77
|
+
- lib/mimemagic/tables.rb
|
78
|
+
- lib/mimemagic/version.rb
|
79
|
+
- mimemagic.gemspec
|
80
|
+
homepage: https://github.com/mimemagicrb/mimemagic
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata:
|
84
|
+
changelog_uri: https://github.com/mimemagicrb/mimemagic/blob/master/CHANGELOG.md
|
85
|
+
source_code_uri: https://github.com/mimemagicrb/mimemagic
|
86
|
+
bug_tracker_uri: https://github.com/mimemagicrb/mimemagic/issues
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubygems_version: 3.2.3
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: Fast mime detection by extension or content
|
106
|
+
test_files: []
|