class Libis::Format::Cli::Convert

Constants

DEFAULT_CONFIG
STRING_CONFIG

Public Class Methods

description(field) click to toggle source
# File lib/libis/format/cli/convert.rb, line 19
def self.description(field)
  "#{STRING_CONFIG[field]}." + (DEFAULT_CONFIG[field].nil? ? '' : " default: #{DEFAULT_CONFIG[field]}")
end

Public Instance Methods

auto(source_file, target_file) click to toggle source
# File lib/libis/format/cli/convert.rb, line 77
def auto(source_file, target_file)
  options_file = options[:options]
  opts = check_input(source_file, target_file, options_file)
  output, converter = do_convert(source_file, target_file, opts)
  prompt.ok "Output file '#{output}' created with converter #{converter}."
end
image(source_file, target_file) click to toggle source
# File lib/libis/format/cli/convert.rb, line 116
def image(source_file, target_file)
  check_input(source_file, target_file)
  convert_image(source_file, target_file)
  prompt.ok "Output file '#{target_file}' created with image converter."
end

Protected Instance Methods

convert_file(src_file, tgt_file, src_format, tgt_format, opts) click to toggle source
# File lib/libis/format/cli/convert.rb, line 161
def convert_file(src_file, tgt_file, src_format, tgt_format, opts)
  converter = Libis::Format::Converter::Repository.get_converter_chain(src_format, tgt_format, opts)

  unless converter
    prompt.error "Could not find converter for #{src_format} -> #{tgt_format} with #{opts}"
    exit
  end

  converter_name = converter.to_s
  prompt.say 'Converting file %s to %s with %s ' % [src_file, tgt_file, converter_name]
  converted = converter.convert(src_file, tgt_file)

  unless converted && converted == tgt_file
    prompt.error 'File conversion failed (%s).', converter_name
    return [nil, converter_name]
  end

  [tgt_file, converter_name]
end
convert_image(source_file, target_file) click to toggle source
# File lib/libis/format/cli/convert.rb, line 181
def convert_image(source_file, target_file)
  target_format = get_format(target_file)
  converter = Libis::Format::Converter::ImageConverter.new
  converter.page options[:page] if options[:page]
  converter.quiet options[:quiet] if options[:quiet]
  converter.scale options[:scale] if options[:scale]
  converter.resize options[:resize] if options[:resize]
  converter.resample options[:resample] if options[:resample]
  converter.flatten if options[:flatten]
  converter.delete_date if options[:delete_date]
  converter.colorspace options[:colorspace] if options[:colorspace]
  converter.profile options[:profile] if options[:profile]
  if options[:wm_text] || options[:wm_file]
    wm_options = {}
    wm_options[:text] = options[:wm_text] if options[:wm_text]
    wm_options[:file] = options[:wm_file] if options[:wm_file]
    wm_options[:tiles] = options[:wm_tiles] if options[:wm_tiles]
    wm_options[:resize] = options[:wm_resize] if options[:wm_resize]
    wm_options[:gap] = options[:wm_gap] if options[:wm_gap]
    wm_options[:rotation] = options[:wm_rotation] if options[:wm_rotation]
    wm_options[:opacity] = options[:wm_opacity] if options[:wm_opacity]
    wm_options[:gravity] = options[:wm_gravity] if options[:wm_gravity]
    wm_options[:composition] = options[:wm_composition] if options[:wm_composition]
    converter.watermark(wm_options)
  end
  converter.convert source_file, target_file, target_format
