mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 16:34:31 +08:00
DEV: Recover missing files of existing uploads (#10757)
UploadRecovery only worked on missing Upload records. Now it also works with existing ones that have an invalid_etag status.
The specs (first that test the S3 path) are a bit of stub-a-palooza, but that's how much this class interacts with the the outside world. 🤷♂️
This commit is contained in:
@ -21,9 +21,7 @@ RSpec.describe UploadRecovery do
|
||||
|
||||
let(:post) do
|
||||
Fabricate(:post,
|
||||
raw: <<~SQL,
|
||||

|
||||
SQL
|
||||
raw: "",
|
||||
user: user
|
||||
).tap(&:link_post_uploads)
|
||||
end
|
||||
@ -115,6 +113,77 @@ RSpec.describe UploadRecovery do
|
||||
.to eq(File.read(file_from_fixtures("smallest.png")))
|
||||
end
|
||||
|
||||
context 'S3 store' do
|
||||
before do
|
||||
setup_s3
|
||||
stub_s3_store
|
||||
end
|
||||
|
||||
it 'recovers the upload' do
|
||||
expect do
|
||||
upload.destroy!
|
||||
end.to change { post.reload.uploads.count }.from(1).to(0)
|
||||
|
||||
original_key = Discourse.store.get_path_for_upload(upload)
|
||||
tombstone_key = original_key.sub("original", "tombstone/original")
|
||||
|
||||
tombstone_copy = stub
|
||||
tombstone_copy.expects(:key).returns(tombstone_key)
|
||||
|
||||
Discourse.store.s3_helper.expects(:list).with("original").returns([])
|
||||
Discourse.store.s3_helper.expects(:list).with("#{FileStore::S3Store::TOMBSTONE_PREFIX}original").returns([tombstone_copy])
|
||||
Discourse.store.s3_helper.expects(:copy).with(tombstone_key, original_key, options: { acl: "public-read" })
|
||||
|
||||
FileHelper.expects(:download).returns(file_from_fixtures("smallest.png"))
|
||||
stub_request(:get, upload.url).to_return(body: file_from_fixtures("smallest.png"))
|
||||
|
||||
expect do
|
||||
upload_recovery.recover
|
||||
end.to change { post.reload.uploads.count }.from(0).to(1)
|
||||
end
|
||||
|
||||
describe 'when the upload exists but its file is missing' do
|
||||
it 'recovers the file' do
|
||||
upload.verification_status = Upload.verification_statuses[:invalid_etag]
|
||||
upload.save!
|
||||
|
||||
original_key = Discourse.store.get_path_for_upload(upload)
|
||||
tombstone_key = original_key.sub("original", "tombstone/original")
|
||||
|
||||
tombstone_copy = stub
|
||||
tombstone_copy.expects(:key).returns(tombstone_key)
|
||||
|
||||
Discourse.store.s3_helper.expects(:list).with("original").returns([])
|
||||
Discourse.store.s3_helper.expects(:list).with("#{FileStore::S3Store::TOMBSTONE_PREFIX}original").returns([tombstone_copy])
|
||||
Discourse.store.s3_helper.expects(:copy).with(tombstone_key, original_key, options: { acl: "public-read" })
|
||||
|
||||
expect do
|
||||
upload_recovery.recover
|
||||
end.to_not change { [post.reload.uploads.count, Upload.count] }
|
||||
end
|
||||
|
||||
it 'does not create a duplicate upload when secure uploads are enabled' do
|
||||
SiteSetting.secure_media = true
|
||||
upload.verification_status = Upload.verification_statuses[:invalid_etag]
|
||||
upload.save!
|
||||
|
||||
original_key = Discourse.store.get_path_for_upload(upload)
|
||||
tombstone_key = original_key.sub("original", "tombstone/original")
|
||||
|
||||
tombstone_copy = stub
|
||||
tombstone_copy.expects(:key).returns(tombstone_key)
|
||||
|
||||
Discourse.store.s3_helper.expects(:list).with("original").returns([])
|
||||
Discourse.store.s3_helper.expects(:list).with("#{FileStore::S3Store::TOMBSTONE_PREFIX}original").returns([tombstone_copy])
|
||||
Discourse.store.s3_helper.expects(:copy).with(tombstone_key, original_key, options: { acl: "public-read" })
|
||||
|
||||
expect do
|
||||
upload_recovery.recover
|
||||
end.to_not change { [post.reload.uploads.count, Upload.count] }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'image tag' do
|
||||
let(:post) do
|
||||
Fabricate(:post,
|
||||
|
Reference in New Issue
Block a user