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(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

Parameters:

  • src

    source file path

  • dst

    destination file path

  • api_mime_type

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



35
36
37
38
39
40
41
42
43
# File 'lib/aspera/preview/generator.rb', line 35

def initialize(options, src, dst, main_temp_dir, api_mime_type)
  @options = options
  @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_typeObject (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

#generateObject

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 > @options.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.expand_path(@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_symbObject

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

Returns:

  • (Boolean)

    true if conversion is 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