class Envoi::Mam::Vdms::Agent

Constants

DEFAULT_DESTINATION_PATH
DEFAULT_FORMAT_NAME

Public Instance Methods

after_initialize() click to toggle source
# File lib/envoi/mam/vdms/agent.rb, line 17
def after_initialize
  @default_format_name = initial_args[:default_format_name] || DEFAULT_FORMAT_NAME
end
download(args = { }) click to toggle source
# File lib/envoi/mam/vdms/agent.rb, line 38
def download(args = { })

  asset_id = args[:asset_id]
  file_id = args[:file_id]
  if file_id && !file_id.empty?
    asset_file = api_client.asset_file_get(asset_id: asset_id, file_id: file_id)
  else
    format_name = args[:format_name] || @default_format_name
    format = api_client.asset_format_get_by_name(asset_id: asset_id, format_name: format_name.upcase)
    format_id = format['id']

    asset_files_get_response = api_client.asset_files_get(asset_id: asset_id, generate_signed_url: true)
    asset_files = asset_files_get_response['objects']

    asset_files_for_format = asset_files.find_all { |f| f['format_id'] == format_id }

    asset_file = asset_files_for_format.first
  end

  # file = files.first
  files = [ asset_file ] # just do the first file for now
  files.each do |file|
    begin
      download_file(args, file)
    rescue => e
      logger.warn { "Exception: #{$!}. #{$@.first}" }
    end
  end
  logger.info { 'DONE' }
end
download_file(args, file) click to toggle source
# File lib/envoi/mam/vdms/agent.rb, line 69
def download_file(args, file)
  logger.debug { "File: #{file}"}
  transfer_type = args[:transfer_type] || ''

  file_storage_id = file['storage_id']
  file_directory_path = file['directory_path']
  file_path = !file_directory_path || file_directory_path.empty? ? file['original_name'] : File.join(file_directory_path, file['original_name'])

  file_storage_config = iconik_config['storages'][file_storage_id]

  unless file_storage_config && !file_storage_config.empty?
    raise Exception, "No configuration found for storage '#{file_storage_id}'"
  end

  logger.info { "Transferring File Path: '#{file_path}'" }
  preserve_path = args.fetch(:preserve_path, file_storage_config.fetch('preserve_path', true))

  destination_path = args[:destination_path] || file_storage_config['destination_path'] || DEFAULT_DESTINATION_PATH
  relative_path = preserve_path ? file_directory_path : nil
  relative_path = nil if relative_path == '.'

  target_path = relative_path ? File.join(destination_path, relative_path) : destination_path
  target_path = target_path[0..-1] if target_path.start_with?('/') && !destination_path.start_with?('/')

  aspera_config = file_storage_config['aspera']
  if (transfer_type.empty? || transfer_type == :aspera) && (aspera_config && !aspera_config.empty?)
    client = Envoi::Mam::Agent::TransferClient::Aspera.new(agent: self)
    return client.download(aspera_config, file_path, target_path)
  end

  s3_config = file_storage_config['s3']
  if (transfer_type.empty? || transfer_type == :s3) && (s3_config && !s3_config.empty?)
    target_path = File.expand_path(target_path) if target_path == '.'
    target_path = File.join(target_path, File.basename(file_path))
    client = Envoi::Mam::Agent::TransferClient::S3.new(agent: self)
    return client.download(s3_config, file_path, target_path)
  end

  logger.warn { "No Supported TransferClient Configuration#{transfer_type && !transfer_type.empty? ? " for transfer type '#{transfer_type}' " : ''}Found in Storage Configuration." }
end
iconik_config() click to toggle source
# File lib/envoi/mam/vdms/agent.rb, line 21
def iconik_config
  config['vdms']
end
initialize_api_client(args = { }) click to toggle source
# File lib/envoi/mam/vdms/agent.rb, line 25
def initialize_api_client(args = { })
  @api_client = args[:api_client] || begin
    http_host_address = iconik_config['http_host_address']
    application_id = iconik_config['application_id']
    token = iconik_config['token']
    client_args = { }
    client_args[:http_host_address] = http_host_address if http_host_address && !http_host_address.empty?
    client_args[:application_id] = application_id if application_id && !application_id.empty?
    client_args[:token] = token if token && !token.empty?
    Ubiquity::Iconik::API::Utilities.new(client_args)
  end
end
upload(args = { }) click to toggle source
# File lib/envoi/mam/vdms/agent.rb, line 110
def upload(args = { })
  file_path = args[:file_path]
  if File.directory?(file_path)
    file_paths = Dir.glob(File.join(file_path, '*.*'))
    logger.debug { "File Paths: #{file_paths}"}
    file_paths.map { |fp| upload(args.merge(file_path: fp))}
    return file_paths
  end
  logger.debug { "Preparing to upload '#{file_path}'" }

  transfer_type = args[:transfer_type] || ''
  storage_id = args[:storage_id]
  path_on_storage = nil
  iconik_storage_config = iconik_config['storages'][storage_id]

  unless iconik_storage_config && !iconik_storage_config.empty?
    raise Exception, "No configuration found for storage '#{storage_id}'"
  end

  preserve_path = args.fetch(:preserve_path, iconik_storage_config.fetch('preserve_path', true))

  destination_path = args[:destination_path] || iconik_storage_config['destination_path'] || '/'
  relative_path = preserve_path ? File.dirname(file_path) : nil
  relative_path = File.expand_path(relative_path) if relative_path == '.'

  target_path = relative_path ? File.join(destination_path, relative_path) : destination_path
  target_path = target_path[1..-1] if target_path.start_with?('/') && !destination_path.start_with?('/')

  # abort("FP: #{file_path} TP: #{target_path}")

  transfer_response = begin
    aspera_config = iconik_storage_config['aspera']
    if (transfer_type.empty? || transfer_type == :aspera) && (aspera_config && !aspera_config.empty?)
      client = Envoi::Mam::Agent::TransferClient::Aspera.new(agent: self)
      resp = client.upload(aspera_config, file_path, target_path)
    end

    s3_config = iconik_storage_config['s3']
    if (!resp) && (transfer_type.empty? || transfer_type == :s3) && (s3_config && !s3_config.empty?)
      _target_path = target_path
      _target_path = '' if target_path == '.'
      _target_path = File.join(_target_path, File.basename(file_path))
      client = Envoi::Mam::Agent::TransferClient::S3.new(agent: self)
      resp = client.upload(s3_config, file_path, _target_path)
      path_on_storage = _target_path
    end

    resp
  end

  if transfer_response.nil?
    logger.warn { "No supported TransferClient configuration#{transfer_type && !transfer_type.empty? ? " for transfer type '#{transfer_type}' " : ''}found in storage configuration." }
    return false
  end

  unless transfer_response
    logger.warn { ""}
    return false
  end

  file_size = File.exist?(file_path) ? File.size?(file_path) : 1024
  path_on_storage ||= File.join(target_path, File.basename(file_path))

  api_client.asset_add_using_file_path(file_path: path_on_storage, storage_id: storage_id, file_size: file_size)
end