uploads: factor out remaining image methods to MediaFile.

This commit is contained in:
evazion
2020-05-19 02:18:30 -05:00
parent 45064853de
commit 364343453c
7 changed files with 56 additions and 88 deletions

View File

@@ -20,6 +20,17 @@ class MediaFile::Image < MediaFile
[0, 0]
end
def is_corrupt?
image.stats
false
rescue Vips::Error
true
end
def is_animated?
is_animated_gif? || is_animated_png?
end
# https://github.com/jcupitt/libvips/wiki/HOWTO----Image-shrinking
# http://jcupitt.github.io/libvips/API/current/Using-vipsthumbnail.md.html
def preview(width, height)
@@ -40,6 +51,14 @@ class MediaFile::Image < MediaFile
private
def is_animated_gif?
file_ext == :gif && image.get("n-pages") > 1
end
def is_animated_png?
file_ext == :png && APNGInspector.new(file.path).inspect!.animated?
end
def image
@image ||= Vips::Image.new_from_file(file.path, fail: true)
end

View File

@@ -1,5 +1,4 @@
class MediaFile::Ugoira < MediaFile
extend Memoist
class Error < StandardError; end
attr_reader :frame_data

View File

@@ -1,10 +1,12 @@
class MediaFile::Video < MediaFile
extend Memoist
def dimensions
[video.width, video.height]
end
def duration
video.duration
end
def preview(max_width, max_height)
preview_frame.preview(max_width, max_height)
end
@@ -13,6 +15,10 @@ class MediaFile::Video < MediaFile
preview_frame.crop(max_width, max_height)
end
def has_audio?
video.audio_channels.present?
end
private
def video