Skip to content

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.rb
index 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_fork
Client.before_fork
end
diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb
index 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 animated
OptimizedImage.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)
+ else
ImageMagick.identify(
"-ping",
"-format",
@@ -706,12 +708,11 @@ class UploadCreator
operation: :upload_animation_probe,
read: [@file.path],
timeout: Upload::MAX_IDENTIFY_SECONDS,
- ).to_i
- rescue StandardError
- 1
+ ).to_i > 1
end
-
- frames > 1
+ rescue StandardError
+ false
+ end
else
false
end
diff --git a/script/discourse_vips_worker b/script/discourse_vips_worker
index 18a27cef04d..2643b7faa79 100644
--- a/script/discourse_vips_worker
+++ b/script/discourse_vips_worker
@@ -46,6 +46,8 @@ class DiscourseVipsWorker
letter_avatar(*arguments)
when "dominant-color"
dominant_color(*arguments)
+ when "animated"
+ animated(*arguments)
else
raise ArgumentError, "unsupported libvips operation"
end
@@ -134,6 +136,20 @@ class DiscourseVipsWorker
end
private_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.avif
new file mode 100644
index 00000000000..71ac9ad49b0
Binary files /dev/null and b/spec/fixtures/images/multipage.avif differ
diff --git a/spec/fixtures/images/static.avif b/spec/fixtures/images/static.avif
new file mode 100644
index 00000000000..3344595f1d3
Binary files /dev/null and b/spec/fixtures/images/static.avif differ
diff --git a/spec/fixtures/images/static.gif b/spec/fixtures/images/static.gif
new file mode 100644
index 00000000000..6207e7591d9
Binary files /dev/null and b/spec/fixtures/images/static.gif differ
diff --git a/spec/fixtures/images/static.webp b/spec/fixtures/images/static.webp
new file mode 100644
index 00000000000..9353d2aeb95
Binary files /dev/null and b/spec/fixtures/images/static.webp differ
diff --git a/spec/lib/discourse_vips_spec.rb b/spec/lib/discourse_vips_spec.rb
index 466d8f7b905..a0af5dd916b 100644
--- a/spec/lib/discourse_vips_spec.rb
+++ b/spec/lib/discourse_vips_spec.rb
@@ -33,6 +33,55 @@ RSpec.describe DiscourseVips do
end
end
+ 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" do
it "recovers after the worker exits unexpectedly" do
described_class.version
diff --git a/spec/lib/upload_creator_spec.rb b/spec/lib/upload_creator_spec.rb
index 90e539511c9..8edb3e17082 100644
--- a/spec/lib/upload_creator_spec.rb
+++ b/spec/lib/upload_creator_spec.rb
@@ -7,6 +7,32 @@ RSpec.describe UploadCreator do
fab!(: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" do
before { SiteSetting.authorized_extensions = "svg" }