Sha256: e498bf495e103011f38103f30ec7e2fe42bf6affa73a3c68cc7e2297bb5c3549

Contents?: true

Size: 1.27 KB

Versions: 7

Compression:

Stored size: 1.27 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)
        warning "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}"
        warning(virus_message)
        errors.add(:base, virus_message)
        false
      end
    end

    private

      def warning(msg)
        ActiveFedora::Base.logger.warn msg if ActiveFedora::Base.logger
      end

      # 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

7 entries across 7 versions & 1 rubygems

Version Path
hydra-works-0.8.1 lib/hydra/works/models/concerns/file_set/virus_check.rb
hydra-works-0.8 lib/hydra/works/models/concerns/file_set/virus_check.rb
hydra-works-0.7.1 lib/hydra/works/models/concerns/file_set/virus_check.rb
hydra-works-0.7.0 lib/hydra/works/models/concerns/file_set/virus_check.rb
hydra-works-0.6.0 lib/hydra/works/models/concerns/file_set/virus_check.rb
hydra-works-0.5.0 lib/hydra/works/models/concerns/file_set/virus_check.rb
hydra-works-0.4.0 lib/hydra/works/models/concerns/file_set/virus_check.rb