Skip to content

Commit

Permalink
Make VirusScanner available as a service c.f. S3Storage
Browse files Browse the repository at this point in the history
Now that the VirusScanner class is a stateless service, we can include
it in the standard Services module to bring this app more into line with
other GOV.UK apps.
  • Loading branch information
floehopper committed Dec 29, 2017
1 parent 5ed5625 commit 674e553
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
5 changes: 2 additions & 3 deletions app/workers/virus_scan_worker.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
require 'virus_scanner'
require 'services'

class VirusScanWorker
include Sidekiq::Worker

def perform(asset_id)
asset = Asset.find(asset_id)
scanner = VirusScanner.new
scanner.scan(asset.file.path)
Services.virus_scanner.scan(asset.file.path)
asset.scanned_clean
rescue VirusScanner::InfectedFile => e
GovukError.notify(e, extra: { id: asset.id, filename: asset.filename })
Expand Down
5 changes: 5 additions & 0 deletions lib/services.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
require 's3_storage'
require 'virus_scanner'

module Services
def self.cloud_storage
@cloud_storage ||= S3Storage.build
end

def self.virus_scanner
@virus_scanner ||= VirusScanner.new
end
end
3 changes: 2 additions & 1 deletion spec/workers/virus_scan_worker_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
require 'rails_helper'
require 'services'

RSpec.describe VirusScanWorker do
let(:worker) { described_class.new }
let(:asset) { FactoryBot.create(:asset) }
let(:scanner) { instance_double('VirusScanner') }

before do
allow(VirusScanner).to receive(:new).and_return(scanner)
allow(Services).to receive(:virus_scanner).and_return(scanner)
end

it "calls out to the VirusScanner to scan the file" do
Expand Down

0 comments on commit 674e553

Please sign in to comment.