Class: Envoi::Mam::Iconik::Agent

Inherits:
Agent
  • Object
show all
Defined in:
lib/envoi/mam/iconik/agent.rb

Constant Summary collapse

DEFAULT_FORMAT_NAME =
'ORIGINAL'
DEFAULT_DESTINATION_PATH =
'.'

Constants inherited from Agent

Agent::VERSION

Instance Attribute Summary

Attributes inherited from Agent

#api_client, #config, #initial_args, #logger

Instance Method Summary collapse

Methods inherited from Agent

#dry_run?, #initialize, #initialize_logger, load_config_and_init, load_config_from_file, load_config_from_service, load_from_config_file, load_from_config_service, #notify, #run_operation, #shell_execute

Constructor Details

This class inherits a constructor from Envoi::Mam::Agent

Instance Method Details

#after_initializeObject



17
18
19
# File 'lib/envoi/mam/iconik/agent.rb', line 17

def after_initialize
  @default_format_name = initial_args[:default_format_name] || DEFAULT_FORMAT_NAME
end

#download(args = { }) ⇒ Object



38
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
66
67
# File 'lib/envoi/mam/iconik/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) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/envoi/mam/iconik/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_configObject



21
22
23
# File 'lib/envoi/mam/iconik/agent.rb', line 21

def iconik_config
  config['iconik']
end

#initialize_api_client(args = { }) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/envoi/mam/iconik/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 = { }) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/envoi/mam/iconik/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}"}
    return file_paths.map { |fp| upload(args.merge(file_path: fp))}
  end
  logger.debug { "Preparing to upload '#{file_path}'" }

  ingest_after_upload = args[:ingest_after_upload]
  analyze_asset = args[:analyze_asset]

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

  if ingest_after_upload
    asset_create_response = api_client.asset_add_using_file_path(file_path: path_on_storage,
                                                                 storage_id: storage_id,
                                                                 file_size: file_size)
    if analyze_asset
      asset_id = asset_create_response[:asset]['id']
      api_client.asset_analyze(:asset_id => asset_id)
    end

  end

end