demarches-normaliennes/app/lib/active_storage/virus_scanner.rb

55 lines
948 B
Ruby
Raw Normal View History

# frozen_string_literal: true
class ActiveStorage::VirusScanner
def initialize(blob)
@blob = blob
end
attr_reader :blob
PENDING = 'pending'
INFECTED = 'infected'
SAFE = 'safe'
INTEGRITY_ERROR = 'integrity_error'
def pending?
virus_scan_result == PENDING
end
def infected?
virus_scan_result == INFECTED
end
def safe?
virus_scan_result == SAFE
end
def corrupt?
virus_scan_result == INTEGRITY_ERROR
end
def done?
started? && virus_scan_result != PENDING
end
def started?
virus_scan_result.present?
end
def attributes
2020-01-28 12:02:06 +01:00
blob.open do |file|
if ClamavService.safe_file?(file.path)
{ virus_scan_result: SAFE, virus_scanned_at: Time.zone.now }
else
{ virus_scan_result: INFECTED, virus_scanned_at: Time.zone.now }
end
end
end
private
def virus_scan_result
blob.virus_scan_result || blob.metadata[:virus_scan_result]
end
end