Skip to content

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

FIX: Skip dominant color calculation when the upload file is missing (#42077)

Alan Guo Xiang Tanb21f43d070a7
diff --git a/app/models/upload.rb b/app/models/upload.rb
index 16b6548dba0..4b8334168c7 100644
--- a/app/models/upload.rb
+++ b/app/models/upload.rb
@@ -387,8 +387,9 @@ class Upload < ActiveRecord::Base
Discourse.store.download(self)
end
- if local_path.nil?
- # Download failed. Could be too large to download, or file could be missing in s3
+ if local_path.nil? || !File.exist?(local_path)
+ # Download failed, or the file is gone from the store. Could be too large
+ # to download, or file could be missing in s3
color = ""
end
diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb
index 504aa28bacf..19f8010efc8 100644
--- a/spec/models/upload_spec.rb
+++ b/spec/models/upload_spec.rb
@@ -1001,6 +1001,14 @@ RSpec.describe Upload do
expect(not_an_image.dominant_color).to eq("")
end
+ it "stores an empty string when the file is missing from the store" do
+ File.delete(Discourse.store.path_for(white_image))
+
+ expect(white_image.dominant_color).to eq(nil)
+ expect(white_image.dominant_color(calculate_if_missing: true)).to eq("")
+ expect(white_image.dominant_color).to eq("")
+ end
+
it "correctly handles invalid image files" do
expect(invalid_image.dominant_color).to eq(nil)
expect(invalid_image.dominant_color(calculate_if_missing: true)).to eq("")