magic 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -0
- data/Rakefile +1 -1
- data/VERSION +1 -0
- data/lib/magic.rb +44 -0
- data/lib/magic/api.rb +19 -0
- data/lib/magic/constants.rb +29 -0
- data/lib/magic/database.rb +43 -0
- data/lib/magic/errors.rb +4 -0
- data/lib/magic/library.rb +22 -0
- metadata +9 -3
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -9,7 +9,7 @@ begin
|
|
9
9
|
gem.name = "magic"
|
10
10
|
gem.summary = %Q{Ruby bindings for Magic}
|
11
11
|
gem.description = %Q{Ruby bindings for Magic}
|
12
|
-
gem.email = "qoobaa
|
12
|
+
gem.email = "qoobaa@gmail.com"
|
13
13
|
gem.homepage = "http://github.com/qoobaa/magic"
|
14
14
|
gem.authors = ["Jakub Kuźma"]
|
15
15
|
gem.add_dependency "ffi", ">= 0.5.1"
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/magic.rb
CHANGED
@@ -0,0 +1,44 @@
|
|
1
|
+
require "ffi"
|
2
|
+
|
3
|
+
require "magic/errors"
|
4
|
+
require "magic/library"
|
5
|
+
require "magic/api"
|
6
|
+
require "magic/constants"
|
7
|
+
require "magic/database"
|
8
|
+
|
9
|
+
module Magic
|
10
|
+
class << self
|
11
|
+
def guess_file_mime(file)
|
12
|
+
guess(:file, :mime, file)
|
13
|
+
end
|
14
|
+
|
15
|
+
def guess_file_mime_encoding(file)
|
16
|
+
guess(:file, :mime_encoding, file)
|
17
|
+
end
|
18
|
+
|
19
|
+
def guess_file_mime_type(file)
|
20
|
+
guess(:file, :mime_type, file)
|
21
|
+
end
|
22
|
+
|
23
|
+
def guess_string_mime(string)
|
24
|
+
guess(:buffer, :mime, string)
|
25
|
+
end
|
26
|
+
|
27
|
+
def guess_string_mime_encoding(string)
|
28
|
+
guess(:buffer, :mime_encoding, string)
|
29
|
+
end
|
30
|
+
|
31
|
+
def guess_string_mime_type(string)
|
32
|
+
guess(:buffer, :mime_type, string)
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def guess(type, what, where)
|
38
|
+
db = Database.new(what)
|
39
|
+
result = db.send(type, where)
|
40
|
+
db.close
|
41
|
+
result
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/magic/api.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Magic
|
2
|
+
module Api
|
3
|
+
extend FFI::Library
|
4
|
+
|
5
|
+
ffi_lib Library.new.name
|
6
|
+
|
7
|
+
attach_function :magic_open, [:int], :pointer
|
8
|
+
attach_function :magic_close, [:pointer], :void
|
9
|
+
attach_function :magic_file, [:pointer, :string], :pointer
|
10
|
+
# attach_function :magic_descriptor, [:pointer, :int], :string
|
11
|
+
attach_function :magic_buffer, [:pointer, :pointer, :size_t], :pointer
|
12
|
+
attach_function :magic_error, [:pointer], :string
|
13
|
+
# attach_function :magic_setflags, [:pointer, :int], :int
|
14
|
+
attach_function :magic_load, [:pointer, :string], :int
|
15
|
+
# attach_function :magic_compile, [:pointer, :string], :int
|
16
|
+
# attach_function :magic_check, [:pointer, :string], :int
|
17
|
+
# attach_function :magic_errno, [:pointer], :int
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Magic
|
2
|
+
module Constants
|
3
|
+
module Flag
|
4
|
+
NONE = 0x000000 # No flags
|
5
|
+
DEBUG = 0x000001 # Turn on debugging
|
6
|
+
SYMLINK = 0x000002 # Follow symlinks
|
7
|
+
COMPRESS = 0x000004 # Check inside compressed files
|
8
|
+
DEVICES = 0x000008 # Look at the contents of devices
|
9
|
+
MIME_TYPE = 0x000010 # Return the MIME type
|
10
|
+
CONTINUE = 0x000020 # Return all matches
|
11
|
+
CHECK = 0x000040 # Print warnings to stderr
|
12
|
+
PRESERVE_ATIME = 0x000080 # Restore access time on exit
|
13
|
+
RAW = 0x000100 # Don't translate unprintable chars
|
14
|
+
ERROR = 0x000200 # Handle ENOENT etc as real errors
|
15
|
+
MIME_ENCODING = 0x000400 # Return the MIME encoding
|
16
|
+
MIME = (MIME_TYPE | MIME_ENCODING)
|
17
|
+
APPLE = 0x000800 # Return the Apple creator and type
|
18
|
+
NO_CHECK_COMPRESS = 0x001000 # Don't check for compressed files
|
19
|
+
NO_CHECK_TAR = 0x002000 # Don't check for tar files
|
20
|
+
NO_CHECK_SOFT = 0x004000 # Don't check magic entries
|
21
|
+
NO_CHECK_APPTYPE = 0x008000 # Don't check application type
|
22
|
+
NO_CHECK_ELF = 0x010000 # Don't check for elf details
|
23
|
+
NO_CHECK_TEXT = 0x020000 # Don't check for text files
|
24
|
+
NO_CHECK_CDF = 0x040000 # Don't check for cdf files
|
25
|
+
NO_CHECK_TOKENS = 0x100000 # Don't check tokens
|
26
|
+
NO_CHECK_ENCODING = 0x200000 # Don't check text encodings
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Magic
|
2
|
+
class Database
|
3
|
+
def initialize(*flags)
|
4
|
+
open(*flags)
|
5
|
+
load
|
6
|
+
end
|
7
|
+
|
8
|
+
def open(*flags)
|
9
|
+
magic_flags = flags.inject(0) { |acc, flag| acc |= Constants::Flag.const_get(flag.to_s.upcase) }
|
10
|
+
@magic_set = Api.magic_open(magic_flags)
|
11
|
+
end
|
12
|
+
|
13
|
+
def close
|
14
|
+
Api.magic_close(@magic_set)
|
15
|
+
end
|
16
|
+
|
17
|
+
def load(database = nil)
|
18
|
+
Api.magic_load(@magic_set, database)
|
19
|
+
end
|
20
|
+
|
21
|
+
def file(file)
|
22
|
+
result = Api.magic_file(@magic_set, file)
|
23
|
+
if result.null?
|
24
|
+
raise Exception, error
|
25
|
+
else
|
26
|
+
result.get_string(0)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def buffer(string)
|
31
|
+
result = Api.magic_buffer(@magic_set, string, string.bytesize)
|
32
|
+
if result.null?
|
33
|
+
raise Exception, error
|
34
|
+
else
|
35
|
+
result.get_string(0)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def error
|
40
|
+
Api.magic_error(@magic_set)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/magic/errors.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Magic
|
2
|
+
class Library
|
3
|
+
def initialize
|
4
|
+
name or raise UnsupportedPlatform, "platform #{platform} is unsupported"
|
5
|
+
end
|
6
|
+
|
7
|
+
def platform
|
8
|
+
RUBY_PLATFORM.downcase
|
9
|
+
end
|
10
|
+
|
11
|
+
def name
|
12
|
+
case platform
|
13
|
+
when /darwin/
|
14
|
+
"libmagic.1.dylib"
|
15
|
+
when /linux|freebsd|netbsd|openbsd|dragonfly|solaris/
|
16
|
+
"libmagic.so.1"
|
17
|
+
when /win|mingw/
|
18
|
+
"magic1.dll"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
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.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Jakub Ku\xC5\xBAma"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-11 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -33,7 +33,7 @@ dependencies:
|
|
33
33
|
version: "2.0"
|
34
34
|
version:
|
35
35
|
description: Ruby bindings for Magic
|
36
|
-
email: qoobaa
|
36
|
+
email: qoobaa@gmail.com
|
37
37
|
executables: []
|
38
38
|
|
39
39
|
extensions: []
|
@@ -47,7 +47,13 @@ files:
|
|
47
47
|
- LICENSE
|
48
48
|
- README.rdoc
|
49
49
|
- Rakefile
|
50
|
+
- VERSION
|
50
51
|
- lib/magic.rb
|
52
|
+
- lib/magic/api.rb
|
53
|
+
- lib/magic/constants.rb
|
54
|
+
- lib/magic/database.rb
|
55
|
+
- lib/magic/errors.rb
|
56
|
+
- lib/magic/library.rb
|
51
57
|
- test/helper.rb
|
52
58
|
- test/test_magic.rb
|
53
59
|
has_rdoc: true
|