Sha256: e846a1aa181ea1d60fb7399da855bc37e4e5cbc4dd360395236c06c3ebae090a

Contents?: true

Size: 1.58 KB

Versions: 8

Compression:

Stored size: 1.58 KB

Contents

module Hydra::Works
  # Responsible for checking if the given file is a virus. Coordinates
  # with the underlying system virus scanner.
  class VirusCheckerService
    attr_accessor :original_file, :system_virus_scanner

    # @api public
    # @param original_file [String, #path]
    # @return true or false result from system_virus_scanner
    def self.file_has_virus?(original_file)
      new(original_file).file_has_virus?
    end

    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

    # Default behavior is to raise a validation error and halt the save if a virus is found
    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

      # Returns a path for reading the content of +file+
      # @param [File] file object to retrieve a path for
      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

      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
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
hydra-works-2.2.0 lib/hydra/works/services/virus_checker_service.rb
hydra-works-2.1.0 lib/hydra/works/services/virus_checker_service.rb
hydra-works-2.0.0 lib/hydra/works/services/virus_checker_service.rb
hydra-works-1.2.0 lib/hydra/works/services/virus_checker_service.rb
hydra-works-1.1.0 lib/hydra/works/services/virus_checker_service.rb
hydra-works-1.0.0 lib/hydra/works/services/virus_checker_service.rb
hydra-works-0.17.0 lib/hydra/works/services/virus_checker_service.rb
hydra-works-0.16.0 lib/hydra/works/services/virus_checker_service.rb