Class: Hyrax::VirusScanner

Inherits:
Object
  • Object
show all
Defined in:
app/models/hyrax/virus_scanner.rb

Overview

The default virus scanner ported from Hyrax::Works.

If ClamAV is present, it will be used to check for the presence of a virus. If ClamAV is not installed or otherwise not available to your application, Hyrax::Works does no virus checking add assumes files have no viruses.

Examples:

to use a virus checker other than Hyrax::VirusScanner:

class MyScanner < Hyrax::Works::VirusScanner
  def infected?
    my_result = Scanner.check_for_viruses(file)
    [return true or false]
  end
end

# Then set Hyrax::Works to use your scanner either in a config file or initializer:
Hyrax.config.virus_scanner = MyScanner

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ VirusScanner

Returns a new instance of VirusScanner.



32
33
34
# File 'app/models/hyrax/virus_scanner.rb', line 32

def initialize(file)
  @file = file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



23
24
25
# File 'app/models/hyrax/virus_scanner.rb', line 23

def file
  @file
end

Class Method Details

.infected?(file) ⇒ Boolean

Parameters:

  • file (String)

Returns:

  • (Boolean)


28
29
30
# File 'app/models/hyrax/virus_scanner.rb', line 28

def self.infected?(file)
  new(file).infected?
end

Instance Method Details

#clam_av_scannerObject



44
45
46
47
48
49
# File 'app/models/hyrax/virus_scanner.rb', line 44

def clam_av_scanner
  scan_result = ClamAV.instance.method(:scanfile).call(file)
  return false if scan_result.zero?
  warning "A virus was found in #{file}: #{scan_result}"
  true
end

#infected?Boolean

Note:

Override this method to use your own virus checking software

Returns:

  • (Boolean)


40
41
42
# File 'app/models/hyrax/virus_scanner.rb', line 40

def infected?
  defined?(ClamAV) ? clam_av_scanner : null_scanner
end

#null_scannerObject

Always return zero if there’s nothing available to check for viruses. This means that we assume all files have no viruses because we can’t conclusively say if they have or not.



55
56
57
58
59
# File 'app/models/hyrax/virus_scanner.rb', line 55

def null_scanner
  warning "Unable to check #{file} for viruses because no virus scanner is defined" unless
    Rails.env.test?
  false
end