MediaFile: allow generating thumbnails for corrupt files.

We need this so we can regenerate thumbnails for old posts with
corrupted images.
This commit is contained in:
evazion
2021-12-01 00:11:23 -06:00
parent 2ffe973275
commit f86e16cfbd
2 changed files with 8 additions and 4 deletions

View File

@@ -8,7 +8,7 @@ class MediaFile
extend Memoist
include ActiveModel::Serializers::JSON
attr_accessor :file
attr_accessor :file, :strict
# delegate all File methods to `file`.
delegate *(File.instance_methods - MediaFile.instance_methods), to: :file
@@ -71,9 +71,13 @@ class MediaFile
end
# Initialize a MediaFile from a regular File.
# @param file [File] the image file
def initialize(file, **options)
#
# @param file [File] The image file.
# @param strict [Boolean] If true, raise errors if the file is corrupt. If false,
# try to process corrupt files without raising any errors.
def initialize(file, strict: true, **options)
@file = file
@strict = strict
end
# @return [Array<(Integer, Integer)>] the width and height of the file

View File

@@ -120,7 +120,7 @@ class MediaFile::Image < MediaFile
# @return [Vips::Image] the Vips image object for the file
def image
Vips::Image.new_from_file(file.path, fail: true).autorot
Vips::Image.new_from_file(file.path, fail: strict).autorot
end
def video