Gab Social. All are welcome.
This commit is contained in:
16
lib/paperclip/blurhash_transcoder.rb
Normal file
16
lib/paperclip/blurhash_transcoder.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Paperclip
|
||||
class BlurhashTranscoder < Paperclip::Processor
|
||||
def make
|
||||
return @file unless options[:style] == :small
|
||||
|
||||
pixels = convert(':source RGB:-', source: File.expand_path(@file.path)).unpack('C*')
|
||||
geometry = options.fetch(:file_geometry_parser).from_file(@file)
|
||||
|
||||
attachment.instance.blurhash = Blurhash.encode(geometry.width, geometry.height, pixels, options[:blurhash] || {})
|
||||
|
||||
@file
|
||||
end
|
||||
end
|
||||
end
|
||||
26
lib/paperclip/gif_transcoder.rb
Normal file
26
lib/paperclip/gif_transcoder.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Paperclip
|
||||
# This transcoder is only to be used for the MediaAttachment model
|
||||
# to convert animated gifs to webm
|
||||
class GifTranscoder < Paperclip::Processor
|
||||
def make
|
||||
return File.open(@file.path) unless needs_convert?
|
||||
|
||||
final_file = Paperclip::Transcoder.make(file, options, attachment)
|
||||
|
||||
attachment.instance.file_file_name = File.basename(attachment.instance.file_file_name, '.*') + '.mp4'
|
||||
attachment.instance.file_content_type = 'video/mp4'
|
||||
attachment.instance.type = MediaAttachment.types[:gifv]
|
||||
|
||||
final_file
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def needs_convert?
|
||||
num_frames = identify('-format %n :file', file: file.path).to_i
|
||||
options[:style] == :original && num_frames > 1
|
||||
end
|
||||
end
|
||||
end
|
||||
39
lib/paperclip/lazy_thumbnail.rb
Normal file
39
lib/paperclip/lazy_thumbnail.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Paperclip
|
||||
class LazyThumbnail < Paperclip::Thumbnail
|
||||
def make
|
||||
return File.open(@file.path) unless needs_convert?
|
||||
|
||||
if options[:geometry]
|
||||
min_side = [@current_geometry.width, @current_geometry.height].min.to_i
|
||||
options[:geometry] = "#{min_side}x#{min_side}#" if @target_geometry.square? && min_side < @target_geometry.width
|
||||
elsif options[:pixels]
|
||||
width = Math.sqrt(options[:pixels] * (@current_geometry.width.to_f / @current_geometry.height.to_f)).round.to_i
|
||||
height = Math.sqrt(options[:pixels] * (@current_geometry.height.to_f / @current_geometry.width.to_f)).round.to_i
|
||||
options[:geometry] = "#{width}x#{height}>"
|
||||
end
|
||||
|
||||
Paperclip::Thumbnail.make(file, options, attachment)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def needs_convert?
|
||||
needs_different_geometry? || needs_different_format? || needs_metadata_stripping?
|
||||
end
|
||||
|
||||
def needs_different_geometry?
|
||||
(options[:geometry] && @current_geometry.width != @target_geometry.width && @current_geometry.height != @target_geometry.height) ||
|
||||
(options[:pixels] && @current_geometry.width * @current_geometry.height > options[:pixels])
|
||||
end
|
||||
|
||||
def needs_different_format?
|
||||
@format.present? && @current_format != @format
|
||||
end
|
||||
|
||||
def needs_metadata_stripping?
|
||||
@attachment.instance.respond_to?(:local?) && @attachment.instance.local?
|
||||
end
|
||||
end
|
||||
end
|
||||
14
lib/paperclip/video_transcoder.rb
Normal file
14
lib/paperclip/video_transcoder.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Paperclip
|
||||
# This transcoder is only to be used for the MediaAttachment model
|
||||
# to check when uploaded videos are actually gifv's
|
||||
class VideoTranscoder < Paperclip::Processor
|
||||
def make
|
||||
meta = ::Av.cli.identify(@file.path)
|
||||
attachment.instance.type = MediaAttachment.types[:gifv] unless meta[:audio_encode]
|
||||
|
||||
Paperclip::Transcoder.make(file, options, attachment)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user