Class: Aspera::Preview::Generator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(src, dst, options, 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

Parameters:

  • src

    source file path

  • dst

    destination file path

  • api_mime_type

    optional mime type as provided by node api (or nil)



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/aspera/preview/generator.rb', line 38

def initialize(src, dst, options, main_temp_dir, api_mime_type)
  @source_file_path = src
  @destination_file_path = dst
  @options = options
  @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_sym = File.extname(@destination_file_path).gsub(/^\./, '').to_sym
  conversion_type = FileTypes.instance.conversion_type(@source_file_path, api_mime_type)
  @processing_method = "convert_#{conversion_type}_to_#{@preview_format_sym}"
  if conversion_type.eql?(:video)
    case @preview_format_sym
    when :mp4
      @processing_method = "#{@processing_method}_using_#{@options.video_conversion}"
    when :png
      @processing_method = "#{@processing_method}_using_#{@options.video_png_conv}"
    end
  end
  @processing_method = @processing_method.to_sym
  Log.log.debug{"method: #{@processing_method}"}
  Aspera.assert(respond_to?(@processing_method, true)){"no processing know for #{conversion_type} -> #{@preview_format_sym}"}
end

Instance Attribute Details

#conversion_typeObject (readonly)

CLI needs to know conversion type to know if need skip it one of CONVERSION_TYPES



25
26
27
# File 'lib/aspera/preview/generator.rb', line 25

def conversion_type
  @conversion_type
end

Instance Method Details

#generateObject

create preview as specified in constructor



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/aspera/preview/generator.rb', line 61

def generate
  Log.log.info{"#{@source_file_path}->#{@destination_file_path} (#{@processing_method})"}
  begin
    send(@processing_method)
    # check that generated size does not exceed maximum
    result_size = File.size(@destination_file_path)
    Log.log.warn{"preview size exceeds maximum allowed #{result_size} > #{@options.max_size}"} if result_size > @options.max_size
  rescue StandardError => e
    Log.log.error{"Ignoring: #{e.message}"}
    Log.log.debug(e.backtrace.join("\n").red)
    FileUtils.cp(File.expand_path(@preview_format_sym.eql?(:mp4) ? 'video_error.png' : 'image_error.png', File.dirname(__FILE__)), @destination_file_path)
  ensure
    FileUtils.rm_rf(@temp_folder)
  end
end