From f86e16cfbd7087fa882a0d1403a77a7ea527a100 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 1 Dec 2021 00:11:23 -0600 Subject: [PATCH] MediaFile: allow generating thumbnails for corrupt files. We need this so we can regenerate thumbnails for old posts with corrupted images. --- app/logical/media_file.rb | 10 +++++++--- app/logical/media_file/image.rb | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/logical/media_file.rb b/app/logical/media_file.rb index 6b3ea53e5..8ca597acf 100644 --- a/app/logical/media_file.rb +++ b/app/logical/media_file.rb @@ -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 diff --git a/app/logical/media_file/image.rb b/app/logical/media_file/image.rb index 9d1cc7c0f..5f7cddf47 100644 --- a/app/logical/media_file/image.rb +++ b/app/logical/media_file/image.rb @@ -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