FIX: Correctly handle HTTP errors during dominant color calculation (#18565)

The previous fix in e83d35d6 was incorrect, and the stub in the test was never actually hit. This commit moves the error handling to the right place and updates the specs to ensure the stub is always used.
This commit is contained in:
David Taylor 2022-10-12 15:50:44 +01:00 committed by GitHub
parent 899ed039b7
commit 76c86a4269
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 15 deletions

View File

@ -340,7 +340,13 @@ class Upload < ActiveRecord::Base
if local? if local?
Discourse.store.path_for(self) Discourse.store.path_for(self)
else else
Discourse.store.download(self)&.path begin
Discourse.store.download(self)&.path
rescue OpenURI::HTTPError => e
# Some issue with downloading the image from a remote store.
# Assume the upload is broken and save an empty string to prevent re-evaluation
nil
end
end end
if local_path.nil? if local_path.nil?
@ -373,10 +379,6 @@ class Upload < ActiveRecord::Base
raise "Calculated dominant color but unable to parse output:\n#{data}" if color.nil? raise "Calculated dominant color but unable to parse output:\n#{data}" if color.nil?
color color
rescue OpenURI::HTTPError => e
# Some issue with downloading the image from a remote store.
# Assume the upload is broken and save an empty string to prevent re-evaluation
""
rescue Discourse::Utils::CommandError => e rescue Discourse::Utils::CommandError => e
# Timeout or unable to parse image # Timeout or unable to parse image
# This can happen due to bad user input - ignore and save # This can happen due to bad user input - ignore and save

View File

@ -704,21 +704,21 @@ RSpec.describe Upload do
end end
it "correctly handles error when file is too large to download" do it "correctly handles error when file is too large to download" do
white_image.stubs(:local?).returns(true) white_image.stubs(:local?).returns(false)
Discourse.store.stubs(:download).returns(nil) FileStore::LocalStore.any_instance.stubs(:download).returns(nil).once
expect(invalid_image.dominant_color).to eq(nil) expect(white_image.dominant_color).to eq(nil)
expect(invalid_image.dominant_color(calculate_if_missing: true)).to eq("") expect(white_image.dominant_color(calculate_if_missing: true)).to eq("")
expect(invalid_image.dominant_color).to eq("") expect(white_image.dominant_color).to eq("")
end end
it "correctly handles error when file has HTTP error" do it "correctly handles error when file has HTTP error" do
white_image.stubs(:local?).returns(true) white_image.stubs(:local?).returns(false)
Discourse.store.stubs(:download).raises(OpenURI::HTTPError) FileStore::LocalStore.any_instance.stubs(:download).raises(OpenURI::HTTPError.new("Error", nil)).once
expect(invalid_image.dominant_color).to eq(nil) expect(white_image.dominant_color).to eq(nil)
expect(invalid_image.dominant_color(calculate_if_missing: true)).to eq("") expect(white_image.dominant_color(calculate_if_missing: true)).to eq("")
expect(invalid_image.dominant_color).to eq("") expect(white_image.dominant_color).to eq("")
end end
it "is validated for length" do it "is validated for length" do