end
do_convert(source_file, target_file, opts) click to toggle source
# File lib/libis/format/cli/convert.rb, line 124
def do_convert(source_file, target_file, opts)
  format_info = format_identifier(source_file)
  src_mime = format_info[:mimetype]
  source_format = format_info[:TYPE]
  unless source_format
    prompt.error "File item %s format (#{src_mime}) is not supported."
    exit
  end
  target_format = get_format(target_file)
  converterlist = []
  temp_files = []
  opts.each do |o|
    o = o.dup
    tgt_format = o.delete(:target_format) || target_format
    tgt_file = tempname(source_file, tgt_format)
    temp_files << tgt_file
    begin
      source_file, converter = convert_file(source_file, tgt_file, source_format, tgt_format, o)
    rescue Exception => e
      prompt.error "File conversion of '%s' from '%s' to '%s' failed: %s @ %s" %
                       [source_file, source_format, tgt_format, e.message, e.backtrace.first]
      exit
    end
    source_format = tgt_format
    converterlist << converter
  end
  converter = converterlist.join(' + ')
  if target_file
    FileUtils.mkpath(File.dirname(target_file))
    FileUtils.copy(source_file, target_file)
  else
    target_file = temp_files.pop
  end
  temp_files.each {|tmp_file| FileUtils.rm_f tmp_file}
  [target_file, converter]
end

Private Instance Methods

check_input(source_file, target_file, options_file = nil) click to toggle source
# File lib/libis/format/cli/convert.rb, line 211
def check_input(source_file, target_file, options_file = nil)
  # Check if source file exists and can be read
  unless source_file && File.exist?(source_file) && File.readable?(source_file)
    prompt.error "Fatal error trying to access source file '#{source_file}'."
    exit
  end
  # Check if target file argument is supplied
  unless target_file
    prompt.error "Fatal error: no target file name supplied."
    exit
  end
  # Check if target file directory can be created
  begin
    FileUtils.mkdir_p(File.dirname(target_file))
  rescue SystemCallError => e
    prompt.error "Fatal error trying to create folder for target file: #{e.message}"
    exit
  end

  return [{}] unless options_file

  # Check if options file exists and can be read
  unless File.exist?(options_file) && File.readable?(options_file)
    prompt.error "Fatal error trying to access options file '#{options_file}'."
    exit
  end

  begin
    opts = ::YAML.load_file(options_file)
    opts = case opts
           when Hash
             [opts]
           when Array
             opts
           when NilClass
             [{}]
           else
             prompt.error "Options file contents should be a Hash or an Array of Hashes."
             exit
           end
    opts.each {|h| h.key_strings_to_symbols!(recursive: true)}
    opts
  rescue Exception => e
    prompt.error "Fatal error trying to parse options file: #{e.message}"
    exit
  end

end
extname(format) click to toggle source
# File lib/libis/format/cli/convert.rb, line 267
def extname(format)
  Libis::Format::Library.get_field(format, :extensions)
end
format_identifier(file) click to toggle source
# File lib/libis/format/cli/convert.rb, line 275
def format_identifier(file)
  prompt.say "Identifying format of file '#{file}'"
  result = Libis::Format::Identifier.get(file) || {}
  process_messages(result)
  format = result[:formats][file]
  unless format[:mimetype]
    prompt.warn "Could not determine MIME type. Using default 'application/octet-stream'."
    result[:puid] ||= 'fmt/unknown'
  end
  prompt.say "#{file} format: #{format[:GROUP]}, #{format[:TYPE]} (puid: #{format[:puid]}) mimetype: #{format[:mimetype]}"
  format
end
get_format(file_name) click to toggle source
# File lib/libis/format/cli/convert.rb, line 271
def get_format(file_name)
  Libis::Format::Library.get_field_by(:extension, File.extname(file_name), :format)
end
process_messages(format_result) click to toggle source
# File lib/libis/format/cli/convert.rb, line 288
def process_messages(format_result)
  format_result[:messages].each do |msg|
    case msg[0]
    when :debug
      prompt.say msg[1]
    when :info
      prompt.say msg[1]
    when :warn
      prompt.warn msg[1]
    when :error
      prompt.error msg[1]
    when :fatal
      prompt.error msg[1]
    else
      prompt.say "#{msg[0]}: #{msg[1]}"
    end
  end
end
tempname(source_file, target_format) click to toggle source
# File lib/libis/format/cli/convert.rb, line 260
def tempname(source_file, target_format)
  # noinspection RubyResolve
  Dir::Tmpname.create(
      [File.basename(source_file, '.*'), ".#{extname(target_format)}"]
  ) {}
end