diff --git a/app/jobs/attach_files_to_work_job.rb b/app/jobs/attach_files_to_work_job.rb index e9cf3619c9..4ebb8a303e 100644 --- a/app/jobs/attach_files_to_work_job.rb +++ b/app/jobs/attach_files_to_work_job.rb @@ -10,9 +10,33 @@ def perform(work, uploaded_files) user = User.find_by_user_key(work.depositor) actor = CurationConcerns::Actors::FileSetActor.new(file_set, user) actor.create_metadata(work, visibility: work.visibility) - actor.create_content(uploaded_file.file.file.to_file) - # Set the uri so that we know this uploaded file has been used. + + attach_content(actor, uploaded_file.file) uploaded_file.update(file_set_uri: file_set.uri) end end + + private + + # @param [CurationConcerns::Actors::FileSetActor] actor + # @param [UploadedFileUploader] file + def attach_content(actor, file) + case file.file + when CarrierWave::SanitizedFile + actor.create_content(file.file.to_file) + when CarrierWave::Storage::Fog::File + import_url(actor, file) + else + raise ArgumentError, "Unknown type of file #{file.class}" + end + end + + # @param [CurationConcerns::Actors::FileSetActor] actor + # @param [UploadedFileUploader] file + def import_url(actor, file) + actor.file_set.update(import_url: file.url) + log = CurationConcerns::Operation.create!(user: actor.user, + operation_type: "Attach File") + ImportUrlJob.perform_later(actor.file_set, log) + end end diff --git a/spec/jobs/attach_files_to_work_job_spec.rb b/spec/jobs/attach_files_to_work_job_spec.rb index 41b62bf0cb..52ddefc348 100644 --- a/spec/jobs/attach_files_to_work_job_spec.rb +++ b/spec/jobs/attach_files_to_work_job_spec.rb @@ -8,13 +8,42 @@ let(:uploaded_file2) { Sufia::UploadedFile.create(file: file2) } let(:generic_work) { create(:public_generic_work) } - it "attaches files, copies visibility and updates the uploaded files" do - expect(CharacterizeJob).to receive(:perform_later).twice - described_class.perform_now(generic_work, [uploaded_file1, uploaded_file2]) - generic_work.reload - expect(generic_work.file_sets.count).to eq 2 - expect(generic_work.file_sets.map(&:visibility)).to all(eq 'open') - expect(uploaded_file1.reload.file_set_uri).not_to be_nil + context "with uploaded files on the filesystem" do + it "attaches files, copies visibility and updates the uploaded files" do + expect(CharacterizeJob).to receive(:perform_later).twice + described_class.perform_now(generic_work, [uploaded_file1, uploaded_file2]) + generic_work.reload + expect(generic_work.file_sets.count).to eq 2 + expect(generic_work.file_sets.map(&:visibility)).to all(eq 'open') + expect(uploaded_file1.reload.file_set_uri).not_to be_nil + end + end + + context "with uploaded files in fog" do + let(:fog_file) { CarrierWave::Storage::Fog::File.new } + before do + module CarrierWave::Storage + module Fog + class File + end + end + end + allow(uploaded_file1.file).to receive(:file).and_return(fog_file) + allow(uploaded_file2.file).to receive(:file).and_return(fog_file) + end + + after do + CarrierWave::Storage.send(:remove_const, :Fog) + end + + it 'creates ImportUrlJobs' do + expect(ImportUrlJob).to receive(:perform_later).twice + described_class.perform_now(generic_work, [uploaded_file1, uploaded_file2]) + generic_work.reload + expect(generic_work.file_sets.count).to eq 2 + expect(generic_work.file_sets.map(&:visibility)).to all(eq 'open') + expect(uploaded_file1.reload.file_set_uri).not_to be_nil + end end end end