test_mimemagick 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ed9f3359a237ad1336a1460ff3d5e88278a2eafc3dd2b1a0322750f713935f23
4
+ data.tar.gz: c4444a355b2c158b35ec146805788af42517e06bd198dea561d5d0316b5e4fa6
5
+ SHA512:
6
+ metadata.gz: 8d3debe7e5dd0cc1cd7da2379fb4c6e4f503782232a366bde0d20322a992dc29a93479d82347cc784ca731db97fa499dab967f7716041b24cc2c56b5456b849d
7
+ data.tar.gz: 8d4e436b4f2e21efa8c9c83fd2f9e5aef422c8ed3e1ca922396afd9d4fe0d338a823dd2d0f7873efe6c1b4876ed6eab3ff87f741184e3b822231ea9454cb4924
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/CHANGELOG.md ADDED
@@ -0,0 +1,150 @@
1
+ # Changelog
2
+
3
+ ## Unreleased
4
+
5
+ ### Breaking Changes
6
+
7
+ ## 0.4.1
8
+
9
+ Remove `mimemagic/overlay` as it contains outdated, little used, data.
10
+
11
+ ## 0.3.7 (2021-03-25)
12
+
13
+ You will now need to ensure you have a copy of the fd.o shared MIME
14
+ types information available before installing this gem. More details
15
+ can be found in the readme.
16
+
17
+ ### Added
18
+
19
+ None
20
+
21
+ ### Fixed
22
+
23
+ None
24
+
25
+ ## 0.4.2
26
+
27
+ * Resolve issues parsing the version of freedesktop.org.xml shipped with
28
+ Ubuntu Trusty.
29
+
30
+ * Make Rake a runtime dependency.
31
+
32
+ * Fix the test suite.
33
+
34
+ * Relax the dependency on Nokogiri to something less specific in order
35
+ to avoid conflicting with other dependencies in people's applications.
36
+
37
+ ## 0.4.1
38
+
39
+
40
+ ## 0.4.0
41
+
42
+ Yanked release.
43
+
44
+ ## 0.3.9 (2021-03-25)
45
+
46
+ * Resolve issues parsing the version of freedesktop.org.xml shipped with
47
+ Ubuntu Trusty.
48
+
49
+ * Reintroduce overlays, since it seems at least some people were using
50
+ them.
51
+
52
+ * Make Rake a runtime dependency.
53
+
54
+ * Fix the test suite.
55
+ ## 0.3.8 (2021-03-25)
56
+
57
+ Relax the dependency on Nokogiri to something less specific in order
58
+ to avoid conflicting with other dependencies in people's applications.
59
+
60
+ ## 0.3.7 (2021-03-25)
61
+
62
+ Add a dependency on having a preinstalled version of the fd.o shared
63
+ MIME types info to resolve licensing concerns, and allow this gem to
64
+ remain MIT licensed.
65
+
66
+ See the readme for details on ensuring you have a copy of the database
67
+ available at install time.
68
+ ## 0.3.6 (2021-03-23)
69
+
70
+ Yanked release, relicensing to GPL due to licensing concerns.
71
+ ## 0.3.5 (2020-05-04)
72
+
73
+ Mimetype extensions are now ordered by freedesktop.org's priority
74
+
75
+ ## 0.3.4 (2020-01-28)
76
+
77
+ Added frozen string literal comments
78
+
79
+ ## 0.3.3 (2018-12-20)
80
+
81
+ Upgrade to shared-mime-info-1.10
82
+
83
+ ## 0.3.2 (2016-08-02)
84
+
85
+ ### Breaking Changes
86
+
87
+ None
88
+
89
+ ### Added
90
+
91
+ - [#37](https://github.com/minad/mimemagic/pull/37)
92
+ A convenient way to get all possible mime types by magic
93
+
94
+ ### Fixed
95
+
96
+ - [#40](https://github.com/minad/mimemagic/pull/40),
97
+ [#41](https://github.com/minad/mimemagic/pull/41)
98
+ Performance improvements
99
+ - [#38](https://github.com/minad/mimemagic/pull/38)
100
+ Updated to shared-mime-info 1.6
101
+
102
+ ## 0.3.1 (2016-01-04)
103
+
104
+ No release notes yet. Contributions welcome.
105
+
106
+ ## 0.3.0 (2015-03-25)
107
+
108
+ No release notes yet. Contributions welcome.
109
+
110
+ ## 0.2.1 (2013-07-29)
111
+
112
+ No release notes yet. Contributions welcome.
113
+
114
+ ## 0.2.0 (2012-10-19)
115
+
116
+ No release notes yet. Contributions welcome.
117
+
118
+ ## 0.1.9 (2012-09-20)
119
+
120
+ No release notes yet. Contributions welcome.
121
+
122
+ ## 0.1.8 (2009-05-08)
123
+
124
+ No release notes yet. Contributions welcome.
125
+
126
+ ## 0.1.7 (2009-05-08)
127
+
128
+ No release notes yet. Contributions welcome.
129
+
130
+ ## 0.1.5 (2009-05-08)
131
+
132
+ No release notes yet. Contributions welcome.
133
+
134
+ ## 0.1.4 (2009-05-08)
135
+
136
+ No release notes yet. Contributions welcome.
137
+
138
+ ## 0.1.3 (2009-05-08)
139
+
140
+ No release notes yet. Contributions welcome.
141
+
142
+ ## 0.1.2 (2009-05-08)
143
+
144
+ No release notes yet. Contributions welcome.
145
+
146
+ ## 0.1.1 (2009-05-08)
147
+
148
+ No release notes yet. Contributions welcome.
149
+
150
+
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at junshimo2@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org/'
2
+ gem 'rake'
3
+ #gem 'test_mimemagic'#, '~> 0.3.2'
4
+ gem 'rake-release'
5
+
6
+ #gemspec
7
+
data/Gemfile.lock ADDED
@@ -0,0 +1,16 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ rake (13.0.3)
5
+ rake-release (1.3.0)
6
+ bundler (>= 1.11, < 3)
7
+
8
+ PLATFORMS
9
+ x86_64-darwin-20
10
+
11
+ DEPENDENCIES
12
+ rake
13
+ rake-release
14
+
15
+ BUNDLED WITH
16
+ 2.2.15
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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 JironBach
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,17 @@
1
+ require 'rake/testtask'
2
+ require 'rake/release'
3
+
4
+ task :default => %w(test)
5
+
6
+ desc 'Run tests with minitest'
7
+ Rake::TestTask.new do |t|
8
+ t.libs << 'test'
9
+ t.pattern = 'test/*_test.rb'
10
+ end
11
+
12
+ desc 'Generate mime tables'
13
+ task :tables => 'lib/mimemagic/tables.rb'
14
+ file 'lib/mimemagic/tables.rb' => FileList['script/freedesktop.org.xml'] do |f|
15
+ sh "script/generate-mime.rb #{f.prerequisites.join(' ')} > #{f.name}"
16
+ end
17
+
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "test_mimemagick"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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.gsub(/^ {3}/, ""))
15
+ Could not find MIME type database in the following locations: #{possible_paths}
16
+
17
+ Ensure you have either installed the shared-mime-info 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
@@ -0,0 +1,155 @@
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
+ # This *one* pattern match, in the entirety of fd.o's mime types blows up the parser
30
+ # because of the escape character \c, so right here we have a hideous hack to
31
+ # accommodate that.
32
+ if value == '\chapter'
33
+ '\chapter'
34
+ else
35
+ value.gsub!(/\\(x[\dA-Fa-f]{1,2}|0\d{1,3}|\d{1,3}|.)/) {
36
+ eval("\"\\#{$1}\"")
37
+ }
38
+ end
39
+ when 'big16'
40
+ value = str2int(value)
41
+ value = ((value >> 8).chr + (value & 0xFF).chr)
42
+ when 'big32'
43
+ value = str2int(value)
44
+ value = (((value >> 24) & 0xFF).chr + ((value >> 16) & 0xFF).chr + ((value >> 8) & 0xFF).chr + (value & 0xFF).chr)
45
+ when 'little16'
46
+ value = str2int(value)
47
+ value = ((value & 0xFF).chr + (value >> 8).chr)
48
+ when 'little32'
49
+ value = str2int(value)
50
+ value = ((value & 0xFF).chr + ((value >> 8) & 0xFF).chr + ((value >> 16) & 0xFF).chr + ((value >> 24) & 0xFF).chr)
51
+ when 'host16' # use little endian
52
+ value = str2int(value)
53
+ value = ((value & 0xFF).chr + (value >> 8).chr)
54
+ when 'host32' # use little endian
55
+ value = str2int(value)
56
+ value = ((value & 0xFF).chr + ((value >> 8) & 0xFF).chr + ((value >> 16) & 0xFF).chr + ((value >> 24) & 0xFF).chr)
57
+ when 'byte'
58
+ value = str2int(value)
59
+ value = value.chr
60
+ end
61
+ children = get_matches(match)
62
+ children.empty? ? [offset, value] : [offset, value, children]
63
+ end
64
+ }.compact
65
+ end
66
+
67
+ def self.open_mime_database
68
+ path = MimeMagic::DATABASE_PATH
69
+ File.open(path)
70
+ end
71
+
72
+ def self.parse_database
73
+ file = open_mime_database
74
+
75
+ doc = Nokogiri::XML(file)
76
+ extensions = {}
77
+ types = {}
78
+ magics = []
79
+ (doc/'mime-info/mime-type').each do |mime|
80
+ comments = Hash[*(mime/'comment').map {|comment| [comment['xml:lang'], comment.inner_text] }.flatten]
81
+ type = mime['type']
82
+ subclass = (mime/'sub-class-of').map{|x| x['type']}
83
+ exts = (mime/'glob').map{|x| x['pattern'] =~ /^\*\.([^\[\]]+)$/ ? $1.downcase : nil }.compact
84
+ (mime/'magic').each do |magic|
85
+ priority = magic['priority'].to_i
86
+ matches = get_matches(magic)
87
+ magics << [priority, type, matches]
88
+ end
89
+ if !exts.empty?
90
+ exts.each{|x|
91
+ extensions[x] = type if !extensions.include?(x)
92
+ }
93
+ types[type] = [exts,subclass,comments[nil]]
94
+ end
95
+ end
96
+
97
+ magics = magics.sort {|a,b| [-a[0],a[1]] <=> [-b[0],b[1]] }
98
+
99
+ common_types = [
100
+ "image/jpeg", # .jpg
101
+ "image/png", # .png
102
+ "image/gif", # .gif
103
+ "image/tiff", # .tiff
104
+ "image/bmp", # .bmp
105
+ "image/vnd.adobe.photoshop", # .psd
106
+ "image/webp", # .webp
107
+ "image/svg+xml", # .svg
108
+
109
+ "video/x-msvideo", # .avi
110
+ "video/x-ms-wmv", # .wmv
111
+ "video/mp4", # .mp4, .m4v
112
+ "video/quicktime", # .mov
113
+ "video/mpeg", # .mpeg
114
+ "video/ogg", # .ogv
115
+ "video/webm", # .webm
116
+ "video/x-matroska", # .mkv
117
+ "video/x-flv", # .flv
118
+
119
+ "audio/mpeg", # .mp3
120
+ "audio/x-wav", # .wav
121
+ "audio/aac", # .aac
122
+ "audio/flac", # .flac
123
+ "audio/mp4", # .m4a
124
+ "audio/ogg", # .ogg
125
+
126
+ "application/pdf", # .pdf
127
+ "application/msword", # .doc
128
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document", # .docx
129
+ "application/vnd.ms-powerpoint", # .pps
130
+ "application/vnd.openxmlformats-officedocument.presentationml.slideshow", # .ppsx
131
+ "application/vnd.ms-excel", # .pps
132
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", # .ppsx
133
+ ]
134
+
135
+ common_magics = common_types.map do |common_type|
136
+ magics.find { |_, type, _| type == common_type }
137
+ end
138
+
139
+ magics = (common_magics.compact + magics).uniq
140
+
141
+ extensions.keys.sort.each do |key|
142
+ EXTENSIONS[key] = extensions[key]
143
+ end
144
+ types.keys.sort.each do |key|
145
+ exts = types[key][0]
146
+ parents = types[key][1].sort
147
+ comment = types[key][2]
148
+
149
+ TYPES[key] = [exts, parents, comment]
150
+ end
151
+ magics.each do |priority, type, matches|
152
+ MAGIC << [type, matches]
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,5 @@
1
+ class MimeMagic
2
+ # MimeMagic version string
3
+ # @api public
4
+ VERSION = '0.4.2'
5
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "test_mimemagick/version"
4
+
5
+ module TestMimemagick
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TestMimemagick
4
+ VERSION = "0.3.2"
5
+ end
@@ -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')
23
+ s.add_dependency('rake')
24
+
25
+ s.add_development_dependency('minitest', '~> 5.14')
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
Binary file
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/test_mimemagick/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "test_mimemagick"
7
+ spec.version = TestMimemagick::VERSION
8
+ spec.authors = ["JironBach"]
9
+ spec.email = ["junshimo2@gmail.com"]
10
+
11
+ spec.summary = "downgrade mimemagick"
12
+ spec.description = "downgrade mimemagick"
13
+ spec.homepage = "https://ameblo.jp/hardcastle"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/JironBach/test_mimemagick"
21
+ spec.metadata["changelog_uri"] = "https://github.com/JironBach/test_mimemagick/edit/master/README.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+
35
+ # For more information and examples about making a new gem, checkout our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: test_mimemagick
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.2
5
+ platform: ruby
6
+ authors:
7
+ - JironBach
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-03-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: downgrade mimemagick
14
+ email:
15
+ - junshimo2@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - CHANGELOG.md
22
+ - CODE_OF_CONDUCT.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - ext/mimemagic/Rakefile
32
+ - lib/mimemagic.rb
33
+ - lib/mimemagic/tables.rb
34
+ - lib/mimemagic/version.rb
35
+ - lib/test_mimemagick.rb
36
+ - lib/test_mimemagick/version.rb
37
+ - mimemagic.gemspec.bk1
38
+ - test_mimemagick-0.1.0.gem
39
+ - test_mimemagick.gemspec
40
+ homepage: https://ameblo.jp/hardcastle
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ allowed_push_host: https://rubygems.org
45
+ homepage_uri: https://ameblo.jp/hardcastle
46
+ source_code_uri: https://github.com/JironBach/test_mimemagick
47
+ changelog_uri: https://github.com/JironBach/test_mimemagick/edit/master/README.md
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.4.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.1.4
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: downgrade mimemagick
67
+ test_files: []