class Hydra::Works::VirusCheckerService

Responsible for checking if the given file is a virus. Coordinates with the underlying system virus scanner.

Attributes

original_file[RW]
system_virus_scanner[RW]

Public Class Methods

file_has_virus?(original_file) click to toggle source

@api public @param original_file [String, path] @return true or false result from system_virus_scanner

# File lib/hydra/works/services/virus_checker_service.rb, line 10
def self.file_has_virus?(original_file)
  new(original_file).file_has_virus?
end
new(original_file, system_virus_scanner = Hydra::Works.default_system_virus_scanner) click to toggle source
# File lib/hydra/works/services/virus_checker_service.rb, line 14
def initialize(original_file, system_virus_scanner = Hydra::Works.default_system_virus_scanner)
  self.original_file = original_file
  self.system_virus_scanner = system_virus_scanner
end

Public Instance Methods

file_has_virus?() click to toggle source

Default behavior is to raise a validation error and halt the save if a virus is found

# File lib/hydra/works/services/virus_checker_service.rb, line 20
def file_has_virus?
  path = original_file.is_a?(String) ? original_file : local_path_for_file(original_file)
  system_virus_scanner.infected?(path)
end

Private Instance Methods

local_path_for_file(file) click to toggle source

Returns a path for reading the content of file @param [File] file object to retrieve a path for

# File lib/hydra/works/services/virus_checker_service.rb, line 29
def local_path_for_file(file)
  return file.path if file.respond_to?(:path)
  return file.content.path if file.content.respond_to?(:path)

  Tempfile.open('') do |t|
    t.binmode
    write_to_temp_file(file, t)
    t.close
    t.path
  end
end
write_to_temp_file(file, temp_file) click to toggle source
# File lib/hydra/works/services/virus_checker_service.rb, line 41
def write_to_temp_file(file, temp_file)
  if file.new_record?
    temp_file.write(file.content.read)
    file.content.rewind
  else
    file.stream.each do |chunk|
      temp_file.write(chunk)
    end
  end
end