class Envoi::Mam::MediaSilo::Agent

Constants

DEFAULT_DOWNLOAD_DESTINATION_PATH

Public Instance Methods

download(args = { }) click to toggle source
# File lib/envoi/mam/mediasilo/agent.rb, line 46
def download(args = { })
  transfer_type = args[:transfer_type]

  file_path = args[:file_path]
  project_id = args[:project_id]
  folder_id = args[:folder_id]
  asset_id = args[:asset_id]

  mediasilo_path = args[:mediasilo_path]
  project_name = args[:project_name]
  folder_name = args[:folder_name]

  if mediasilo_path || project_name || folder_name
    check_path_result = api_client.check_path(mediasilo_path)
    found = check_path_result[:existing]

    project = found[:project] || { }
    project_id = project['id']

    folders = found[:folders]
    folder_id = (folders.last || { })['id']

    asset = found[:asset] || { }
    asset_id = asset['id']
  end

  asset ||= asset_id

  destination_file_path = args[:destination_path] || DEFAULT_DOWNLOAD_DESTINATION_PATH

  case transfer_type
  when :aspera; download_using_aspera(asset_id, destination_file_path, args)
  else
    do_upload_response = download_using_http(asset, destination_file_path, args)
    asset_url = do_upload_response[:asset_url]
  end

end
download_using_aspera(asset_id, destination_file_path, args = { }) click to toggle source
# File lib/envoi/mam/mediasilo/agent.rb, line 85
def download_using_aspera(asset_id, destination_file_path, args = { })
  derivative_type = args[:asset_derivative_type]
  derivative_type = derivative_type.downcase == 'source' ? 'source' : 'proxy'
  ticket = api_client.aspera_file_download_ticket_create(:asset_id => asset_id, :target => derivative_type)
  return false unless ticket.is_a?(Hash)

  host = ticket['server']
  username = ticket['username']
  password = ticket['password']
  token = ticket['token']

  original_file_name = ticket['fileName']

  file_path = ticket['path']
  target_path = destination_file_path

  FileUtils.mkdir_p(target_path) if target_path.end_with?('/') && !File.directory?(target_path)
  if target_path.end_with?('/') || File.directory?(target_path)
    target_path = File.join(target_path, original_file_name)
  end

  aspera_config = { }
  aspera_config['host'] = host
  aspera_config['username'] = username
  aspera_config['password'] = password
  aspera_config['token'] = token

  client = Envoi::Mam::Agent::TransferClient::Aspera.new(agent: self)
  client.download(aspera_config, file_path, target_path)
end
download_using_http(asset, destination_file_path, args) click to toggle source
# File lib/envoi/mam/mediasilo/agent.rb, line 116
def download_using_http(asset, destination_file_path, args)
  overwrite = args.fetch(:overwrite, false)
  derivative_type = args[:asset_derivative_type]
  api_client.asset_download_derivative(derivative_type, asset, destination_file_path, overwrite)
end
initialize_api_client(args = { }) click to toggle source
# File lib/envoi/mam/mediasilo/agent.rb, line 28
def initialize_api_client(args = { })
  @api_client = args[:api_client] || begin
    ms_config = config['mediasilo']
    mediasilo_hostname = ms_config['hostname']
    mediasilo_username = ms_config['username']
    mediasilo_password = ms_config['password']
    mediasilo_api_key = ms_config['api_key']

    client_args = { }
    client_args[:hostname] = mediasilo_hostname if mediasilo_hostname
    client_args[:username] = mediasilo_username if mediasilo_username
    client_args[:password] =  mediasilo_password if mediasilo_password
    client_args[:api_key] = mediasilo_api_key if mediasilo_api_key
    Ubiquity::MediaSilo::API::V3::Utilities.new(client_args)
  end

