Class: AntivirusValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/antivirus_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/validators/antivirus_validator.rb', line 4

def validate_each(record, attribute, value)
  # Avoid unnecessary scans in case
  # a) the storage backend does not accept the resource
  # b) the attribute has not changed
  storage = Ratonvirus.storage
  return unless storage.accept?(value)
  return unless storage.changed?(record, attribute)

  # Only scan if the scanner is available
  scanner = Ratonvirus.scanner
  return unless scanner.available?

  return unless scanner.virus?(value)

  if scanner.errors.any?
    scanner.errors.each do |err|
      record.errors.add attribute, err
    end
  else
    record.errors.add attribute, :antivirus_virus_detected
  end
end