2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-05-02 11:37:35 +02:00
|
|
|
class ActiveStorage::VirusScanner
|
|
|
|
def initialize(blob)
|
|
|
|
@blob = blob
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :blob
|
|
|
|
|
|
|
|
PENDING = 'pending'
|
|
|
|
INFECTED = 'infected'
|
|
|
|
SAFE = 'safe'
|
2021-04-06 14:59:12 +02:00
|
|
|
INTEGRITY_ERROR = 'integrity_error'
|
2019-05-02 11:37:35 +02:00
|
|
|
|
|
|
|
def pending?
|
2022-12-22 18:20:58 +01:00
|
|
|
virus_scan_result == PENDING
|
2019-05-02 11:37:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def infected?
|
2022-12-22 18:20:58 +01:00
|
|
|
virus_scan_result == INFECTED
|
2019-05-02 11:37:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def safe?
|
2022-12-22 18:20:58 +01:00
|
|
|
virus_scan_result == SAFE
|
2019-05-02 11:37:35 +02:00
|
|
|
end
|
|
|
|
|
2021-04-06 14:59:12 +02:00
|
|
|
def corrupt?
|
2022-12-22 18:20:58 +01:00
|
|
|
virus_scan_result == INTEGRITY_ERROR
|
2021-04-06 14:59:12 +02:00
|
|
|
end
|
|
|
|
|
2019-05-16 18:59:34 +02:00
|
|
|
def done?
|
2022-12-22 18:20:58 +01:00
|
|
|
started? && virus_scan_result != PENDING
|
2019-05-02 11:37:35 +02:00
|
|
|
end
|
|
|
|
|
2019-05-16 18:59:34 +02:00
|
|
|
def started?
|
2022-12-22 18:20:58 +01:00
|
|
|
virus_scan_result.present?
|
2019-05-02 11:37:35 +02:00
|
|
|
end
|
|
|
|
|
2022-12-22 18:20:58 +01:00
|
|
|
def attributes
|
2020-01-28 12:02:06 +01:00
|
|
|
blob.open do |file|
|
2019-05-16 18:59:34 +02:00
|
|
|
if ClamavService.safe_file?(file.path)
|
2022-12-22 18:20:58 +01:00
|
|
|
{ virus_scan_result: SAFE, virus_scanned_at: Time.zone.now }
|
2019-05-16 18:59:34 +02:00
|
|
|
else
|
2022-12-22 18:20:58 +01:00
|
|
|
{ virus_scan_result: INFECTED, virus_scanned_at: Time.zone.now }
|
2019-05-02 11:37:35 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-12-22 18:20:58 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def virus_scan_result
|
|
|
|
blob.virus_scan_result || blob.metadata[:virus_scan_result]
|
|
|
|
end
|
2019-05-02 11:37:35 +02:00
|
|
|
end
|