end
upload(args = { }) click to toggle source
# File lib/envoi/mam/mediasilo/agent.rb, line 123
def upload(args = { })
  file_path = args[:file_path]
  raise "File not found: '#{file_path}'" unless File.exists?(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]
  project_id = args[:project_id]
  folder_id = args[:folder_id]
  # asset_id = args[:asset_id]

  mediasilo_path = args[:mediasilo_path]
  project_name = args[:project_name]
  folder_name = args[:folder_name]

  if mediasilo_path || project_name || folder_name
    mediasilo_path ||= File.join(project_name, folder_name)

    check_path_result = api_client.check_path(mediasilo_path, false)
    found = check_path_result[:existing]

    project = found[:project] || { }
    project_id = project['id']

    folders = found[:folders]
    folder_id = (folders.last || { })['id']
  end

  # # preserve_path = args.fetch(:preserve_path, vidispine_storage_true)
  # preserve_path = args.fetch(:preserve_path, true)
  #
  # destination_path = args[:destination_path] || DEFAULT_DESTINATION_PATH
  # relative_path = preserve_path ? File.dirname(file_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?('/')

  # upload file
  case transfer_type
  when :aspera; do_upload_response = upload_using_aspera(file_path, args)
  else
    do_upload_response = upload_using_http(file_path)
    asset_url = do_upload_response[:asset_url]
  end

  return false unless do_upload_response[:success]

  asset_create_args = {
      'projectId' => project_id,
      'folderId' => folder_id,
      :source_url => asset_url
  }
  response = api_client.asset_create(asset_create_args)
end
upload_using_aspera(file_path, args = { }) click to toggle source
# File lib/envoi/mam/mediasilo/agent.rb, line 185
def upload_using_aspera(file_path, args = { })
  raise 'Upload using Aspera not yet Implemented.'

  file_name = File.basename(file_path)

  ticket =  api_client.aspera_file_upload_ticket_create(:file_name => file_name)

  host = ticket['server']
  username = ticket['username']
  password = ticket['password']
  token = ticket['token']
  destination = ticket['destination']

  target_path = destination

  aspera_config = { }
  aspera_config['host'] = host
  aspera_config['username'] = username
  aspera_config['password'] = password
  aspera_config['token'] = token

  client = AsperaTransferClient.new(agent: self)
  client.upload(aspera_config, file_path, target_path)
  { :success => true }
end
upload_using_faraday(url, file_path, options = { }) click to toggle source
# File lib/envoi/mam/mediasilo/agent.rb, line 211
def upload_using_faraday(url, file_path, options = { })
  headers = options[:headers]

  uri = URI(url)
  connection_url = "#{uri.scheme}://#{uri.host}:#{uri.port}"
  conn = Faraday.new(connection_url, { headers: headers, ssl: { } }) do |f|
    f.adapter :net_http
  end
  payload = Faraday::UploadIO.new(file_path, headers['Content-Type'])
  response = conn.put do |req|
    req.url  uri.path
    req.body = payload
    req.headers = headers
  end
end
upload_using_http(file_path) click to toggle source
# File lib/envoi/mam/mediasilo/agent.rb, line 227
def upload_using_http(file_path)
  file_name = File.basename(file_path)

  res = api_client.asset_upload_ticket_create(:file_name => file_name)
  uri_str = res['assetUrl']

  uri = URI.parse(uri_str)

  amz_date = res['amzDate']
  amz_acl = res['amzAcl']
  content_type = res['contentType']
  authorization = res['authorization']

  # file = FileToSend.open(file_path)
  #
  file = File.open(file_path)

  headers = {
      'x-amz-date' => amz_date,
      'x-amz-acl' => amz_acl,
      'Content-Type' => content_type,
      'Authorization' => authorization,
      'Content-Length' => file.size.to_s
  }


  response = upload_using_faraday(uri_str, file_path, { headers: headers })

  unless response
    req = Net::HTTP::Put.new(uri.request_uri)
    req['x-amz-date'] = amz_date
    req['x-amz-acl'] = amz_acl
    req[CaseSensitiveHeaderKey.new('Content-Type')] = content_type
    res[CaseSensitiveHeaderKey.new('Authorization')] = authorization
    # req[CaseSensitiveHeaderKey.new('Transfer-Encoding')] = 'chunked'
    req[CaseSensitiveHeaderKey.new('Content-Length')] = file.size

    req.body_stream = file
    # req.body = file

    request = req
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    logger.debug { %(REQUEST: #{request.method} https://#{http.address}:#{http.port}#{request.path} HEADERS: #{request.to_hash.inspect} #{api_client.http_client.log_request_body and request.request_body_permitted? ? "BODY: #{api_client.http_client.format_body_for_log_output(request)}" : ''}) }

    response = res = http.request(req)
  end
  logger.debug { %(RESPONSE: #{response.inspect})} #HEADERS: #{response.to_hash.inspect} #{api_client.http_client.log_response_body and response.respond_to?(:body) ? "BODY: #{api_client.http_client.format_body_for_log_output(response)}" : ''}) }
  response_code = response.respond_to?(:status) ? response.status.to_s : response.code
  { :success => response_code.start_with?('2'), asset_url: uri_str }
end