class MagicGzipValidator

Constants

VALID_STARTING_SIGNATURE

Attributes

file[R]
starting_signature[R]

Public Class Methods

new(file) click to toggle source
# File lib/logstash/inputs/mime/magic_gzip_validator.rb, line 7
def initialize(file)
  raise "Expecting a file object as an argument" unless file.is_a?(File)

  # Ensure there are sufficient number of bytes to determine the
  # signature.
  if file.stat.size < minimum_bytes_for_determining_signature
    puts "File too small to calculate signature"
    return false
  end

  @file = file
  process_file!
end

Public Instance Methods

starting_signature_bytes() click to toggle source
# File lib/logstash/inputs/mime/magic_gzip_validator.rb, line 21
def starting_signature_bytes
  2
end
valid?() click to toggle source
# File lib/logstash/inputs/mime/magic_gzip_validator.rb, line 25
def valid?
  @starting_signature == VALID_STARTING_SIGNATURE
end

Private Instance Methods

minimum_bytes_for_determining_signature() click to toggle source
# File lib/logstash/inputs/mime/magic_gzip_validator.rb, line 30
def minimum_bytes_for_determining_signature
  starting_signature_bytes
end
process_file!() click to toggle source
# File lib/logstash/inputs/mime/magic_gzip_validator.rb, line 34
def process_file!
  read_starting_signature!

  # Ensure the file is closed after reading the starting signiture
  # bytes
  @file.close
end
read_starting_signature!() click to toggle source
# File lib/logstash/inputs/mime/magic_gzip_validator.rb, line 42
def read_starting_signature!
  @file.rewind
  starting_bytes = @file.readpartial(starting_signature_bytes)
  @starting_signature = starting_bytes.unpack("H*").first
end