1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Make ASt previewer/analyzer binary paths configurable

This commit is contained in:
George Claghorn 2017-12-01 11:07:30 -05:00 committed by GitHub
parent 32516e8862
commit b852ef2660
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 4 deletions

View file

@ -19,6 +19,8 @@ module ActiveStorage
# This analyzer requires the {ffmpeg}[https://www.ffmpeg.org] system library, which is not provided by Rails. You must
# install ffmpeg yourself to use this analyzer.
class Analyzer::VideoAnalyzer < Analyzer
class_attribute :ffprobe_path, default: "ffprobe"
def self.accept?(blob)
blob.video?
end
@ -68,7 +70,7 @@ module ActiveStorage
end
def probe_from(file)
IO.popen([ "ffprobe", "-print_format", "json", "-show_streams", "-v", "error", file.path ]) do |output|
IO.popen([ ffprobe_path, "-print_format", "json", "-show_streams", "-v", "error", file.path ]) do |output|
JSON.parse(output.read)
end
rescue Errno::ENOENT

View file

@ -15,7 +15,8 @@ module ActiveStorage
config.active_storage = ActiveSupport::OrderedOptions.new
config.active_storage.previewers = [ ActiveStorage::Previewer::PDFPreviewer, ActiveStorage::Previewer::VideoPreviewer ]
config.active_storage.analyzers = [ ActiveStorage::Analyzer::ImageAnalyzer, ActiveStorage::Analyzer::VideoAnalyzer ]
config.active_storage.analyzers = [ ActiveStorage::Analyzer::ImageAnalyzer, ActiveStorage::Analyzer::VideoAnalyzer ]
config.active_storage.paths = ActiveSupport::OrderedOptions.new
config.eager_load_namespaces << ActiveStorage
@ -68,5 +69,21 @@ module ActiveStorage
end
end
end
initializer "active_storage.paths" do
config.after_initialize do |app|
if ffprobe_path = app.config.active_storage.paths.ffprobe
ActiveStorage::Analyzer::VideoAnalyzer.ffprobe_path = ffprobe_path
end
if ffmpeg_path = app.config.active_storage.paths.ffmpeg
ActiveStorage::Previewer::VideoPreviewer.ffmpeg_path = ffmpeg_path
end
if mutool_path = app.config.active_storage.paths.mutool
ActiveStorage::Previewer::PDFPreviewer.mutool_path = mutool_path
end
end
end
end
end

View file

@ -2,16 +2,23 @@
module ActiveStorage
class Previewer::PDFPreviewer < Previewer
class_attribute :mutool_path, default: "mutool"
def self.accept?(blob)
blob.content_type == "application/pdf"
end
def preview
download_blob_to_tempfile do |input|
draw "mutool", "draw", "-F", "png", "-o", "-", input.path, "1" do |output|
draw_first_page_from input do |output|
yield io: output, filename: "#{blob.filename.base}.png", content_type: "image/png"
end
end
end
private
def draw_first_page_from(file, &block)
draw mutool_path, "draw", "-F", "png", "-o", "-", file.path, "1", &block
end
end
end

View file

@ -2,6 +2,8 @@
module ActiveStorage
class Previewer::VideoPreviewer < Previewer
class_attribute :ffmpeg_path, default: "ffmpeg"
def self.accept?(blob)
blob.video?
end
@ -16,7 +18,7 @@ module ActiveStorage
private
def draw_relevant_frame_from(file, &block)
draw "ffmpeg", "-i", file.path, "-y", "-vcodec", "png",
draw ffmpeg_path, "-i", file.path, "-y", "-vcodec", "png",
"-vf", "thumbnail", "-vframes", "1", "-f", "image2", "-", &block
end
end