Class: Aspera::Preview::Generator
- Inherits:
-
Object
- Object
- Aspera::Preview::Generator
- Defined in:
- lib/aspera/preview/generator.rb
Overview
generate one preview file for one format for one file at a time
Constant Summary collapse
- PREVIEW_FORMATS =
values for preview_format : output format
i[png mp4].freeze
- FFMPEG_OPTIONS_LIST =
%w[in out].freeze
Instance Attribute Summary collapse
-
#conversion_type ⇒ Object
readonly
CLI needs to know conversion type to know if need skip it.
Instance Method Summary collapse
-
#generate ⇒ Object
create preview as specified in constructor.
-
#initialize(options, src, dst, main_temp_dir, api_mime_type) ⇒ Generator
constructor
node API mime types are from: svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types supported preview type is one of Preview::PREVIEW_FORMATS the resulting preview file type is taken from destination file extension.
-
#processing_method_symb ⇒ Object
name of processing method in this object combination of: conversion type and output format (and video_conversion for video).
-
#supported? ⇒ Boolean
for instance, plaintext to mp4 is not supported.
Constructor Details
#initialize(options, src, dst, main_temp_dir, api_mime_type) ⇒ Generator
node API mime types are from: svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types supported preview type is one of Preview::PREVIEW_FORMATS the resulting preview file type is taken from destination file extension. conversion methods are provided by private methods: convert_<conversion_type>to<preview_format>
-> conversion_type is one of FileTypes::CONVERSION_TYPES
-> preview_format is one of Generator::PREVIEW_FORMATS
the conversion video->mp4 is implemented in methods: convert_video_to_mp4_using_<video_conversion>
-> conversion method is one of Generator::VIDEO_CONVERSION_METHODS
35 36 37 38 39 40 41 42 43 |
# File 'lib/aspera/preview/generator.rb', line 35 def initialize(, src, dst, main_temp_dir, api_mime_type) = @source_file_path = src @destination_file_path = dst @temp_folder = File.join(main_temp_dir, @source_file_path.split('/').last.gsub(/\s/, '_').gsub(/\W/, '')) # extract preview format from extension of target file @preview_format_symb = File.extname(@destination_file_path).gsub(/^\./, '').to_sym @conversion_type = FileTypes.instance.conversion_type(@source_file_path, api_mime_type) end |
Instance Attribute Details
#conversion_type ⇒ Object (readonly)
CLI needs to know conversion type to know if need skip it
22 23 24 |
# File 'lib/aspera/preview/generator.rb', line 22 def conversion_type @conversion_type end |
Instance Method Details
#generate ⇒ Object
create preview as specified in constructor
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/aspera/preview/generator.rb', line 69 def generate raise 'could not detect type of file' if @conversion_type.nil? method_symb = processing_method_symb Log.log.info{"#{@source_file_path}->#{@destination_file_path} (#{method_symb})"} begin send(method_symb) # check that generated size does not exceed maximum result_size = File.size(@destination_file_path) if result_size > .max_size Log.log.warn{"preview size exceeds maximum allowed #{result_size} > #{@options.max_size}"} end rescue StandardError => e Log.log.error{"Ignoring: #{e.message}"} Log.log.debug(e.backtrace.join("\n").red) FileUtils.cp(File.(@preview_format_symb.eql?(:mp4) ? 'video_error.png' : 'image_error.png', File.dirname(__FILE__)), @destination_file_path) ensure FileUtils.rm_rf(@temp_folder) end end |
#processing_method_symb ⇒ Object
name of processing method in this object combination of: conversion type and output format (and video_conversion for video)
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/aspera/preview/generator.rb', line 47 def processing_method_symb name = "convert_#{@conversion_type}_to_#{@preview_format_symb}" if @conversion_type.eql?(:video) case @preview_format_symb when :mp4 name = "#{name}_using_#{@options.video_conversion}" when :png name = "#{name}_using_#{@options.video_png_conv}" end end Log.log.debug{"method: #{name}"} return name.to_sym end |
#supported? ⇒ Boolean
for instance, plaintext to mp4 is not supported
63 64 65 66 |
# File 'lib/aspera/preview/generator.rb', line 63 def supported? return false if @conversion_type.nil? return respond_to?(processing_method_symb, true) end |