25 lines
595 B
Ruby
Raw Normal View History

2016-09-24 13:52:43 +02:00
module Hbc
class Container
class Criteria
attr_reader :path, :command
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def initialize(path, command)
@path = path
@command = command
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def extension(regex)
path.extname.sub(/^\./, "") =~ Regexp.new(regex.source, regex.options | Regexp::IGNORECASE)
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
2016-09-24 13:52:43 +02:00
def magic_number(regex)
2017-08-08 18:10:01 +02:00
return false if path.directory?
2016-09-24 13:52:43 +02:00
# 262: length of the longest regex (currently: Hbc::Container::Tar)
2017-08-08 18:10:01 +02:00
@magic_number ||= File.open(path, "rb") { |f| f.read(262) }
@magic_number.match?(regex)
2016-09-24 13:52:43 +02:00
end
end
2016-08-18 22:11:42 +03:00
end
end