media assets: fix regenerating AI tags for flash files.

Fix it so that trying to regenerate AI tags for a Flash file doesn't
fail because Flash files have no image preview.

Also let `MediaFile.open` take a block argument.
This commit is contained in:
evazion
2022-11-01 16:30:50 -05:00
parent 29b3b83c9a
commit d2c520035b
2 changed files with 41 additions and 12 deletions

View File

@@ -15,17 +15,38 @@ class MediaFile
# delegate all File methods to `file`.
delegate *(File.instance_methods - MediaFile.instance_methods), to: :file
# Open a file or filename and return a MediaFile object.
# Open a file or filename and return a MediaFile object. If a block is given,
# pass the file to the block and return the result after closing the file.
#
# @param file [File, String] a filename or an open File object
# @param file [File, MediaFile, String] A filename or an open File object.
# @param options [Hash] extra options for the MediaFile subclass.
# @return [MediaFile] the media file
def self.open(file, **options)
return file.dup if file.is_a?(MediaFile)
# @yieldparam media_file [MediaFile] The opened media file.
# @return [MediaFile] The media file.
def self.open(file, **options, &block)
if file.is_a?(MediaFile)
media_file = file
else
file = Kernel.open(file, "r", binmode: true) unless file.respond_to?(:read)
media_file = new_from_file(file, **options)
end
file = Kernel.open(file, "r", binmode: true) unless file.respond_to?(:read)
if block_given?
result = yield media_file
media_file.close
result
else
media_file
end
end
case file_ext(file)
# Return a new MediaFile from an open File object.
#
# @param file [File] The File object.
# @param file_ext [Symbol] The file extension.
# @param options [Hash] Extra options for the MediaFile subclass.
# @return [MediaFile] The media file.
def self.new_from_file(file, file_ext = MediaFile.file_ext(file), **options)
case file_ext
when :jpg, :gif, :png, :webp, :avif
MediaFile::Image.new(file, **options)
when :swf