From 001172b75b0d16231a34b5f407440a91dab416f6 Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Wed, 10 Apr 2024 11:57:58 +0100 Subject: [PATCH] Use purge_later This uses the purge_later feature to delete attachments rather than using our own job and service. --- app/jobs/remove_malware_blob_job.rb | 8 ----- app/services/fetch_malware_scan_result.rb | 4 +-- app/services/remove_malware_blob.rb | 17 ----------- .../fetch_malware_scan_result_spec.rb | 4 +-- spec/services/remove_malware_blob_spec.rb | 29 ------------------- 5 files changed, 2 insertions(+), 60 deletions(-) delete mode 100644 app/jobs/remove_malware_blob_job.rb delete mode 100644 app/services/remove_malware_blob.rb delete mode 100644 spec/services/remove_malware_blob_spec.rb diff --git a/app/jobs/remove_malware_blob_job.rb b/app/jobs/remove_malware_blob_job.rb deleted file mode 100644 index 97fe0af0c8..0000000000 --- a/app/jobs/remove_malware_blob_job.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -class RemoveMalwareBlobJob < ApplicationJob - def perform(upload_id:) - upload = Upload.find(upload_id) - RemoveMalwareBlob.call(upload:) - end -end diff --git a/app/services/fetch_malware_scan_result.rb b/app/services/fetch_malware_scan_result.rb index fbad10c654..166c7d549f 100644 --- a/app/services/fetch_malware_scan_result.rb +++ b/app/services/fetch_malware_scan_result.rb @@ -26,9 +26,7 @@ def call upload.update!( malware_scan_result: malware_scan_result_from_response(response), ) - if upload.scan_result_suspect? - RemoveMalwareBlobJob.perform_later(upload_id: upload.id) - end + upload.attachment.purge_later if upload.scan_result_suspect? end else upload.update!(malware_scan_result: "error") diff --git a/app/services/remove_malware_blob.rb b/app/services/remove_malware_blob.rb deleted file mode 100644 index 1b0e887e52..0000000000 --- a/app/services/remove_malware_blob.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -class RemoveMalwareBlob - include ServicePattern - - attr_reader :upload - - def initialize(upload:) - @upload = upload - end - - def call - return unless upload.scan_result_suspect? - - upload.attachment.purge - end -end diff --git a/spec/services/fetch_malware_scan_result_spec.rb b/spec/services/fetch_malware_scan_result_spec.rb index a64758748e..4a542a19ed 100644 --- a/spec/services/fetch_malware_scan_result_spec.rb +++ b/spec/services/fetch_malware_scan_result_spec.rb @@ -56,9 +56,7 @@ end it "enqueues a job to remove the file" do - expect(RemoveMalwareBlobJob).to receive(:perform_later).with( - upload_id: upload.id, - ) + expect(ActiveStorage::PurgeJob).to receive(:perform_later) fetch_malware_scan_result end end diff --git a/spec/services/remove_malware_blob_spec.rb b/spec/services/remove_malware_blob_spec.rb deleted file mode 100644 index 138390d3ea..0000000000 --- a/spec/services/remove_malware_blob_spec.rb +++ /dev/null @@ -1,29 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -RSpec.describe RemoveMalwareBlob do - describe "#call" do - let(:result) { "suspect" } - let(:upload) { create(:upload, malware_scan_result: result) } - subject(:remove_malware_blob) { described_class.call(upload:) } - - it "removes the upload attachment" do - remove_malware_blob - - expect(upload.reload.attachment.filename).to be_nil - expect(upload.reload.attachment.download).to be_nil - end - - context "when the upload does not have a suspect scan result" do - let(:result) { "clean" } - - it "does not remove the malware blob" do - remove_malware_blob - - expect(upload.reload.attachment.filename).to be_present - expect(upload.reload.attachment.download).to be_present - end - end - end -end