Skip to content

Commit

Permalink
Ensure asset metadata is updated when file name isn't changed
Browse files Browse the repository at this point in the history
Asset metadata is not being updated when an asset is uploaded with a file which
has the same name as the previous file. This is because of a condition added in
#1258 that was intended to quieten some errors we were seeing at the time. This
same issue was addressed in #1263.

This is problematic as there is a guard statement in the S3 uploader that
prevents assets with an unchanged MD5 hexdigest from uploading.
  • Loading branch information
jkempster34 committed Mar 18, 2024
1 parent 6c583bc commit b4edc19
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Asset

mount_uploader :file, AssetUploader

before_save :store_metadata, unless: :uploaded?, if: -> { changes.include?(:file) }
before_save :store_metadata, unless: :uploaded?
after_save :schedule_virus_scan
after_save :update_indirect_replacements_on_publish
after_save :backpropagate_replacement
Expand Down
13 changes: 13 additions & 0 deletions spec/models/asset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,19 @@
expect(asset.save).to be_truthy
end
end

context "when the file name is unchanged" do
let(:saved_asset) { FactoryBot.create(:asset) }
let(:md5_hexdigest) { saved_asset.md5_hexdigest }
let(:original_filename) { saved_asset.file.send(:original_filename) }

it "updates the md5 hexdigest" do
asset = described_class.find(saved_asset.id) # find to clear memoization
asset.update!(file: load_fixture_file("lorem.txt", named: original_filename))

expect(asset.md5_hexdigest).not_to eq md5_hexdigest
end
end
end

describe "#replacement" do
Expand Down

0 comments on commit b4edc19

Please sign in to comment.