FIX: Handle failed download when calculating image dominant color (#18342)

This can happen when the upload size exceeds the maximum upload size, or there is a network issue during download
This commit is contained in:
David Taylor 2022-09-23 12:42:07 +01:00 committed by GitHub
parent 8964749989
commit 42947ec6f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -340,10 +340,15 @@ class Upload < ActiveRecord::Base
if local?
Discourse.store.path_for(self)
else
Discourse.store.download(self).path
Discourse.store.download(self)&.path
end
color = begin
if local_path.nil?
# Download failed. Could be too large to download, or file could be missing in s3
color = ""
end
color ||= begin
data = Discourse::Utils.execute_command(
"nice",
"-n",

View File

@ -690,6 +690,15 @@ RSpec.describe Upload do
expect(invalid_image.dominant_color).to eq(nil)
end
it "correctly handles download failures" do
white_image.stubs(:local?).returns(true)
Discourse.store.stubs(:download).returns(nil)
expect(invalid_image.dominant_color).to eq(nil)
expect(invalid_image.dominant_color(calculate_if_missing: true)).to eq("")
expect(invalid_image.dominant_color).to eq("")
end
it "is validated for length" do
u = Fabricate(:upload)