Sha256: b2e801bb69c53e33f38e8a94e5305056d37b9d4f0fafcf0d6026e6fba04c4968

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module Hydra::Works
  module VirusCheck
    extend ActiveSupport::Concern

    included do
      validate :detect_viruses
    end

    # Default behavior is to raise a validation error and halt the save if a virus is found
    def detect_viruses
      return unless original_file && original_file.new_record?

      path = original_file.is_a?(String) ? original_file : local_path_for_file(original_file)
      unless defined?(ClamAV)
        warn "Virus checking disabled, #{path} not checked"
        return
      end

      scan_result = ClamAV.instance.scanfile(path)
      if scan_result == 0
        true
      else
        virus_message = "A virus was found in #{path}: #{scan_result}"
        warn(virus_message)
        errors.add(:base, virus_message)
        false
      end
    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)
        if file.respond_to?(:path)
          file.path
        else
          Tempfile.open('') do |t|
            t.binmode
            t.write(file)
            t.close
            t.path
          end
        end
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hydra-works-0.3.0 lib/hydra/works/models/concerns/file_set/virus_check.rb