Class: Vectory::FileMagic

Inherits:
Object
  • Object
show all
Defined in:
lib/vectory/file_magic.rb

Constant Summary collapse

EPS_30_MAGIC =
"\x25\x21\x50\x53\x2d\x41\x64\x6f\x62\x65\x2d\x33\x2e\x30" \
             "\x20\x45\x50\x53\x46\x2d\x33\x2e\x30"
.force_encoding("BINARY")
EPS_31_MAGIC =
"\x25\x21\x50\x53\x2d\x41\x64\x6f\x62\x65\x2d\x33\x2e\x31" \
             "\x20\x45\x50\x53\x46\x2d\x33\x2e\x30"
.force_encoding("BINARY")
PS_MAGIC =
"\x25\x21\x50\x53\x2d\x41\x64\x6f\x62\x65\x2d\x33\x2e\x30"
.force_encoding("BINARY")
EMF_MAGIC =
"\x01\x00\x00\x00".force_encoding("BINARY")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileMagic

Returns a new instance of FileMagic.



20
21
22
# File 'lib/vectory/file_magic.rb', line 20

def initialize(path)
  @path = path
end

Class Method Details

.detect(path) ⇒ Object



16
17
18
# File 'lib/vectory/file_magic.rb', line 16

def self.detect(path)
  new(path).detect
end

Instance Method Details

#detectObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vectory/file_magic.rb', line 24

def detect
  beginning = File.read(@path, 100, mode: "rb")

  eps_slice = beginning.byteslice(0, EPS_30_MAGIC.size)
  Vectory.ui.debug("File magic is '#{to_bytes(beginning)}' of '#{@path}'.")

  return :eps if [EPS_30_MAGIC, EPS_31_MAGIC].include?(eps_slice)

  ps_slice = beginning.byteslice(0, PS_MAGIC.size)
  return :ps if ps_slice == PS_MAGIC

  emf_slice = beginning.byteslice(0, EMF_MAGIC.size)
  return :emf if emf_slice == EMF_MAGIC

  return :svg if contain_svg_tag?
end