Class: Envoi::Mam::MediaSilo::Agent
- Inherits:
-
Agent
- Object
- Agent
- Envoi::Mam::MediaSilo::Agent
show all
- Defined in:
- lib/envoi/mam/mediasilo/agent.rb
Defined Under Namespace
Classes: CaseSensitiveHeaderKey, File
Constant Summary
collapse
- DEFAULT_DOWNLOAD_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
#after_initialize, #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
Instance Method Details
#download(args = { }) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# 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 = { }) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# 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) ⇒ Object
116
117
118
119
120
|
# 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 = { }) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# 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 = { }) ⇒ Object
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
|
# 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]
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
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 = { }) ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# 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 = { }) ⇒ Object
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
# File 'lib/envoi/mam/mediasilo/agent.rb', line 211
def upload_using_faraday(url, file_path, options = { })
= options[:headers]
uri = URI(url)
connection_url = "#{uri.scheme}://#{uri.host}:#{uri.port}"
conn = Faraday.new(connection_url, { headers: , ssl: { } }) do |f|
f.adapter :net_http
end
payload = Faraday::UploadIO.new(file_path, ['Content-Type'])
response = conn.put do |req|
req.url uri.path
req.body = payload
req. =
end
end
|
#upload_using_http(file_path) ⇒ Object
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
# 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 = File.open(file_path)
= {
'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: })
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('Content-Length')] = file.size
req.body_stream = 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})} response_code = response.respond_to?(:status) ? response.status.to_s : response.code
{ :success => response_code.start_with?('2'), asset_url: uri_str }
end
|