A platform for community discussion. Free, open, simple.
DEV: Add libvips support for animation detection (#43453)
Alan Guo Xiang Tan2015e040d636
diff --git a/lib/discourse_vips.rb b/lib/discourse_vips.rbindex c5a50d975f8..2827432a31a 100644--- a/lib/discourse_vips.rb+++ b/lib/discourse_vips.rb@@ -23,6 +23,12 @@ module DiscourseVips)end+ # Uses GIF/WebP frame counts and HEIF/AVIF image collections.+ # APNG and timed HEIF/AVIF sequences are unsupported.+ def self.animated?(input_path:, timeout:)+ Client.call(["animated", input_path], operation: :upload_animation_probe, timeout:)+ end+def self.before_forkClient.before_forkenddiff --git a/lib/upload_creator.rb b/lib/upload_creator.rbindex 36721c781a5..c8dc259e813 100644--- a/lib/upload_creator.rb+++ b/lib/upload_creator.rb@@ -696,8 +696,10 @@ class UploadCreator# Only GIFs, WEBPs and a few other unsupported image types can be animatedOptimizedImage.ensure_safe_paths!(@file.path)- frames =- begin+ begin+ if GlobalSetting.enable_vips_image_processing+ DiscourseVips.animated?(input_path: @file.path, timeout: Upload::MAX_IDENTIFY_SECONDS)+ elseImageMagick.identify("-ping","-format",@@ -706,12 +708,11 @@ class UploadCreatoroperation: :upload_animation_probe,read: [@file.path],timeout: Upload::MAX_IDENTIFY_SECONDS,- ).to_i- rescue StandardError- 1+ ).to_i > 1end-- frames > 1+ rescue StandardError+ false+ endelsefalseenddiff --git a/script/discourse_vips_worker b/script/discourse_vips_workerindex 18a27cef04d..2643b7faa79 100644--- a/script/discourse_vips_worker+++ b/script/discourse_vips_worker@@ -46,6 +46,8 @@ class DiscourseVipsWorkerletter_avatar(*arguments)when "dominant-color"dominant_color(*arguments)+ when "animated"+ animated(*arguments)elseraise ArgumentError, "unsupported libvips operation"end@@ -134,6 +136,20 @@ class DiscourseVipsWorkerendprivate_class_method :dominant_color+ def self.animated(input_path)+ new(read_paths: [input_path]) do+ block_loaders(allowed: %w[VipsForeignLoadNsgif VipsForeignLoadWebp VipsForeignLoadHeif])++ begin+ image = Vips::Image.new_from_file(input_path, access: :sequential)+ image.get_typeof("n-pages") != 0 && image.get("n-pages") > 1+ rescue Vips::Error => error+ raise InvalidImage, error.message+ end+ end+ end+ private_class_method :animated+def self.normalize_color(image)image = image.colourspace(:srgb)image = (image * 255).round(:rint) if %i[float double].include?(image.format)diff --git a/spec/fixtures/images/multipage.avif b/spec/fixtures/images/multipage.avifnew file mode 100644index 00000000000..71ac9ad49b0Binary files /dev/null and b/spec/fixtures/images/multipage.avif differdiff --git a/spec/fixtures/images/static.avif b/spec/fixtures/images/static.avifnew file mode 100644index 00000000000..3344595f1d3Binary files /dev/null and b/spec/fixtures/images/static.avif differdiff --git a/spec/fixtures/images/static.gif b/spec/fixtures/images/static.gifnew file mode 100644index 00000000000..6207e7591d9Binary files /dev/null and b/spec/fixtures/images/static.gif differdiff --git a/spec/fixtures/images/static.webp b/spec/fixtures/images/static.webpnew file mode 100644index 00000000000..9353d2aeb95Binary files /dev/null and b/spec/fixtures/images/static.webp differdiff --git a/spec/lib/discourse_vips_spec.rb b/spec/lib/discourse_vips_spec.rbindex 466d8f7b905..a0af5dd916b 100644--- a/spec/lib/discourse_vips_spec.rb+++ b/spec/lib/discourse_vips_spec.rb@@ -33,6 +33,55 @@ RSpec.describe DiscourseVips doendend+ describe ".animated?" do+ it "detects animation in a GIF" do+ input_path = file_from_fixtures("tiny_animated.gif").path++ expect(described_class.animated?(input_path:, timeout: 5)).to eq(true)+ end++ it "detects animation in a WebP" do+ input_path = file_from_fixtures("animated.webp").path++ expect(described_class.animated?(input_path:, timeout: 5)).to eq(true)+ end++ it "detects animation in an AVIF" do+ input_path = file_from_fixtures("multipage.avif").path++ expect(described_class.animated?(input_path:, timeout: 5)).to eq(true)+ end++ it "identifies a static GIF" do+ input_path = file_from_fixtures("static.gif").path++ expect(described_class.animated?(input_path:, timeout: 5)).to eq(false)+ end++ it "identifies a static WebP" do+ input_path = file_from_fixtures("static.webp").path++ expect(described_class.animated?(input_path:, timeout: 5)).to eq(false)+ end++ it "identifies a static AVIF" do+ input_path = file_from_fixtures("static.avif").path++ expect(described_class.animated?(input_path:, timeout: 5)).to eq(false)+ end++ it "rejects an unreadable image" do+ Tempfile.create(%w[unreadable .gif]) do |file|+ file.write("invalid image")+ file.flush++ expect { described_class.animated?(input_path: file.path, timeout: 5) }.to raise_error(+ DiscourseVips::InvalidImage,+ )+ end+ end+ end+describe "worker lifecycle" doit "recovers after the worker exits unexpectedly" dodescribed_class.versiondiff --git a/spec/lib/upload_creator_spec.rb b/spec/lib/upload_creator_spec.rbindex 90e539511c9..8edb3e17082 100644--- a/spec/lib/upload_creator_spec.rb+++ b/spec/lib/upload_creator_spec.rb@@ -7,6 +7,32 @@ RSpec.describe UploadCreator dofab!(:admin)describe "#create_for" do+ shared_examples "animated upload preservation" do+ it "preserves animated GIF uploads" do+ FastImage.stubs(:animated?).returns(nil)+ file = file_from_fixtures("tiny_animated.gif")+ original = File.binread(file.path)++ upload = described_class.new(file, "tiny_animated.gif").create_for(user.id)++ expect(upload).to be_persisted+ expect(upload.animated).to eq(true)+ expect(File.binread(Discourse.store.path_for(upload))).to eq(original)+ end+ end++ context "with libvips disabled" do+ before { global_setting :enable_vips_image_processing, false }++ include_examples "animated upload preservation"+ end++ context "with libvips enabled" do+ before { global_setting :enable_vips_image_processing, true }++ include_examples "animated upload preservation"+ end+context "when the upload is an SVG" dobefore { SiteSetting.authorized_extensions = "svg" }