magic-0.2.9/0000755000004100000410000000000012530735146012640 5ustar www-datawww-datamagic-0.2.9/Rakefile0000644000004100000410000000123212530735146014303 0ustar www-datawww-data$:.unshift File.expand_path("../lib", __FILE__) require "rubygems" require "rubygems/specification" require "rake/testtask" require "rdoc/task" require "rubygems/package_task" require "bundler/gem_tasks" require "magic" def gemspec file = File.expand_path("../magic.gemspec", __FILE__) eval(File.read(file), binding, file) end Rake::TestTask.new(:test) do |test| test.libs << "lib" << "test" test.pattern = "test/**/test_*.rb" test.verbose = true end RDoc::Task.new do |rdoc| rdoc.rdoc_dir = "rdoc" rdoc.title = "magic #{Magic::VERSION}" rdoc.rdoc_files.include("README.rdoc") rdoc.rdoc_files.include("lib/**/*.rb") end task :default => :test magic-0.2.9/Gemfile0000644000004100000410000000013212530735146014127 0ustar www-datawww-datasource 'https://rubygems.org' # Specify your gem's dependencies in magic.gemspec gemspec magic-0.2.9/magic.gemspec0000644000004100000410000000533212530735146015270 0ustar www-datawww-data# coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'magic/version' Gem::Specification.new do |spec| spec.name = "magic" spec.version = Magic::VERSION spec.authors = ["Kuba Kuźma"] spec.email = ["kuba@jah.pl"] spec.summary = %q{Determine file type and encoding using "magic" numbers} spec.description = %q{Ruby FFI bindings to libmagic} spec.homepage = "https://github.com/qoobaa/magic" spec.license = "MIT" spec.files = `git ls-files -z`.split("\x0") spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] spec.add_runtime_dependency "ffi", [">= 0.6.3"] spec.add_development_dependency "bundler", "~> 1.5" spec.add_development_dependency "rake" spec.add_development_dependency "test-unit", [">= 2.0"] spec.post_install_message = <<-EOM +-NOTE FOR LINUX USERS----------------------------------------------+ | | | Install libmagic using your package manager, e.g. | | | | sudo apt-get install file | | | +-NOTE FOR WINDOWS USERS -------------------------------------------+ | | | Install File for Windows from | | | | http://gnuwin32.sourceforge.net/packages/file.htm | | | | You'll also need to set your PATH environment variable to the | | directory of the magic1.dll file | | | | set PATH=C:\\Program Files\\GnuWin32\\bin;%PATH% | | | +-NOTE FOR MAC OS USERS --------------------------------------------+ | | | If you don't have libmagic.1.dylib file in your system, you need | | to install it using port command | | | | sudo port install file | | | +-------------------------------------------------------------------+ EOM end magic-0.2.9/LICENSE.txt0000644000004100000410000000203712530735146014465 0ustar www-datawww-dataCopyright (c) 2014 Kuba Kuźma Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. magic-0.2.9/README.rdoc0000644000004100000410000000135012530735146014445 0ustar www-datawww-data= magic Ruby FFI wrapper to the "magic" library, that determines content type and encoding of files and strings. The library does three types of tests: filesystem tests, magic number tests, and language tests. The first test that succeeds causes the file type to be returned. == Installation and Usage See {project page}[http://jah.pl/projects/magic.html] for details. == Links * {project page}[http://jah.pl/projects/magic.html] * gemcutter[http://gemcutter.org/gems/magic] * repository[http://github.com/qoobaa/magic] * {issue tracker}[http://github.com/qoobaa/magic/issues] * rdoc[http://qoobaa.github.com/magic] == Copyright Copyright (c) 2010 Jakub Kuźma. See LICENSE[http://github.com/qoobaa/magic/raw/master/LICENSE] for details. magic-0.2.9/lib/0000755000004100000410000000000012530735146013406 5ustar www-datawww-datamagic-0.2.9/lib/magic.rb0000644000004100000410000000413312530735146015014 0ustar www-datawww-data# encoding: utf-8 require "ffi" require "magic/errors" require "magic/api" require "magic/constants" require "magic/database" require "magic/version" module Magic class << self # Guesses mime of given file # # ====== Example # Magic.guess_file_mime("public/images/rails.png") # # => "image/png; charset=binary" def guess_file_mime(filename, *args) guess(*args.unshift(:mime)) { |db| db.file(filename) } end # Guesses mime encoding of given file # # ===== Example # Magic.guess_file_mime_encoding("public/images/rails.png") # # => "binary" def guess_file_mime_encoding(filename, *args) guess(*args.unshift(:mime_encoding)) { |db| db.file(filename) } end # Guesses mime type of given file # # ===== Example # Magic.guess_file_mime_type("public/images/rails.png") # # => "image/png" def guess_file_mime_type(filename, *args) guess(*args.unshift(:mime_type)) { |db| db.file(filename) } end # Guesses mime type of given string # # ===== Example # Magic.guess_string_mime("Magic® File™") # # => "text/plain; charset=utf-8" def guess_string_mime(string, *args) guess(*args.unshift(:mime)) { |db| db.buffer(string) } end # Guesses mime type of given string # # ===== Example # Magic.guess_string_mime_encoding("Magic® File™") # # => "utf-8" def guess_string_mime_encoding(string, *args) guess(*args.unshift(:mime_encoding)) { |db| db.buffer(string) } end # Guesses mime type of given string # # ===== Example # Magic.guess_string_mime_type("Magic® File™") # # => "text/plain" def guess_string_mime_type(string, *args) guess(*args.unshift(:mime_type)) { |db| db.buffer(string) } end # Creates magic database and yields it to the given block # # ===== Example # Magic.guess(:mime) { |db| db.buffer("Magic® File™") } # # => "text/plain; charset=utf-8" def guess(*args) db = Database.new(*args) result = yield(db) db.close result end end end magic-0.2.9/lib/magic/0000755000004100000410000000000012530735146014466 5ustar www-datawww-datamagic-0.2.9/lib/magic/errors.rb0000644000004100000410000000012112530735146016321 0ustar www-datawww-datamodule Magic # General Exception class class Error < StandardError end end magic-0.2.9/lib/magic/database.rb0000644000004100000410000000315112530735146016557 0ustar www-datawww-datamodule Magic class Database attr_reader :flags # Creates an instance of +Magic::Database+ using given database # file and flags def initialize(*args) options = args.last.is_a?(Hash) ? args.pop : {} # extract options database = options.delete(:database) open(*args) load(database) end # Opens magic db using given flags def open(*flags) @flags = calculate_flags(*flags) @magic_set = Api.magic_open(@flags) end # Closes the database def close Api.magic_close(@magic_set) end # Loads given database file (or default if +nil+ given) def load(database = nil) Api.magic_load(@magic_set, database) end # Determine type of a file at given path def file(filename) raise Errno::ENOENT, filename unless File.exists?(filename) result = Api.magic_file(@magic_set, filename.to_s) if result.null? raise Error, error else result.get_string(0) end end # Determine type of given string def buffer(string) result = Api.magic_buffer(@magic_set, string, string.bytesize) if result.null? raise Error, error else result.get_string(0) end end # Returns the last error occured def error Api.magic_error(@magic_set) end # Sets the flags def flags=(*flags) @flags = calculate_flags(*flags) Api.magic_setflags(@magic_set, @flags) end protected def calculate_flags(*flags) flags.inject(0) { |calculated, flag| calculated |= Constants::Flag.const_get(flag.to_s.upcase) } end end end magic-0.2.9/lib/magic/api.rb0000644000004100000410000000166012530735146015567 0ustar www-datawww-datamodule Magic module Api #:nodoc: extend FFI::Library lib_paths = Array(ENV["MAGIC_LIB"] || Dir["/{opt,usr}/{,local/}lib{,64}/libmagic.{1.dylib,so.1*,so.4}"]) fallback_names = %w(libmagic.1.dylib libmagic.so.1 magic1.dll) ffi_lib(lib_paths + fallback_names) attach_function :magic_open, [:int], :pointer attach_function :magic_close, [:pointer], :void attach_function :magic_file, [:pointer, :string], :pointer # attach_function :magic_descriptor, [:pointer, :int], :string attach_function :magic_buffer, [:pointer, :pointer, :uint], :pointer attach_function :magic_error, [:pointer], :string attach_function :magic_setflags, [:pointer, :int], :int attach_function :magic_load, [:pointer, :string], :int # attach_function :magic_compile, [:pointer, :string], :int # attach_function :magic_check, [:pointer, :string], :int # attach_function :magic_errno, [:pointer], :int end end magic-0.2.9/lib/magic/version.rb0000644000004100000410000000004512530735146016477 0ustar www-datawww-datamodule Magic VERSION = "0.2.9" end magic-0.2.9/lib/magic/constants.rb0000644000004100000410000000323112530735146017026 0ustar www-datawww-datamodule Magic module Constants #:nodoc: module Flag # No flags NONE = 0x000000 # Turn on debugging DEBUG = 0x000001 # Follow symlinks SYMLINK = 0x000002 # Check inside compressed files COMPRESS = 0x000004 # Look at the contents of devices DEVICES = 0x000008 # Return the MIME type MIME_TYPE = 0x000010 # Return all matches CONTINUE = 0x000020 # Print warnings to stderr CHECK = 0x000040 # Restore access time on exit PRESERVE_ATIME = 0x000080 # Don't translate unprintable chars RAW = 0x000100 # Handle ENOENT etc as real errors ERROR = 0x000200 # Return the MIME encoding MIME_ENCODING = 0x000400 # Return the MIME "type; charset=encoding" MIME = (MIME_TYPE | MIME_ENCODING) # Return the Apple creator and type APPLE = 0x000800 # Don't check for compressed files NO_CHECK_COMPRESS = 0x001000 # Don't check for tar files NO_CHECK_TAR = 0x002000 # Don't check magic entries NO_CHECK_SOFT = 0x004000 # Don't check application type NO_CHECK_APPTYPE = 0x008000 # Don't check for elf details NO_CHECK_ELF = 0x010000 # Don't check for text files NO_CHECK_TEXT = 0x020000 # Don't check for cdf files NO_CHECK_CDF = 0x040000 # Don't check tokens NO_CHECK_TOKENS = 0x100000 # Don't check text encodings NO_CHECK_ENCODING = 0x200000 end end end magic-0.2.9/metadata.yml0000644000004100000410000001110012530735146015134 0ustar www-datawww-data--- !ruby/object:Gem::Specification name: magic version: !ruby/object:Gem::Version version: 0.2.9 platform: ruby authors: - Kuba Kuźma autorequire: bindir: bin cert_chain: [] date: 2015-04-16 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: ffi requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: 0.6.3 type: :runtime prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: 0.6.3 - !ruby/object:Gem::Dependency name: bundler requirement: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '1.5' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - "~>" - !ruby/object:Gem::Version version: '1.5' - !ruby/object:Gem::Dependency name: rake requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: test-unit requirement: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '2.0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '2.0' description: Ruby FFI bindings to libmagic email: - kuba@jah.pl executables: [] extensions: [] extra_rdoc_files: [] files: - ".gitignore" - Gemfile - LICENSE.txt - README.rdoc - Rakefile - lib/magic.rb - lib/magic/api.rb - lib/magic/constants.rb - lib/magic/database.rb - lib/magic/errors.rb - lib/magic/version.rb - magic.gemspec - test/fixtures/filelogo.jpg - test/fixtures/magic.txt - test/fixtures/magic_empty - test/fixtures/magic_jpeg - test/helper.rb - test/test_magic.rb homepage: https://github.com/qoobaa/magic licenses: - MIT metadata: {} post_install_message: | +-NOTE FOR LINUX USERS----------------------------------------------+ | | | Install libmagic using your package manager, e.g. | | | | sudo apt-get install file | | | +-NOTE FOR WINDOWS USERS -------------------------------------------+ | | | Install File for Windows from | | | | http://gnuwin32.sourceforge.net/packages/file.htm | | | | You'll also need to set your PATH environment variable to the | | directory of the magic1.dll file | | | | set PATH=C:\Program Files\GnuWin32\bin;%PATH% | | | +-NOTE FOR MAC OS USERS --------------------------------------------+ | | | If you don't have libmagic.1.dylib file in your system, you need | | to install it using port command | | | | sudo port install file | | | +-------------------------------------------------------------------+ rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: rubygems_version: 2.4.5 signing_key: specification_version: 4 summary: Determine file type and encoding using "magic" numbers test_files: - test/fixtures/filelogo.jpg - test/fixtures/magic.txt - test/fixtures/magic_empty - test/fixtures/magic_jpeg - test/helper.rb - test/test_magic.rb magic-0.2.9/test/0000755000004100000410000000000012530735146013617 5ustar www-datawww-datamagic-0.2.9/test/helper.rb0000644000004100000410000000046012530735146015423 0ustar www-datawww-datarequire "rubygems" gem "test-unit" require "test/unit" $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib")) $LOAD_PATH.unshift(File.dirname(__FILE__)) require "magic" class Test::Unit::TestCase def fixture(filename) File.join(File.dirname(__FILE__), "fixtures", filename) end end magic-0.2.9/test/fixtures/0000755000004100000410000000000012530735146015470 5ustar www-datawww-datamagic-0.2.9/test/fixtures/magic_empty0000644000004100000410000000004712530735146017712 0ustar www-datawww-data# This is an empty magic database file magic-0.2.9/test/fixtures/filelogo.jpg0000644000004100000410000001272712530735146020003 0ustar www-datawww-dataJFIFHHC    $.' ",#(7),01444'9=82<.342C  2!!22222222222222222222222222222222222222222222222222J"A!"1AQ2aq#r35R$4BCcs%DTb+!"123Aq#BQ4Ca ?!xM/%-2FJjr--P.\:䎝 mX<ԭGقGT@l/C%4uar:tju7]l!R@!@!FD\*Κׂa~*6ul]4 J sFΔ.YުVT%q &ue':E‰YDfTlWB&T8H7^9!A G,,uSUϔRuY. M(j+$m&W䵬ht^ J&:W;Z<2F(GVr+<*a%ۢkeC85uU&kb*weg> f=8\.#Q3398p-̗܆']՚qU,vn:5Xߤn/Y}Khⶍ'T>7=& ّf+1,%KNfGR~qMc%Vp5Z>%:jiBq\-j(D3ݘd*at&6OLl|#diX*3TUMFPuXUuUj+lM++UV1Dͤhܱ*Jx H7+\{pfnZF/NgⱡU$BlӶOQZ8=l S3q QO d-GkP܋My3F\:M#- f(VAE\I(2V3`ѐ$ G#LEWkWac $4P.OOXX>cIm ~jpn=-N`5Zq>nOf !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|OчEhl"謮Knəs~K01##c_%4h.y'u J!n&;PSE R+rS } Eh଴X 6jFg-' !B!B!B!B!B!B@Yz B B,@X/P!@magic-0.2.9/test/fixtures/magic.txt0000644000004100000410000000001712530735146017307 0ustar www-datawww-dataMagic® File™magic-0.2.9/test/fixtures/magic_jpeg0000644000004100000410000000242112530735146017477 0ustar www-datawww-data#------------------------------------------------------------------------------ # JPEG images # SunOS 5.5.1 had # # 0 string \377\330\377\340 JPEG file # 0 string \377\330\377\356 JPG file # # both of which turn into "JPEG image data" here. # 0 beshort 0xffd8 JPEG image data !:mime image/jpeg !:apple 8BIMJPEG !:strength +1 >6 string JFIF \b, JFIF standard # The following added by Erik Rossen 1999-09-06 # in a vain attempt to add image size reporting for JFIF. Note that these # tests are not fool-proof since some perfectly valid JPEGs are currently # impossible to specify in magic(4) format. # First, a little JFIF version info: >>11 byte x \b %d. >>12 byte x \b%02d # Next, the resolution or aspect ratio of the image: #>>13 byte 0 \b, aspect ratio #>>13 byte 1 \b, resolution (DPI) #>>13 byte 2 \b, resolution (DPCM) #>>4 beshort x \b, segment length %d # Next, show thumbnail info, if it exists: >>18 byte !0 \b, thumbnail %dx >>>19 byte x \b%d magic-0.2.9/test/test_magic.rb0000644000004100000410000000335512530735146016271 0ustar www-datawww-data# encoding: UTF-8 require "helper" class TestMagic < Test::Unit::TestCase test "guess magic.txt mime" do assert_equal "text/plain; charset=utf-8", Magic.guess_file_mime(fixture("magic.txt")) end test "guess magic.txt mime type" do assert_equal "text/plain", Magic.guess_file_mime_type(fixture("magic.txt")) end test "guess magic.txt mime encoding" do assert_equal "utf-8", Magic.guess_file_mime_encoding(fixture("magic.txt")) end test "guess filelogo.jpg mime" do assert_equal "image/jpeg; charset=binary", Magic.guess_file_mime(fixture("filelogo.jpg")) end test "guess filelogo.jpg mime type" do assert_equal "image/jpeg", Magic.guess_file_mime_type(fixture("filelogo.jpg")) end test "guess filelogo.jpg mime encoding" do assert_equal "binary", Magic.guess_file_mime_encoding(fixture("filelogo.jpg")) end test "guess non-existing file mime" do assert_raises Errno::ENOENT do Magic.guess_file_mime(fixture("non-existing.file")) end end test "guess filelogo.jpg mime with magic_jpeg database" do assert_equal "image/jpeg; charset=binary", Magic.guess_file_mime(fixture("filelogo.jpg"), :database => fixture("magic_jpeg")) end test "guess filelogo.jpg mime with empty database" do assert_equal "application/octet-stream; charset=binary", Magic.guess_file_mime(fixture("filelogo.jpg"), :database => fixture("magic_empty")) end test "guess with block" do result = nil Magic.guess(:mime) { |db| result = db.file(fixture("filelogo.jpg")) } assert_equal "image/jpeg; charset=binary", result end test "guess encoding from string" do utf8 = "utf-8".force_encoding(Encoding::ASCII_8BIT) assert_equal utf8, Magic.guess_string_mime_encoding('áéíóú') end end magic-0.2.9/.gitignore0000644000004100000410000000023212530735146014625 0ustar www-datawww-data*.gem *.rbc .bundle .config .yardoc Gemfile.lock InstalledFiles _yardoc coverage doc/ lib/bundler/man pkg rdoc spec/reports test/tmp test/version_tmp tmp