Skip to content

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

FIX: Ignore invalid images when shrinking uploads (#25346)

Jarek Radosz5278734fe267
diff --git a/lib/shrink_uploaded_image.rb b/lib/shrink_uploaded_image.rb
index f46a3b72073..042a9fcce3f 100644
--- a/lib/shrink_uploaded_image.rb
+++ b/lib/shrink_uploaded_image.rb
@@ -40,7 +40,11 @@ class ShrinkUploadedImage
return false
end
- w, h = FastImage.size(path, timeout: 15, raise_on_failure: true)
+ begin
+ w, h = FastImage.size(path, timeout: 15, raise_on_failure: true)
+ rescue FastImage::SizeNotFound
+ return false
+ end
if !w || !h
log "Invalid image dimensions after resizing"
diff --git a/spec/lib/shrink_uploaded_image_spec.rb b/spec/lib/shrink_uploaded_image_spec.rb
index 58b5522a69d..06f0076a0ce 100644
--- a/spec/lib/shrink_uploaded_image_spec.rb
+++ b/spec/lib/shrink_uploaded_image_spec.rb
@@ -110,6 +110,21 @@ RSpec.describe ShrinkUploadedImage do
expect(result).to be(false)
end
+
+ it "returns false if the image is invalid" do
+ post = Fabricate(:post, raw: "<img src='#{upload.url}'>")
+ post.link_post_uploads
+ FastImage.stubs(:size).raises(FastImage::SizeNotFound.new)
+
+ result =
+ ShrinkUploadedImage.new(
+ upload: upload,
+ path: Discourse.store.path_for(upload),
+ max_pixels: 10_000,
+ ).perform
+
+ expect(result).to be(false)
+ end
end
context "when S3 uploads are enabled" do