media file: fix exception when getting frame count of corrupted gif.
Fix a `gifload: no frames in GIF` error from libvips when trying to read the frame count for https://danbooru.donmai.us/media_assets/1141668.
This commit is contained in:
@@ -40,7 +40,7 @@ class MediaFile::Image < MediaFile
|
|||||||
def frame_count
|
def frame_count
|
||||||
case file_ext
|
case file_ext
|
||||||
when :gif, :webp
|
when :gif, :webp
|
||||||
image.get("n-pages") if image.get_fields.include?("n-pages")
|
n_pages
|
||||||
when :png
|
when :png
|
||||||
metadata.fetch("PNG:AnimationFrames", 1)
|
metadata.fetch("PNG:AnimationFrames", 1)
|
||||||
when :avif
|
when :avif
|
||||||
@@ -50,6 +50,13 @@ class MediaFile::Image < MediaFile
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [Integer, nil] The frame count for gif and webp images, or possibly nil if the file doesn't have a frame count or is corrupt.
|
||||||
|
def n_pages
|
||||||
|
image.get("n-pages")
|
||||||
|
rescue Vips::Error
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
def frame_rate
|
def frame_rate
|
||||||
return nil if !is_animated? || frame_count.nil? || duration.nil? || duration == 0
|
return nil if !is_animated? || frame_count.nil? || duration.nil? || duration == 0
|
||||||
frame_count / duration
|
frame_count / duration
|
||||||
|
|||||||
BIN
test/files/gif/corrupt-static.gif
Normal file
BIN
test/files/gif/corrupt-static.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
@@ -471,6 +471,18 @@ class MediaFileTest < ActiveSupport::TestCase
|
|||||||
assert_equal("89a", @metadata["GIF:GIFVersion"])
|
assert_equal("89a", @metadata["GIF:GIFVersion"])
|
||||||
assert_equal(6, @metadata.count)
|
assert_equal(6, @metadata.count)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "not raise an exception when reading the frame count" do
|
||||||
|
@file = MediaFile.open("test/files/gif/corrupt-static.gif")
|
||||||
|
@metadata = @file.metadata
|
||||||
|
|
||||||
|
assert_equal(true, @file.is_corrupt?)
|
||||||
|
assert_equal(nil, @file.frame_count)
|
||||||
|
assert_equal("File format error", @metadata["ExifTool:Error"])
|
||||||
|
assert_equal("89a", @metadata["GIF:GIFVersion"])
|
||||||
|
assert_equal(6, @metadata.count)
|
||||||
|
assert_nothing_raised { @file.attributes }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "a corrupt PNG" do
|
context "a corrupt PNG" do
|
||||||
|
|||||||
Reference in New Issue
Block a user