Class: Aspera::Preview::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/preview/generator.rb

Overview

Generates 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 =

List of valid ffmpeg option keys for reencode configuration.

%w[in out].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, dst, options, main_temp_dir, mime: nil) ⇒ Generator

Node API MIME types are from: svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types. 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 (String)

    Source file path.

  • dst (String)

    Destination file path.

  • options (Options)

    All conversion options.

  • main_temp_dir (String)

    Main temp folder, sub folder will be created for generation.

  • mime (String, nil) (defaults to: nil)

    Optional MIME type as provided by node api (or nil).



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

def initialize(src, dst, options, main_temp_dir, mime: nil)
  # Source file path
  @source = src
  # Destination file path
  @destination = dst
  @options = options
  # temp folder name based on source file
  @temp_folder = File.join(main_temp_dir, @source.split('/').last.gsub(/\s/, '_').gsub(/\W/, ''))
  # Extract preview format from extension of target file.
  @preview_format_sym = File.extname(@destination).gsub(/^\./, '').to_sym
  conversion_type = FileTypes.instance.conversion_type(@source, mime)
  @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 known for #{conversion_type} -> #{@preview_format_sym}"}
  command = [:magick] + %w[identify -list font]
  magick_fonts = Utils.parse_magick_fonts(Utils.execute(*command, mode: :capture).first)
  Aspera.assert(magick_fonts[:fonts].any?{ |f| f[:name].eql?(@options.thumb_text_font)}){"Missing font #{@options.thumb_text_font} in #{command}"}
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

#destinationObject (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 destination
  @destination
end

Instance Method Details

#error_assetString

Path to error image corresponding to preview type.

Returns:

  • (String)

    The path to the error image.



82
83
84
# File 'lib/aspera/preview/generator.rb', line 82

def error_asset
  File.expand_path(@preview_format_sym.eql?(:mp4) ? 'video_error.png' : 'image_error.png', File.dirname(__FILE__))
end

#generateObject

Creates preview as specified in constructor.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/aspera/preview/generator.rb', line 68

def generate
  Log.log.debug{"#{@source}->#{@destination} (#{@processing_method})"}
  begin
    send(@processing_method)
    # Check that generated size does not exceed maximum.
    result_size = File.size(@destination)
    Log.log.warn{"preview size exceeds maximum allowed #{result_size} > #{@options.max_size}"} if result_size > @options.max_size
  ensure
    FileUtils.rm_rf(@temp_folder)
  end
end