Skip to content

Commit

Permalink
Allow blob to exists and reattach it
Browse files Browse the repository at this point in the history
Also remove add_post_curation_uploads as we should never be using it.  It would cause key conflicts all the time if we did, since the key does not include the bucket
  • Loading branch information
carolyncole committed Sep 1, 2022
1 parent 29e277a commit 8d67007
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
15 changes: 6 additions & 9 deletions app/models/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,15 +351,6 @@ def add_pre_curation_uploads(s3_file)
pre_curation_uploads << persisted
end

def add_post_curation_uploads(s3_file)
blob = s3_file_to_blob(s3_file)
persisted = ActiveStorage::Attachment.new(blob: blob, name: :post_curation_uploads)
persisted.record = self
persisted.save
persisted.reload
pre_curation_uploads << persisted
end

def post_curation_s3_resources
return [] unless accepted?

Expand Down Expand Up @@ -512,6 +503,12 @@ def save_new_attachments(new_attachments:)
end

def s3_file_to_blob(s3_file)
existing_blob = ActiveStorage::Blob.find_by(key: s3_file.filename)
if existing_blob.present?
Rails.logger.warn("There is a blob existing for #{s3_file.filename}, which we are not expecting! It will be reattached #{existing_blob.inspect}")
return existing_blob
end

params = { filename: s3_file.filename, content_type: "", byte_size: s3_file.size, checksum: s3_file.checksum }
blob = ActiveStorage::Blob.create_before_direct_upload!(**params)
blob.key = s3_file.filename
Expand Down
26 changes: 19 additions & 7 deletions spec/models/work_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -535,12 +535,6 @@
expect(work.pre_curation_uploads.count).to eq(1)
expect(work.pre_curation_uploads.first.key).to eq(key)
end

it "adds an s3 attachment" do
work.add_post_curation_uploads(s3_file)
expect(work.post_curation_uploads.count).to eq(1)
expect(work.post_curation_uploads.first.key).to eq(key)
end
end

describe "#save", mock_s3_query_service: false do
Expand Down Expand Up @@ -577,7 +571,6 @@
stub_request(:put, /#{bucket_url}/).to_return(status: 200)

work.complete_submission!(user)
work.save
work.reload
end

Expand All @@ -589,6 +582,25 @@
expect(work.pre_curation_uploads.last).to be_a(ActiveStorage::Attachment)
expect(work.pre_curation_uploads.last.key).to eq("SCoData_combined_v1_2020-07_datapackage.json")
end

context "a blob already exists for one of the files" do
let(:work) do
work = FactoryBot.create(:draft_work)
blob = work.send(:s3_file_to_blob, file1)
blob.save
work.save
work
end

it "finds the blob and attaches it as an ActiveStorage Attachments" do
expect(work.pre_curation_uploads).not_to be_empty
expect(work.pre_curation_uploads.length).to eq(2)
expect(work.pre_curation_uploads.first).to be_a(ActiveStorage::Attachment)
expect(work.pre_curation_uploads.first.key).to eq("SCoData_combined_v1_2020-07_README.txt")
expect(work.pre_curation_uploads.last).to be_a(ActiveStorage::Attachment)
expect(work.pre_curation_uploads.last.key).to eq("SCoData_combined_v1_2020-07_datapackage.json")
end
end
end
end
end

0 comments on commit 8d67007

Please sign in to comment.