Skip to content

A platform for community discussion. Free, open, simple.

FIX: Do not try to recover invalid `Upload#short_url` in `UploadRecovery`.

Guo Xiang Tan5eb65ad612ae
diff --git a/lib/upload_recovery.rb b/lib/upload_recovery.rb
index 70021d29ac4..8eb1e7725a8 100644
--- a/lib/upload_recovery.rb
+++ b/lib/upload_recovery.rb
@@ -34,9 +34,12 @@ class UploadRecovery
private
def recover_post_upload(post, short_url)
+ sha1 = Upload.sha1_from_short_url(short_url)
+ return unless sha1.present?
+
attributes = {
post: post,
- sha1: Upload.sha1_from_short_url(short_url)
+ sha1: sha1
}
if Discourse.store.external?
@@ -47,8 +50,6 @@ class UploadRecovery
end
def recover_from_local(post:, sha1:)
- return unless sha1.present?
-
public_path = Rails.root.join("public")
@paths ||= begin
diff --git a/spec/lib/upload_recovery_spec.rb b/spec/lib/upload_recovery_spec.rb
index 1d3f74a9d55..01334c5e7e5 100644
--- a/spec/lib/upload_recovery_spec.rb
+++ b/spec/lib/upload_recovery_spec.rb
@@ -18,31 +18,45 @@ RSpec.describe UploadRecovery do
).tap(&:link_post_uploads)
end
+ let(:upload_recovery) { UploadRecovery.new }
+
before do
SiteSetting.queue_jobs = false
end
describe '#recover' do
- it 'should recover the upload' do
- begin
- stub_request(:get, "http://test.localhost#{upload.url}")
- .to_return(status: 200)
-
- expect do
- upload.destroy!
- end.to change { post.reload.uploads.count }.from(1).to(0)
-
- expect do
- UploadRecovery.new.recover
- end.to change { post.reload.uploads.count }.from(0).to(1)
- ensure
- public_path = "#{Discourse.store.public_dir}#{upload.url}"
-
- [
- public_path,
- public_path.sub("uploads", "uploads/tombstone")
- ].each { |path| File.delete(path) if File.exists?(path) }
+ after do
+ public_path = "#{Discourse.store.public_dir}#{upload.url}"
+
+ [
+ public_path,
+ public_path.sub("uploads", "uploads/tombstone")
+ ].each { |path| File.delete(path) if File.exists?(path) }
+ end
+
+ describe 'when given an invalid sha1' do
+ it 'should not do anything' do
+ upload_recovery.expects(:recover_from_local).never
+
+ post.update!(
+ raw: "![logo.png](upload://#{'a' * 28}.png)"
+ )
+
+ upload_recovery.recover
end
end
+
+ it 'should recover the upload' do
+ stub_request(:get, "http://test.localhost#{upload.url}")
+ .to_return(status: 200)
+
+ expect do
+ upload.destroy!
+ end.to change { post.reload.uploads.count }.from(1).to(0)
+
+ expect do
+ upload_recovery.recover
+ end.to change { post.reload.uploads.count }.from(0).to(1)
+ end
end
end