mimemagic 0.1.4

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of mimemagic might be problematic. Click here for more details.

@@ -0,0 +1,104 @@
1
+ #!/usr/bin/ruby
2
+
3
+ gem 'nokogiri', '>= 0'
4
+ require 'nokogiri'
5
+
6
+ def str2int(s)
7
+ return s.to_i(16) if s[0..1].downcase == '0x'
8
+ return s.to_i(8) if s[0..0].downcase == '0'
9
+ s.to_i(10)
10
+ end
11
+
12
+ def get_matches(parent)
13
+ parent.elements.map {|match|
14
+ if match['mask']
15
+ nil
16
+ else
17
+ type = match['type']
18
+ value = match['value']
19
+ offset = match['offset'].split(':').map {|x| x.to_i }
20
+ offset = offset.size == 2 ? offset[0]..offset[1] : offset[0]
21
+ case type
22
+ when 'string'
23
+ value.gsub!(/\\(x[\dA-Fa-f]{1,2}|0\d{1,3}|\d{1,3}|.)/) { eval("\"\\#{$1}\"") }
24
+ when 'big16'
25
+ value = str2int(value)
26
+ value = ((value >> 8).chr + (value & 0xFF).chr)
27
+ when 'big32'
28
+ value = str2int(value)
29
+ value = (((value >> 24) & 0xFF).chr + ((value >> 16) & 0xFF).chr + ((value >> 8) & 0xFF).chr + (value & 0xFF).chr)
30
+ when 'little16'
31
+ value = str2int(value)
32
+ value = ((value & 0xFF).chr + (value >> 8).chr)
33
+ when 'little32'
34
+ value = str2int(value)
35
+ value = ((value & 0xFF).chr + ((value >> 8) & 0xFF).chr + ((value >> 16) & 0xFF).chr + ((value >> 24) & 0xFF).chr)
36
+ when 'host16' # use little endian
37
+ value = str2int(value)
38
+ value = ((value & 0xFF).chr + (value >> 8).chr)
39
+ when 'host32' # use little endian
40
+ value = str2int(value)
41
+ value = ((value & 0xFF).chr + ((value >> 8) & 0xFF).chr + ((value >> 16) & 0xFF).chr + ((value >> 24) & 0xFF).chr)
42
+ when 'byte'
43
+ value = str2int(value)
44
+ value = value.chr
45
+ end
46
+ children = get_matches(match)
47
+ children.empty? ? [offset, value] : [offset, value, children]
48
+ end
49
+ }.compact
50
+ end
51
+
52
+ if ARGV.size != 1
53
+ puts "Usage: #{$0} <freedesktop.org.xml>"
54
+ exit 1
55
+ end
56
+
57
+ FILE = ARGV[0]
58
+ file = File.new(FILE)
59
+ doc = Nokogiri::XML(file)
60
+ extensions = {}
61
+ types = {}
62
+ magics = []
63
+ (doc/'mime-info/mime-type').each do |mime|
64
+ comments = Hash[*(mime/'comment').map {|comment| [comment['lang'], comment.inner_text] }.flatten]
65
+ type = mime['type']
66
+ subclass = (mime/'sub-class-of').map{|x| x['type']}
67
+ exts = (mime/'glob').map{|x| x['pattern'] =~ /^\*\.([^\[\]]+)$/ ? $1.downcase : nil }.compact
68
+ (mime/'magic').each do |magic|
69
+ priority = magic['priority'].to_i
70
+ matches = get_matches(magic)
71
+ magics << [priority, type, matches]
72
+ end
73
+ if !exts.empty?
74
+ exts.each{|x|
75
+ extensions[x] = type if !extensions.include?(x)
76
+ }
77
+ types[type] = [exts,subclass,comments[nil]]
78
+ end
79
+ end
80
+
81
+ magics = magics.sort {|a,b| b[0] <=> a[0] }.map {|x| [x[1], x[2]] }
82
+
83
+ puts "# Generated from #{FILE}"
84
+ puts "class MimeMagic"
85
+ puts " private"
86
+ puts " EXTENSIONS = {"
87
+ extensions.keys.sort.each do |key|
88
+ puts " '#{key}' => '#{extensions[key]}',"
89
+ end
90
+ puts " }"
91
+ puts " TYPES = {"
92
+ types.keys.sort.each do |key|
93
+ exts = types[key][0].sort.join(' ')
94
+ parents = types[key][1].sort.join(' ')
95
+ comment = types[key][2].inspect
96
+ puts " '#{key}' => [%w(#{exts}), %w(#{parents}), #{comment}],"
97
+ end
98
+ puts " }"
99
+ puts " MAGIC = ["
100
+ magics.each do |type, matches|
101
+ puts " ['#{type}', #{matches.inspect}],"
102
+ end
103
+ puts " ]"
104
+ puts "end"
Binary file
Binary file
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/ruby
2
+ print "Hello World"
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,72 @@
1
+ gem 'bacon', '>= 0'
2
+
3
+ require 'bacon'
4
+ require 'mimemagic'
5
+
6
+ describe 'MimeMagic' do
7
+ it 'should have mediatype helpers' do
8
+ MimeMagic.new('text/plain').should.be.text
9
+ MimeMagic.new('text/html').should.be.text
10
+ MimeMagic.new('application/octet-stream').should.not.be.text
11
+ MimeMagic.new('image/png').should.not.be.text
12
+ MimeMagic.new('image/png').should.be.image
13
+ MimeMagic.new('video/ogg').should.be.video
14
+ MimeMagic.new('audio/mpeg').should.be.audio
15
+ end
16
+
17
+ it 'should have hierarchy' do
18
+ MimeMagic.new('text/html').should.be.child_of 'text/plain'
19
+ MimeMagic.new('text/x-java').should.be.child_of 'text/plain'
20
+ end
21
+
22
+ it 'should have extensions' do
23
+ MimeMagic.new('text/html').extensions.should.equal %w(htm html)
24
+ end
25
+
26
+ it 'should have comment' do
27
+ MimeMagic.new('text/html').comment.should.equal 'HTML document'
28
+ end
29
+
30
+ it 'should recognize extensions' do
31
+ MimeMagic.by_extension('.html').to_s.should.equal 'text/html'
32
+ MimeMagic.by_extension('html').to_s.should.equal 'text/html'
33
+ MimeMagic.by_extension(:html).to_s.should.equal 'text/html'
34
+ MimeMagic.by_extension('rb').to_s.should.equal 'application/x-ruby'
35
+ MimeMagic.by_extension('crazy').should.equal nil
36
+ MimeMagic.by_extension('').should.equal nil
37
+ end
38
+
39
+ it 'should recognize by magic' do
40
+ Dir['test/files/*'].each do |file|
41
+ mime = file[11..-1].gsub('.', '/')
42
+ MimeMagic.by_magic(File.read(file)).to_s.should.equal mime
43
+ MimeMagic.by_magic(File.open(file, 'rb')).to_s.should.equal mime
44
+ end
45
+ end
46
+
47
+ it 'should have add' do
48
+ MimeMagic.add('application/mimemagic-test',
49
+ :extensions => %w(ext1 ext2),
50
+ :parents => 'application/xml',
51
+ :comment => 'Comment')
52
+ MimeMagic.by_extension('ext1').to_s.should.equal 'application/mimemagic-test'
53
+ MimeMagic.by_extension('ext2').to_s.should.equal 'application/mimemagic-test'
54
+ MimeMagic.by_extension('ext2').comment.should.equal 'Comment'
55
+ MimeMagic.new('application/mimemagic-test').extensions.should.equal %w(ext1 ext2)
56
+ MimeMagic.new('application/mimemagic-test').should.be.child_of 'text/plain'
57
+ end
58
+
59
+ it 'should process magic' do
60
+ MimeMagic.add('application/mimemagic-test',
61
+ :magic => [[0, 'MAGICTEST'], # MAGICTEST at position 0
62
+ [1, 'MAGICTEST'], # MAGICTEST at position 1
63
+ [2, 'MAGICTEST', [[0, 'X'], [0, 'Y']]]]) # MAGICTEST at position 2 and (X at 0 or Y at 0)
64
+
65
+ MimeMagic.by_magic('MAGICTEST').to_s.should.equal 'application/mimemagic-test'
66
+ MimeMagic.by_magic('XMAGICTEST').to_s.should.equal 'application/mimemagic-test'
67
+ MimeMagic.by_magic(' MAGICTEST').to_s.should.equal 'application/mimemagic-test'
68
+ MimeMagic.by_magic('X MAGICTEST').to_s.should.equal 'application/mimemagic-test'
69
+ MimeMagic.by_magic('Y MAGICTEST').to_s.should.equal 'application/mimemagic-test'
70
+ MimeMagic.by_magic('Z MAGICTEST').should.equal nil
71
+ end
72
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mimemagic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Mendler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-09 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.3
24
+ version:
25
+ description:
26
+ email:
27
+ - mail@daniel-mendler.de
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files: []
33
+
34
+ files:
35
+ - lib/mimemagic.rb
36
+ - lib/mimemagic_tables.rb
37
+ - test/test_mimemagic.rb
38
+ - test/files/application.x-bzip
39
+ - test/files/image.jpeg
40
+ - test/files/image.png
41
+ - test/files/application.x-tar
42
+ - test/files/application.x-gzip
43
+ - test/files/application.zip
44
+ - test/files/application.x-ruby
45
+ - script/freedesktop.org.xml
46
+ - script/generate-mime.rb
47
+ - Rakefile
48
+ - README
49
+ has_rdoc: true
50
+ homepage: https://github.com/minad/mimemagic
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --main
54
+ - README.txt
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project: mimemagic
72
+ rubygems_version: 1.3.1
73
+ signing_key:
74
+ specification_version: 2
75
+ summary: Mime detection by extension or content
76
+ test_files:
77
+ - test/test_mimemagic.rb