1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
# frozen_string_literal: true
begin
::RUBY_VERSION =~ /(\d+\.\d+)/
require_relative "magic/#{Regexp.last_match(1)}/magic"
rescue LoadError
# use "require" instead of "require_relative" because non-native gems will place C extension files
# in Gem::BasicSpecification#extension_dir after compilation (during normal installation), which
# is in $LOAD_PATH but not necessarily relative to this file (see #21 and nokogiri#2300)
require 'magic/magic'
end
require_relative 'magic/version'
require_relative 'magic/core/file'
require_relative 'magic/core/string'
#
# File _Magic_ in Ruby.
#
# Simple interface to _libmagic_ for Ruby Programming Language.
#
class Magic
#
# call-seq:
# magic.inspect -> string
#
# Example:
#
# magic = Magic.new
# magic.inspect #=> "#<Magic:0x007fd5258a1108 @flags=0, @paths=[\"/etc/magic\", \"/usr/share/misc/magic\"]>"
# magic.close
# magic.inspect #=> "#<Magic:0x007fd5258a1108 @flags=0, @paths=[\"/etc/magic\", \"/usr/share/misc/magic\"] (closed)>"
#
def inspect
super.insert(-2, self.closed? ? ' (closed)' : '')
end
#
# call-seq:
# magic.flags_list( boolean ) -> array
#
# See also: Magic#flags and Magic#flags_names
#
def flags_list(names = false)
raise LibraryError, 'Magic library is not open' if closed?
return [names ? 'NONE' : 0] if @flags.zero?
n, i = 0, @flags
flags = []
@@flags_map ||= flags_as_map if names
while i > 0
n = 2 ** (Math.log(i) / Math.log(2)).to_i
i = i - n
flags.insert(0, names ? @@flags_map[n] : n)
end
flags
end
alias_method :flags_to_a, :flags_list
# call-seq:
# magic.flags_names -> array
#
# See also: Magic#flags and Magic#flags_list
#
def flags_names
flags_list(true)
end
class << self
#
# call-seq:
# Magic.open( integer ) -> self
# Magic.open( integer ) {|magic| block } -> string or array
#
# See also: Magic::mime, Magic::type, Magic::encoding, Magic::compile and Magic::check
#
def open(flags = Magic::NONE)
magic = Magic.new
magic.flags = flags
if block_given?
begin
yield magic
ensure
magic.close
end
else
magic
end
end
#
# call-seq:
# Magic.mime -> self
# Magic.mime {|magic| block } -> string or array
#
# See also: Magic::open, Magic::type, Magic::encoding, Magic::compile and Magic::check
#
def mime(&block)
open(Magic::MIME, &block)
end
alias_method :mime_type, :mime
#
# call-seq:
# Magic.type -> self
# Magic.type {|magic| block } -> string or array
#
# See also: Magic::open, Magic::mime, Magic::encoding, Magic::compile and Magic::check
#
def type(&block)
open(Magic::MIME_TYPE, &block)
end
#
# call-seq:
# Magic.encoding -> self
# Magic.encoding {|magic| block } -> string or array
#
# See also: Magic::open, Magic::mime, Magic::type, Magic::compile and Magic::check
#
def encoding(&block)
open(Magic::MIME_ENCODING, &block)
end
#
# call-seq:
# Magic.compile( string, ... ) -> true
# Magic.compile( array ) -> true
#
# See also: Magic::open, Magic::mime, Magic::type, Magic::encoding, and Magic::check
#
def compile(*paths)
open {|m| m.compile(paths) }
end
#
# call-seq:
# Magic.check( string, ... ) -> true or false
# Magic.check( array ) -> true or false
#
# See also: Magic::open, Magic::mime, Magic::type, Magic::encoding and Magic::compile
#
def check(*paths)
open {|m| m.check(paths) }
end
#
# call-seq:
# Magic.file( object ) -> string or array
# Magic.file( string ) -> string or array
# Magic.file( string, integer ) -> string or array
#
# See also:
#
def file(path, flags = Magic::MIME)
open(flags).file(path)
end
#
# call-seq:
# Magic.buffer( string ) -> string or array
# Magic.buffer( string, integer ) -> string or array
#
# See also:
#
def buffer(buffer, flags = Magic::MIME)
open(flags).buffer(buffer)
end
#
# call-seq:
# Magic.descriptor( object ) -> string or array
# Magic.descriptor( integer ) -> string or array
# Magic.descriptor( integer, integer ) -> string or array
#
# See also:
#
def descriptor(fd, flags = Magic::MIME)
open(flags).descriptor(fd)
end
alias_method :fd, :descriptor
private
def default_paths
paths = Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), "../ext/magic/share/*.mgc")))
paths.empty? ? nil : paths
end
end
private
def power_of_two?(number)
number > 0 && Math.log2(number) % 1 == 0
end
def flags_as_map
klass = self.class
klass.constants.each_with_object({}) do |constant, flags|
constant = constant.to_s
next if constant.start_with?('PARAM_')
value = klass.const_get(constant)
if value.is_a?(Integer) && power_of_two?(value)
flags[value] = constant
end
end
end
end
FileMagic = Magic
|