Skip to content

Commit

Permalink
Import s3 files if you are using fog
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed May 24, 2016
1 parent 3572c88 commit 26eed26
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
28 changes: 26 additions & 2 deletions app/jobs/attach_files_to_work_job.rb
Expand Up @@ -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
43 changes: 36 additions & 7 deletions spec/jobs/attach_files_to_work_job_spec.rb
Expand Up @@ -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

0 comments on commit 26eed26

Please sign in to comment.