mime_type 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Topspin Media Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ What is MimeType
2
+ ================
3
+
4
+ A Ruby library to identify the MIME type of a local file
5
+
6
+ Why MimeType was born
7
+ =====================
8
+
9
+ At [Topspin](http://topspinmedia.com) we provide [ArtistLink](http://artistlink.com), a platform for musician to upload and share their songs and videos.
10
+ Artistlink provides a form to upload media files, and we need a way to present them differently if they are images, videos, sounds or other files.
11
+
12
+ How to use from the command line (executable)
13
+ =============================================
14
+
15
+ Type `mime_type_of` followed by the path of a local file.
16
+ This is will show the MIME type of that file.
17
+
18
+ How to use in other Ruby programs
19
+ =================================
20
+
21
+ Install by running `gem install mime_type` or adding `mime_type` in the Gemfile of your bundled project.
22
+
23
+ Call `MimeType::of(file)` to know the MIME type of `file`.
24
+ `file` can be any derivation of the `File` object such that `file.path` returns its local path.
25
+
26
+ How to contribute
27
+ =================
28
+
29
+ Make sure tests pass, then either submit a Pull Request.
30
+ A list of [nice TODOs](http://github.com/topspin/mime_type/tree/master/TODO.md) is provided.
31
+ You can also build a new version of the gem and move it to your gem repository.
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+ begin
3
+ require 'mime_type'
4
+ rescue LoadError
5
+ require 'rubygems'
6
+ require 'mime_type'
7
+ end
8
+
9
+ require 'optparse'
10
+ class MimeTypeParser
11
+ def self.parse(args)
12
+ options = {}
13
+ options[:output] = :html
14
+
15
+ opt_parser = OptionParser.new do |opts|
16
+ opts.banner = "Usage: #{File.basename($0)} [file_path]"
17
+
18
+ opts.separator ''
19
+ opts.separator 'Specific options:'
20
+
21
+ opts.on("-m", "--[no-]magic", "Use File magic number") do |n|
22
+ options[:by_file_magic] = n
23
+ end
24
+
25
+ opts.on("-t", "--[no-]file-type", "Use File type") do |n|
26
+ options[:by_file_type] = n
27
+ end
28
+
29
+ opts.separator ''
30
+ opts.separator 'Common options:'
31
+
32
+ opts.on_tail('-h', '--help', 'Show this message') do
33
+ puts opts
34
+ exit
35
+ end
36
+
37
+ opts.on_tail('--version', 'Show version') do
38
+ puts MimeType::VERSION
39
+ exit
40
+ end
41
+ end
42
+
43
+ opt_parser.parse!(args)
44
+ options
45
+ end
46
+ end
47
+
48
+ if ARGV.empty?
49
+ abort "Please specify an file, e.g. `#{File.basename($0)} example.jpg'"
50
+ else
51
+ file_path = ARGV[0]
52
+ end
53
+ options = MimeTypeParser.parse(ARGV)
54
+
55
+ mime_type = File.open(file_path) {|file| MimeType.of file, options}
56
+ puts "The MIME type of #{file_path} is: #{mime_type}"
@@ -0,0 +1,35 @@
1
+ require 'magic'
2
+ require 'mimemagic'
3
+
4
+ # Provides a methods to read the MIME type of a local file
5
+ module MimeType
6
+
7
+ # Returns the MIME type of a local file
8
+ #
9
+ # @param [File] file Local file (or any Object with a +path+ method)
10
+ # @param [Hash] options the options to read the MIME type
11
+ # @option options [Boolean] :by_magic Use the File Magic type
12
+ # @option options [Integer] :by_file_type Use the File extension
13
+ # @return [String] MIME path of the file or +nil+ if unrecognized
14
+ #
15
+ # @note More info about techniques to guess the MIME type at
16
+ # {https://github.com/qoobaa/magic Magic Gem docs}
17
+ # and
18
+ # {http://rdoc.info/github/minad/mimemagic/frames/file/README MimeMagic doc}
19
+ #
20
+ # @example Read the MIME type of a local JPEG file
21
+ # MimeType.of("example.jpg")
22
+ #
23
+ # # => 'image/jpeg'
24
+ def self.of(file, options ={})
25
+ mime_type = nil
26
+ if options.fetch(:by_magic, true)
27
+ mime_magic = MimeMagic.by_magic(File.open file.path)
28
+ mime_type ||= mime_magic.type if mime_magic
29
+ end
30
+ if options.fetch(:by_file_type, true)
31
+ mime_type ||= Magic.guess_file_mime_type(file.path)
32
+ end
33
+ mime_type
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module MimeType
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mime_type
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Claudio Baccigalupo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: magic
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: mimemagic
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: redcarpet
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: yard
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: Returns the MIME type of a local file
127
+ email:
128
+ - claudio@topspinmedia.com
129
+ executables:
130
+ - mime_type_of
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - bin/mime_type_of
135
+ - lib/mime_type/version.rb
136
+ - lib/mime_type.rb
137
+ - MIT-LICENSE
138
+ - README.md
139
+ homepage: https://github.com/topspin/mime_type
140
+ licenses:
141
+ - MIT
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ none: false
148
+ requirements:
149
+ - - ! '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ segments:
153
+ - 0
154
+ hash: -555311211199056139
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ none: false
157
+ requirements:
158
+ - - ! '>='
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ segments:
162
+ - 0
163
+ hash: -555311211199056139
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 1.8.23
167
+ signing_key:
168
+ specification_version: 3
169
+ summary: ! 'MimeType provides one method: * of, which returns the MIME type of the
170
+ file passed as argument'
171
+ test_files: []
172
+ has_rdoc: