Skip to content

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

FIX: Record correct image dimensions after libvips resizing (#43802)

Alan Guo Xiang Tan2dab22c161c3
diff --git a/lib/discourse_vips.rb b/lib/discourse_vips.rb
index cdd79f68e3e..666730c7cfe 100644
--- a/lib/discourse_vips.rb
+++ b/lib/discourse_vips.rb
@@ -161,7 +161,7 @@ module DiscourseVips
timeout:,
nice: 10,
)
- File.rename(output.path, output_path)
+ FileUtils.copy_file(output.path, output_path)
end
nil
end
diff --git a/spec/lib/upload_creator_spec.rb b/spec/lib/upload_creator_spec.rb
index b0c33852d38..252d630f8ce 100644
--- a/spec/lib/upload_creator_spec.rb
+++ b/spec/lib/upload_creator_spec.rb
@@ -49,6 +49,10 @@ RSpec.describe UploadCreator do
expect(upload).to be_persisted
expect(upload.original_filename).to eq("wrong_upload.png")
expect(upload.extension).to eq("png")
+ expect(upload).to have_attributes(
+ width: Discourse.avatar_sizes.max,
+ height: Discourse.avatar_sizes.max,
+ )
expect(FastImage.type(Discourse.store.path_for(upload))).to eq(:png)
expect(FastImage.size(Discourse.store.path_for(upload))).to eq(
[Discourse.avatar_sizes.max, Discourse.avatar_sizes.max],
@@ -68,6 +72,20 @@ RSpec.describe UploadCreator do
before { global_setting :enable_vips_image_processing, true }
include_examples "image extension correction"
+
+ it "accepts JPEG uploads after downsizing them below the size limit" do
+ file = file_from_fixtures("logo.jpg")
+ SiteSetting.max_image_size_kb = 16
+ expect(File.size(file.path)).to be > SiteSetting.max_image_size_kb.kilobytes
+
+ upload = described_class.new(file, "large.jpg", force_optimize: true).create_for(user.id)
+
+ expect(upload).to be_persisted
+ stored_path = Discourse.store.path_for(upload)
+ width, height = FastImage.size(stored_path)
+ expect(upload).to have_attributes(width:, height:, filesize: File.size(stored_path))
+ expect(upload.filesize).to be < SiteSetting.max_image_size_kb.kilobytes
+ end
end
context "when the upload is an SVG" do