magic 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,12 +1,13 @@
1
1
  = magic
2
2
 
3
- Ruby FFI bindings to the "magic" library. Determines content type and
4
- encoding of files and strings. There are three sets of tests,
3
+ Ruby FFI bindings to the "magic" library, that determines content type
4
+ and encoding of files and strings. There are three sets of tests,
5
5
  performed in this order: filesystem tests, magic number tests, and
6
6
  language tests. The first test that succeeds causes the file type to
7
7
  be printed.
8
8
 
9
9
  == Usage
10
+
10
11
  require "magic"
11
12
  Magic.guess_file_mime("public/images/rails.png")
12
13
  # => "image/png; charset=binary"
@@ -20,6 +21,44 @@ be printed.
20
21
  # => "utf-8"
21
22
  Magic.guess_string_mime_type("Magic® File™")
22
23
  # => "text/plain"
24
+ Magic.guess(:mime_type, :database => "/etc/magic") { |db| db.buffer("Magic® File™") }
25
+ # => "text/plain"
26
+
27
+ == Installation
28
+
29
+ gem install magic --source=http://gemcutter.org
30
+
31
+ === Linux
32
+
33
+ Install magic library using your package manager, e.g.:
34
+
35
+ sudo apt-get install file
36
+
37
+ === Mac OS
38
+
39
+ If you don't have <tt>libmagic.1.dylib</tt> file in your system, you need to
40
+ install it via port command:
41
+
42
+ sudo port install file
43
+
44
+ Sometimes you also need to set your
45
+ <tt>DYLD_FALLBACK_LIBRARY_PATH</tt> environment variable to the
46
+ directory of the <tt>libmagic.1.dylib</tt>:
47
+
48
+ export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib
49
+
50
+ === Windows
51
+
52
+ Install {File for
53
+ Windows}[http://gnuwin32.sourceforge.net/packages/file.htm]. You also
54
+ need to set your <tt>PATH</tt> environment variable to the directory of the
55
+ <tt>magic1.dll</tt> file:
56
+
57
+ set PATH=C:\Program Files\GnuWin32\bin;%PATH%
58
+
59
+ It might be also required to add <tt>database</tt> option explicitely:
60
+
61
+ Magic.guess_string_mime("Magic® File™", :database => 'C:\Program Files\GnuWin32\share\misc\magic.mgc')
23
62
 
24
63
  == Links
25
64
  * gemcutter[http://gemcutter.org/gems/magic]
@@ -28,6 +67,7 @@ be printed.
28
67
  * rdoc[http://qoobaa.github.com/magic]
29
68
 
30
69
  == Note on Patches/Pull Requests
70
+
31
71
  * Fork the project.
32
72
  * Make your feature addition or bug fix.
33
73
  * Add tests for it. This is important so I don't break it in a future
@@ -38,6 +78,7 @@ be printed.
38
78
  * Send me a pull request. Bonus points for topic branches.
39
79
 
40
80
  == Copyright
81
+
41
82
  Copyright (c) 2010 Jakub Kuźma. See
42
83
  LICENSE[http://github.com/qoobaa/magic/raw/master/LICENSE] for
43
84
  details.
data/Rakefile CHANGED
@@ -31,7 +31,7 @@ begin
31
31
  | You'll also need to set your PATH environment variable to the |
32
32
  | directory of the magic1.dll file |
33
33
  | |
34
- | set PATH=C:\Program Files\GnuWin32\bin;%PATH% |
34
+ | set PATH=C:\\Program Files\\GnuWin32\\bin;%PATH% |
35
35
  | |
36
36
  +-NOTE FOR MAC OS USERS --------------------------------------------+
37
37
  | |
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
data/lib/magic.rb CHANGED
@@ -13,57 +13,59 @@ module Magic
13
13
  # ====== Example
14
14
  # Magic.guess_file_mime("public/images/rails.png")
15
15
  # # => "image/png; charset=binary"
16
- def guess_file_mime(file)
17
- guess(:file, :mime, file)
16
+ def guess_file_mime(filename, *args)
17
+ guess(*args.unshift(:mime)) { |db| db.file(filename) }
18
18
  end
19
19
 
20
20
  # Guesses mime encoding of given file
21
21
  # ===== Example
22
22
  # Magic.guess_file_mime_encoding("public/images/rails.png")
23
23
  # # => "binary"
24
- def guess_file_mime_encoding(file)
25
- guess(:file, :mime_encoding, file)
24
+ def guess_file_mime_encoding(filename, *args)
25
+ guess(*args.unshift(:mime_encoding)) { |db| db.file(filename) }
26
26
  end
27
27
 
28
28
  # Guesses mime type of given file
29
29
  # ===== Example
30
30
  # Magic.guess_file_mime_type("public/images/rails.png")
31
31
  # # => "image/png"
32
- def guess_file_mime_type(file)
33
- guess(:file, :mime_type, file)
32
+ def guess_file_mime_type(filename, *args)
33
+ guess(*args.unshift(:mime_type)) { |db| db.file(filename) }
34
34
  end
35
35
 
36
36
  # Guesses mime type of given string
37
37
  # ===== Example
38
38
  # Magic.guess_string_mime("Magic® File™")
39
39
  # # => "text/plain; charset=utf-8"
40
- def guess_string_mime(string)
41
- guess(:buffer, :mime, string)
40
+ def guess_string_mime(string, *args)
41
+ guess(*args.unshift(:mime)) { |db| db.buffer(string) }
42
42
  end
43
43
 
44
44
  # Guesses mime type of given string
45
45
  # ===== Example
46
46
  # Magic.guess_string_mime_encoding("Magic® File™")
47
47
  # # => "utf-8"
48
- def guess_string_mime_encoding(string)
49
- guess(:buffer, :mime_encoding, string)
48
+ def guess_string_mime_encoding(string, *args)
49
+ guess(*args.unshift(:mime_type)) { |db| db.buffer(string) }
50
50
  end
51
51
 
52
52
  # Guesses mime type of given string
53
53
  # ===== Example
54
54
  # Magic.guess_string_mime_type("Magic® File™")
55
55
  # # => "text/plain"
56
- def guess_string_mime_type(string)
57
- guess(:buffer, :mime_type, string)
56
+ def guess_string_mime_type(string, *args)
57
+ guess(*args.unshift(:mime_type)) { |db| db.buffer(string) }
58
58
  end
59
59
 
60
- protected
61
-
62
- def guess(type, what, where) #:nodoc:
63
- db = Database.new(what)
64
- result = db.send(type, where)
65
- db.close
66
- result
60
+ # Creates magic database and yields it to the given block
61
+ # ===== Example
62
+ # Magic.guess(:mime) { |db| db.buffer("Magic® File™") }
63
+ # # => "text/plain; charset=utf-8"
64
+ def guess(*args)
65
+ db = Database.new(*args)
66
+ yield(db).tap do
67
+ db.close
68
+ end
67
69
  end
68
70
  end
69
71
  end
data/lib/magic/api.rb CHANGED
@@ -10,7 +10,7 @@ module Magic
10
10
  # attach_function :magic_descriptor, [:pointer, :int], :string
11
11
  attach_function :magic_buffer, [:pointer, :pointer, :uint], :pointer
12
12
  attach_function :magic_error, [:pointer], :string
13
- # attach_function :magic_setflags, [:pointer, :int], :int
13
+ attach_function :magic_setflags, [:pointer, :int], :int
14
14
  attach_function :magic_load, [:pointer, :string], :int
15
15
  # attach_function :magic_compile, [:pointer, :string], :int
16
16
  # attach_function :magic_check, [:pointer, :string], :int
@@ -1,15 +1,20 @@
1
1
  module Magic
2
2
  class Database
3
- # Creates an instance of +Magic::Database+ using given flags
4
- def initialize(*flags)
5
- open(*flags)
6
- load
3
+ attr_reader :flags
4
+
5
+ # Creates an instance of +Magic::Database+ using given database
6
+ # file and flags
7
+ def initialize(*args)
8
+ options = args.last.is_a?(Hash) ? args.pop : {} # extract options
9
+ database = options.delete(:database)
10
+ open(*args)
11
+ load(database)
7
12
  end
8
13
 
9
14
  # Opens magic db using given flags
10
15
  def open(*flags)
11
- magic_flags = flags.inject(0) { |acc, flag| acc |= Constants::Flag.const_get(flag.to_s.upcase) }
12
- @magic_set = Api.magic_open(magic_flags)
16
+ @flags = calculate_flags(*flags)
17
+ @magic_set = Api.magic_open(@flags)
13
18
  end
14
19
 
15
20
  # Closes the database
@@ -42,9 +47,21 @@ module Magic
42
47
  end
43
48
  end
44
49
 
45
- # Returns last error occured
50
+ # Returns the last error occured
46
51
  def error
47
52
  Api.magic_error(@magic_set)
48
53
  end
54
+
55
+ # Sets the flags
56
+ def flags=(*flags)
57
+ @flags = calculate_flags(*flags)
58
+ Api.magic_setflags(@magic_set, @flags)
59
+ end
60
+
61
+ protected
62
+
63
+ def calculate_flags(*flags)
64
+ flags.inject(0) { |calculated, flag| calculated |= Constants::Flag.const_get(flag.to_s.upcase) }
65
+ end
49
66
  end
50
67
  end
@@ -0,0 +1 @@
1
+ # This is an empty magic database file
@@ -0,0 +1,29 @@
1
+ #------------------------------------------------------------------------------
2
+ # JPEG images
3
+ # SunOS 5.5.1 had
4
+ #
5
+ # 0 string \377\330\377\340 JPEG file
6
+ # 0 string \377\330\377\356 JPG file
7
+ #
8
+ # both of which turn into "JPEG image data" here.
9
+ #
10
+ 0 beshort 0xffd8 JPEG image data
11
+ !:mime image/jpeg
12
+ !:apple 8BIMJPEG
13
+ !:strength +1
14
+ >6 string JFIF \b, JFIF standard
15
+ # The following added by Erik Rossen <rossen@freesurf.ch> 1999-09-06
16
+ # in a vain attempt to add image size reporting for JFIF. Note that these
17
+ # tests are not fool-proof since some perfectly valid JPEGs are currently
18
+ # impossible to specify in magic(4) format.
19
+ # First, a little JFIF version info:
20
+ >>11 byte x \b %d.
21
+ >>12 byte x \b%02d
22
+ # Next, the resolution or aspect ratio of the image:
23
+ #>>13 byte 0 \b, aspect ratio
24
+ #>>13 byte 1 \b, resolution (DPI)
25
+ #>>13 byte 2 \b, resolution (DPCM)
26
+ #>>4 beshort x \b, segment length %d
27
+ # Next, show thumbnail info, if it exists:
28
+ >>18 byte !0 \b, thumbnail %dx
29
+ >>>19 byte x \b%d
data/test/test_magic.rb CHANGED
@@ -42,4 +42,18 @@ class TestMagic < Test::Unit::TestCase
42
42
  Magic.guess_file_mime_encoding(fixture("non-existing.file"))
43
43
  end
44
44
  end
45
+
46
+ def test_guess_magic_logo_mime_with_jpeg_database
47
+ assert_equal "image/jpeg; charset=binary", Magic.guess_file_mime(fixture("filelogo.jpg"), :database => fixture("magic_jpeg"))
48
+ end
49
+
50
+ def test_guess_magic_logo_mime_with_empty_database
51
+ assert_equal "application/octet-stream; charset=binary", Magic.guess_file_mime(fixture("filelogo.jpg"), :database => fixture("magic_empty"))
52
+ end
53
+
54
+ def test_guess_with_block
55
+ result = nil
56
+ Magic.guess(:mime) { |db| result = db.file(fixture("filelogo.jpg")) }
57
+ assert_equal "image/jpeg; charset=binary", result
58
+ end
45
59
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jakub Ku\xC5\xBAma"
@@ -55,45 +55,49 @@ files:
55
55
  - lib/magic/errors.rb
56
56
  - test/fixtures/filelogo.jpg
57
57
  - test/fixtures/magic.txt
58
+ - test/fixtures/magic_empty
59
+ - test/fixtures/magic_jpeg
58
60
  - test/helper.rb
59
61
  - test/test_magic.rb
60
62
  has_rdoc: true
61
63
  homepage: http://github.com/qoobaa/magic
62
64
  licenses: []
63
65
 
64
- post_install_message: "+-NOTE FOR LINUX USERS----------------------------------------------+\n\
65
- | |\n\
66
- | Install libmagic using your package manager, e.g. |\n\
67
- | |\n\
68
- | sudo apt-get install file |\n\
69
- | |\n\
70
- +-NOTE FOR WINDOWS USERS -------------------------------------------+\n\
71
- | |\n\
72
- | Install File for Windows from |\n\
73
- | |\n\
74
- | http://gnuwin32.sourceforge.net/packages/file.htm |\n\
75
- | |\n\
76
- | You'll also need to set your PATH environment variable to the |\n\
77
- | directory of the magic1.dll file |\n\
78
- | |\n\
79
- | set PATH=C:Program FilesGnuWin32\bin;%PATH% |\n\
80
- | |\n\
81
- +-NOTE FOR MAC OS USERS --------------------------------------------+\n\
82
- | |\n\
83
- | If you don't have libmagic.1.dylib file in your system |\n\
84
- | |\n\
85
- | find / -name libmagic.1.dylib |\n\
86
- | |\n\
87
- | You need to install it via port command |\n\
88
- | |\n\
89
- | sudo port install file |\n\
90
- | |\n\
91
- | Sometimes you'll also need to set your DYLD_FALLBACK_LIBRARY_PATH |\n\
92
- | environment variable to the directory of the libmagic.1.dylib |\n\
93
- | |\n\
94
- | export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib |\n\
95
- | |\n\
96
- +-------------------------------------------------------------------+\n"
66
+ post_install_message: |
67
+ +-NOTE FOR LINUX USERS----------------------------------------------+
68
+ | |
69
+ | Install libmagic using your package manager, e.g. |
70
+ | |
71
+ | sudo apt-get install file |
72
+ | |
73
+ +-NOTE FOR WINDOWS USERS -------------------------------------------+
74
+ | |
75
+ | Install File for Windows from |
76
+ | |
77
+ | http://gnuwin32.sourceforge.net/packages/file.htm |
78
+ | |
79
+ | You'll also need to set your PATH environment variable to the |
80
+ | directory of the magic1.dll file |
81
+ | |
82
+ | set PATH=C:\Program Files\GnuWin32\bin;%PATH% |
83
+ | |
84
+ +-NOTE FOR MAC OS USERS --------------------------------------------+
85
+ | |
86
+ | If you don't have libmagic.1.dylib file in your system |
87
+ | |
88
+ | find / -name libmagic.1.dylib |
89
+ | |
90
+ | You need to install it via port command |
91
+ | |
92
+ | sudo port install file |
93
+ | |
94
+ | Sometimes you'll also need to set your DYLD_FALLBACK_LIBRARY_PATH |
95
+ | environment variable to the directory of the libmagic.1.dylib |
96
+ | |
97
+ | export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib |
98
+ | |
99
+ +-------------------------------------------------------------------+
100
+
97
101
  rdoc_options:
98
102
  - --charset=UTF-8
99
103
  require_paths: