Module: MagicBytes

Extended by:
MagicBytes
Included in:
MagicBytes
Defined in:
lib/magic_bytes.rb

Defined Under Namespace

Classes: FileType

Constant Summary collapse

VERSION =
'1.0.1'

Instance Method Summary collapse

Instance Method Details

#detect(header_bytes) ⇒ Hash?

This is a line-for-line port of github.com/sindresorhus/file-type which is more than sufficient for our purposes

Parameters:

  • header_bytes (String)

    the header bytes of the file

Returns:

  • (Hash, nil)

    the hash of ext: and mime: or nil if the type could not be deduced



23
24
25
26
# File 'lib/magic_bytes.rb', line 23

def detect(header_bytes)
  d = _detect(header_bytes)
  FileType.new(d.fetch(:ext), d.fetch(:mime))
end

#read_and_detect(io) ⇒ Hash?

Performs detection from a given IO or File.

Parameters:

  • io (#read)

    a readable object

Returns:

  • (Hash, nil)

    the hash of ext: and mime: or nil if the type could not be deduced



13
14
15
16
# File 'lib/magic_bytes.rb', line 13

def read_and_detect(io)
  n_bytes = 262 # The maximum length supported (needed for .tar archives)
  detect(io.read(n_bytes))
end