未验证 提交 b852ef26 编写于 作者: G George Claghorn 提交者: GitHub

Make ASt previewer/analyzer binary paths configurable

上级 32516e88
......@@ -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 @@ def probe
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
......
......@@ -15,7 +15,8 @@ class Engine < Rails::Engine # :nodoc:
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 @@ class Engine < Rails::Engine # :nodoc:
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
......@@ -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
......@@ -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 @@ def preview
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册