Class: Ubiquity::MediaSilo::API::V3::Utilities
- Inherits:
-
Client
- Object
- Client
- Ubiquity::MediaSilo::API::V3::Utilities
show all
- Defined in:
- lib/ubiquity/mediasilo/api/v3/utilities.rb
Instance Attribute Summary
Attributes inherited from Client
#batch_requests, #http_client, #http_response, #logger, #request, #response
Instance Method Summary
collapse
-
#asset_create_using_path(args = { }) ⇒ Object
(also: #asset_create_using_file_path)
Creates an asset building any missing parts of the path (Project/Folder/Asset).
-
#asset_derivatives_transform_to_hash(derivatives) ⇒ Object
-
#asset_download_derivative(derivative_name, asset, destination_file_path, overwrite = false, field_name = 'url') ⇒ Object
-
#asset_download_poster_frame_file(asset, destination_file_path, overwrite = false) ⇒ Object
-
#asset_download_proxy_file(asset, destination_file_path, overwrite = false) ⇒ Hash
See (MediaSilo#download_file).
-
#asset_download_proxy_poster_frame_file(asset, destination_file_path, overwrite = false) ⇒ Object
-
#asset_download_proxy_thumbnail_file(asset, destination_file_path, overwrite = false) ⇒ Object
-
#asset_download_resource(download_file_path, destination_file_path, overwrite = false) ⇒ Object
-
#asset_download_source_file(asset, destination_file_path, overwrite = false) ⇒ Hash
See (MediaSilo#download_file).
-
#asset_edit_extended(asset_id, args = { }, options = { }) ⇒ Object
-
#asset_get_by_field_search(search_field_name, search_value, project_id = nil, folder_id = nil, options = { }) ⇒ Array
-
#asset_get_by_filename(asset_name, project_id = nil, folder_id = nil, options = { }) ⇒ Object
-
#asset_get_by_id_extended(args = { }, options = { }) ⇒ Object
-
#asset_get_by_title(asset_name, project_id = nil, folder_id = nil, options = { }) ⇒ Object
-
#assets_get_extended(args = { }, options = { }) ⇒ Object
-
#default_case_sensitive_search ⇒ Object
-
#download_file(download_file_path, destination_file_path, overwrite = false) ⇒ Hash
Downloads a file from a URI or file location and saves it to a local path.
-
#folder_get_by_name(project_id, folder_name, parent_id, options = { }) ⇒ Object
-
#metadata_transform_to_hash(metadata_in) ⇒ Object
-
#path_check(path, path_contains_asset = false, options = { }) ⇒ Object
(also: #check_path)
Checks to see if a project/folder/asset path exists and records each as existing or missing.
-
#path_create(path, contains_asset = false, asset_url = nil, metadata = nil, options = { }) ⇒ Hash
(also: #create_path)
Calls check_path to see if any part of a project/folder/asset path are missing from MediaSilo and creates any part that is missing.
-
#path_delete(path, options = { }) ⇒ Object
-
#path_delete_by_id(project_id, folder_id = 0, recursive = false, include_assets = false, delete_contents_only = true, options = { }) ⇒ Object
Deletes a project’s and/or folder’s contents.
-
#path_resolve(path, path_contains_asset = false, options = { }) ⇒ Object
(also: #resolve_path)
-
#project_get_by_name(args = { }, options = { }) ⇒ Object
-
#refine_asset_get_by_field_search_results(field_name, search_value, assets, options) ⇒ Object
Refines asset_get_by_field_search results to exact (full string case sensitive) matches.
-
#resolve_folder_path(project_id, path, parent_id = nil) ⇒ Object
Takes a file system type path and resolves the MediaSilo id’s for each of the folders of that path.
Methods inherited from Client
#aspera_file_download_ticket_create, #aspera_file_upload_ticket_create, #asset_copy_to_folder, #asset_copy_to_project, #asset_create, #asset_delete_by_id, #asset_edit, #asset_get_by_id, #asset_move_to_folder, #asset_move_to_project, #asset_tag_add, #asset_tag_delete, #asset_upload, #asset_upload_ticket_create, #asset_watermark_trigger, #assets_get, #assets_get_by_folder_id, #assets_get_by_project_id, #batch_execute, #batch_mode=, #batch_mode?, #batch_mode_enable, #error_message, #folder_create, #folder_delete, #folder_get_by_id, #folders_get_by_parent_id, #folders_get_by_project_id, #initialize, #initialize_http_client, #initialize_logger, #metadata_create_or_update, #metadata_delete, #metadata_get, #metadata_replace, #paginator, #process_request, #process_request_using_class, #project_create, #project_delete, #project_get_by_id, #project_watermark_settings_get, #project_watermark_settings_set, #projects_get, #quicklink_create, #quicklink_share, #success?, #tag_edit, #tags_get, #user_delete, #users_get
Instance Method Details
#asset_create_using_path(args = { }) ⇒ Object
Also known as:
asset_create_using_file_path
Creates an asset building any missing parts of the path (Project/Folder/Asset)
Required Parameters
:url
:mediasilo_path or :file_path
Optional Parameters
:metadata
:overwrite_existing_asset
:asset_search_field_name
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
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
109
110
111
112
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 48
def asset_create_using_path(args = { })
file_url = args[:url] || args[:file_url] || args[:source_url]
raise ArgumentError ':url is a required parameter.' unless file_url
file_path = args[:mediasilo_path] || args[:file_path]
raise ArgumentError ':mediasilo_path is a required parameter.' unless file_path
ms_metadata = args[:metadata]
asset_title = args[:title]
asset_description = args[:description]
additional_asset_create_params = { }
additional_asset_create_params['title'] = asset_title if asset_title
additional_asset_create_params['description'] = asset_description if asset_description
overwrite_existing_asset = args.fetch(:overwrite_existing_asset, false)
asset_search_field_name = args[:asset_search_field_name] || :filename
path_create_options = {
:overwrite_existing_asset => overwrite_existing_asset,
:additional_asset_create_params => additional_asset_create_params,
:asset_search_field_name => asset_search_field_name
}
begin
result = path_create(file_path, true, file_url, ms_metadata, path_create_options)
end
output_values = { }
output_values[:result] = result
return false unless result and result.has_key?(:asset)
result_asset = result[:asset]
if result_asset == false
ms_asset_id = false
elsif result[:asset_missing] == false
ms_asset_id = result[:existing][:asset]['id'] elsif result_asset.is_a?(Array)
ms_asset_id = result_asset[1]
else
ms_asset_id = result_asset['id']
ms_asset_id ||= result_asset
end
if ms_asset_id
output_values[:asset_id] = ms_asset_id
return output_values
else
return false
end
end
|
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 115
def asset_derivatives_transform_to_hash(derivatives)
return { } unless derivatives.is_a?(Array)
_derivatives = derivatives.map do |d|
strategies = d['strategies']
d['strategies'] = Hash[strategies.map { |s| [ s['type'], s ] }] if strategies
[ d['type'], d ]
end
Hash[_derivatives]
end
|
#asset_download_derivative(derivative_name, asset, destination_file_path, overwrite = false, field_name = 'url') ⇒ Object
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 154
def asset_download_derivative(derivative_name, asset, destination_file_path, overwrite = false, field_name = 'url')
return asset.map { |a| asset_download_derivative(derivative_name, a, destination_file_path, overwrite) } if asset.is_a?(Array)
asset = asset_get_by_id_extended( { :asset_id => asset }, :include_derivatives_hash => true ) if asset.is_a? String
_derivatives = asset['derivatives_hash'] || begin
asset_derivatives_transform_to_hash(asset['derivatives'])
end
file_to_download = _derivatives[derivative_name][field_name]
asset_download_resource(file_to_download, destination_file_path, overwrite)
end
|
#asset_download_poster_frame_file(asset, destination_file_path, overwrite = false) ⇒ Object
165
166
167
168
169
170
171
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 165
def asset_download_poster_frame_file(asset, destination_file_path, overwrite = false)
return asset.map { |a| asset_download_poster_frame_file(a, destination_file_path, overwrite) } if asset.is_a?(Array)
asset = asset_get_by_id(:asset_id => asset ) if asset.is_a? String
file_to_download = asset['posterFrame']
asset_download_resource(file_to_download, destination_file_path, overwrite)
end
|
#asset_download_proxy_file(asset, destination_file_path, overwrite = false) ⇒ Hash
Returns see (MediaSilo#download_file).
177
178
179
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 177
def asset_download_proxy_file(asset, destination_file_path, overwrite = false)
asset_download_derivative('proxy', asset, destination_file_path, overwrite)
end
|
#asset_download_proxy_poster_frame_file(asset, destination_file_path, overwrite = false) ⇒ Object
199
200
201
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 199
def asset_download_proxy_poster_frame_file(asset, destination_file_path, overwrite = false)
asset_download_derivative('proxy', asset, destination_file_path, overwrite, 'posterFrame')
end
|
#asset_download_proxy_thumbnail_file(asset, destination_file_path, overwrite = false) ⇒ Object
203
204
205
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 203
def asset_download_proxy_thumbnail_file(asset, destination_file_path, overwrite = false)
asset_download_derivative('proxy', asset, destination_file_path, overwrite, 'thumbnail')
end
|
#asset_download_resource(download_file_path, destination_file_path, overwrite = false) ⇒ Object
184
185
186
187
188
189
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 184
def asset_download_resource(download_file_path, destination_file_path, overwrite = false)
destination_file_path = File.join(destination_file_path, File.basename(URI.decode(download_file_path))) if File.directory? destination_file_path
download_file(download_file_path, destination_file_path, overwrite)
end
|
#asset_download_source_file(asset, destination_file_path, overwrite = false) ⇒ Hash
Returns see (MediaSilo#download_file).
195
196
197
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 195
def asset_download_source_file(asset, destination_file_path, overwrite = false)
asset_download_derivative('source', asset, destination_file_path, overwrite)
end
|
#asset_edit_extended(asset_id, args = { }, options = { }) ⇒ Object
217
218
219
220
221
222
223
224
225
226
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
278
279
280
281
282
283
284
285
286
287
288
289
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 217
def asset_edit_extended(asset_id, args = { }, options = { })
logger.debug { "Asset Edit Extended: #{asset_id} ARGS: #{args.inspect}" }
if asset_id.is_a?(Hash)
options = args.dup
args = asset_id.dup
asset_id = args[:asset_id]
raise ArgumentError, 'Error Editing Asset. Missing required argument :asset_id' unless asset_id
else
args = args.dup
args[:asset_id] = asset_id
end
ms_metadata = args.delete(:metadata) { false }
mirror_metadata = args.delete(:mirror_metadata) { false }
add_tag_to_asset = args.delete(:tags_to_add) { [ ] }
add_tag_to_asset = args.delete(:tags_to_add_to_asset) { add_tag_to_asset }
remove_tag_from_asset = args.delete(:tags_to_remove) { [ ] }
remove_tag_from_asset = args.delete(:tags_to_remove_from_asset) { remove_tag_from_asset }
_response = { :success => false }
if args[:title] or args[:description]
result = asset_edit(args)
_response[:asset_edit_result] = result
_response[:asset_edit_response] = response
_response[:asset_edit_success] = success?
unless success?
_response[:error_message] = "Error Editing Asset. #{error_message}"
return _response
end
end
if ms_metadata.is_a?(Hash)
if mirror_metadata
result = metadata_mirror(:asset_id => asset_id, :metadata => ms_metadata)
else
result = metadata_create_or_update(:asset_id => asset_id, :metadata => ms_metadata)
end
_response[:metadata_edit_result] = result
_response[:metadata_edit_response] = response
_response[:metadata_edit_success] = success?
unless success?
_response[:error_message] = "Error Editing Asset's Metadata. #{error_message}"
return _response
end
end
batch_execute do
unless remove_tag_from_asset.nil? or remove_tag_from_asset.empty?
[*remove_tag_from_asset].uniq.each { |tag_to_remove| asset_tag_remove(:asset_id => asset_id, :tag => tag_to_remove) if tag_to_remove.is_a?(String); }
end
unless add_tag_to_asset.nil? or add_tag_to_asset.empty?
[*add_tag_to_asset].uniq.each { |tag_to_add| asset_tag_add(:asset_id => asset_id, :tags => tag_to_add) if tag_to_add.is_a?(String) }
end
end
_response[:success] = true
_response
end
|
#asset_get_by_field_search(search_field_name, search_value, project_id = nil, folder_id = nil, options = { }) ⇒ Array
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 298
def asset_get_by_field_search(search_field_name, search_value, project_id = nil, folder_id = nil, options = { })
path = if folder_id and !folder_id == 0
limit_to_project_root = false
"/folders/#{folder_id}/assets"
elsif project_id
limit_to_project_root = options.fetch(:limit_to_project_root, true)
"/projects/#{project_id}/assets"
else
limit_to_project_root = false
'/assets'
end
_search_field_name = search_field_name.to_s
search_operator_default = search_value.is_a?(Array) ? 'in' : nil
search_operator = options[:operator] || search_operator_default
case search_operator
when nil
search_field_query = { _search_field_name => search_value }
else
search_field_query = { _search_field_name => %({"#{search_operator}":"#{[*search_value].join(',')}"}) }
end
query = (options[:query] || { }).merge(search_field_query)
assets = http_client.get(path, query)
assets = [ ] if http_client.response.code == '404'
limit_to_project_root ? assets.delete_if { |v| v['folderId'] } : assets
end
|
#asset_get_by_filename(asset_name, project_id = nil, folder_id = nil, options = { }) ⇒ Object
422
423
424
425
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 422
def asset_get_by_filename(asset_name, project_id = nil, folder_id = nil, options = { })
assets = asset_get_by_field_search(:filename, asset_name, project_id, folder_id, options)
refine_asset_get_by_field_search_results('fileName', asset_name, assets, options)
end
|
#asset_get_by_id_extended(args = { }, options = { }) ⇒ Object
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 331
def asset_get_by_id_extended(args = { }, options = { })
include_metadata = options.delete(:include_metadata)
include_derivatives_hash = options.delete(:include_derivatives_hash) { true }
transform_metadata = options.delete(:transform_metadata) { true }
asset_id = args[:asset_id]
asset = asset_get_by_id({ :asset_id => asset_id }, options)
if include_metadata
md = metadata_get( { :asset_id => asset['id'] }, options )
md = metadata_transform_to_hash(md) if transform_metadata
asset['metadata'] = md
end
if include_derivatives_hash
_derivatives = asset['derivatives']
asset['derivatives_hash'] = asset_derivatives_transform_to_hash(_derivatives)
end
asset
end
|
#asset_get_by_title(asset_name, project_id = nil, folder_id = nil, options = { }) ⇒ Object
427
428
429
430
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 427
def asset_get_by_title(asset_name, project_id = nil, folder_id = nil, options = { })
assets = asset_get_by_field_search(:title, asset_name, project_id, folder_id, options)
refine_asset_get_by_field_search_results('title', asset_name, assets, options)
end
|
#assets_get_extended(args = { }, options = { }) ⇒ Object
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 358
def assets_get_extended(args = { }, options = { })
include_metadata = options.delete(:include_metadata)
include_derivatives_hash = options.delete(:include_derivatives_hash) { true }
transform_metadata = options.delete(:transform_metadata) { true }
assets = assets_get(args, options)
assets.map! do |a|
if include_metadata
md = metadata_get(:asset_id => a['id'])
md = metadata_transform_to_hash(md) if transform_metadata
a['metadata'] = md
end
if include_derivatives_hash
_derivatives = a['derivatives'].map do |d|
strategies = d['strategies']
d['strategies'] = Hash[strategies.map { |s| [s['type'], s] }] if strategies
[ d['type'], d ]
end
a['derivatives_hash'] = Hash[_derivatives]
end
a
end if (include_metadata || include_derivatives_hash)
assets
end
|
#default_case_sensitive_search ⇒ Object
34
35
36
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 34
def default_case_sensitive_search
true
end
|
#download_file(download_file_path, destination_file_path, overwrite = false) ⇒ Hash
Downloads a file from a URI or file location and saves it to a local path
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 139
def download_file(download_file_path, destination_file_path, overwrite = false)
logger.debug { "Downloading '#{download_file_path}' -> '#{destination_file_path}' Overwrite: #{overwrite}" }
file_existed = 'unknown'
if overwrite or not(file_existed = File.exists?(destination_file_path))
File.open(destination_file_path, 'wb') { |tf|
open(download_file_path) { |sf| tf.write sf.read }
}
file_downloaded = true
else
file_downloaded = false
end
return { :download_file_path => download_file_path, :overwrite => overwrite, :file_downloaded => file_downloaded, :destination_file_existed => file_existed, :destination_file_path => destination_file_path }
end
|
#folder_get_by_name(project_id, folder_name, parent_id, options = { }) ⇒ Object
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 432
def folder_get_by_name(project_id, folder_name, parent_id, options = { })
logger.debug { "Searching For Folder by Name: '#{folder_name}' Project Id: '#{project_id}' Parent Id: '#{parent_id}'"}
case_sensitive = options.fetch(:case_sensitive, default_case_sensitive_search)
return_all_matches = !options.fetch(:return_first_match, false)
folders = options[:folders] || (parent_id ? folders_get_by_parent_id(:parent_id => parent_id) : folders_get_by_project_id(:project_id => project_id))
return false unless folders
folders.dup.delete_if do |folder|
folder_name_to_test = folder['name']
folder_name_to_test.upcase! unless case_sensitive
no_match = (folder_name_to_test != folder_name)
return folder unless no_match or return_all_matches
no_match
end
return nil unless return_all_matches
folders
end
|
543
544
545
546
547
548
549
550
551
552
553
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 543
def metadata_transform_to_hash(metadata_in)
case metadata_in
when Array
return Hash[ metadata_in.map { |m| [m['key'], m['value'] ] } ]
when Hash
return metadata_in
else
return { }
end
end
|
#path_check(path, path_contains_asset = false, options = { }) ⇒ Object
Also known as:
check_path
Checks to see if a project/folder/asset path exists and records each as existing or missing
(@see #resolve_path)
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 562
def path_check(path, path_contains_asset = false, options = { })
return false unless path
path = path[1..-1] while path.start_with? '/'
path_ary = path.split('/')
existing_path_result = resolve_path(path, path_contains_asset, options)
existing_path_ary = existing_path_result[:id_path_ary]
check_path_length = path_ary.length
existing_path_length = existing_path_ary.length
missing_path = path_ary.drop(existing_path_length)
missing_path_length = missing_path.length
if missing_path_length > 0
if missing_path_length == check_path_length
project_missing = true
if path_contains_asset
asset_missing = true
if check_path_length > 2
searched_folders = true
folder_missing = true
else
searched_folders = false
folder_missing = false
end
else
if check_path_length > 1
searched_folders = true
folder_missing = true
else
searched_folders = false
folder_missing = false
end
end
else
project_missing = false
if path_contains_asset
asset_missing = true
if missing_path_length == 1
folder_missing = false
searched_folders = check_path_length > 2
else
if check_path_length > 2
searched_folders = true
folder_missing = true
else
searched_folders = false
folder_missing = false
end
end
else
searched_folders = true
folder_missing = true
end
end
else
searched_folders = !existing_path_result[:folders].empty?
project_missing = folder_missing = asset_missing = false
end
{
:check_path_ary => path_ary,
:existing => existing_path_result,
:missing_path => missing_path,
:searched_folders => searched_folders,
:project_missing => project_missing,
:folder_missing => folder_missing,
:asset_missing => asset_missing,
}
end
|
#path_create(path, contains_asset = false, asset_url = nil, metadata = nil, options = { }) ⇒ Hash
Also known as:
create_path
Calls check_path to see if any part of a project/folder/asset path are missing from MediaSilo and creates any part that is missing
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 697
def path_create(path, contains_asset = false, asset_url = nil, metadata = nil, options = { })
overwrite_asset = options.fetch(:overwrite_asset, false)
additional_asset_create_params = options[:additional_asset_create_params] || { }
path_creation_delay = options[:path_creation_delay] || 10
asset_search_field_name = options[:asset_search_field_name] || :filename
cp_result = check_path(path, contains_asset, :asset_search_field_name => asset_search_field_name)
logger.debug { "CHECK PATH RESULT #{cp_result.inspect}" }
return false unless cp_result
project_missing = cp_result[:project_missing]
folder_missing = cp_result[:folder_missing]
asset_missing = cp_result[:asset_missing]
existing = cp_result[:existing]
asset = existing[:asset]
searched_folders = cp_result[:searched_folders]
missing_path = cp_result[:missing_path]
project_name = cp_result[:check_path_ary][0]
if project_missing
logger.debug { "Missing Project - Creating Project '#{project_name}'" }
project = project_create(project_name)
raise "Error Creating Project. Response: #{project}" unless project.is_a?(Hash)
cp_result[:project] = project
project_id = project['id']
missing_path.shift
logger.debug { "Created Project '#{project_name}' - #{project_id}" }
else
project_id = existing[:id_path_ary].first
end
if searched_folders
if folder_missing
parent_folder_id = (existing[:id_path_ary].length <= 1) ? 0 : existing[:id_path_ary].last
asset_name = missing_path.pop if contains_asset
previous_missing = project_missing
missing_path.each do |folder_name|
sleep path_creation_delay if path_creation_delay and previous_missing
begin
logger.debug { "Creating folder '#{folder_name}' parent id: #{parent_folder_id} project id: #{project_id}" }
new_folder = folder_create(:name => folder_name, :project_id => project_id, :parent_id => parent_folder_id)
raise "Error Creating Folder. Response: #{new_folder}" unless new_folder.is_a?(Hash)
logger.debug { "New Folder: #{new_folder.inspect}" }
parent_folder_id = new_folder['id']
logger.debug { "Folder Created #{new_folder} - #{parent_folder_id}" }
rescue => e
raise e.prefix_message("Failed to create folder '#{folder_name}' parent id: '#{parent_folder_id}' project id: '#{project_id}'. Exception:")
end
previous_missing = true
end
else
if contains_asset and not asset_missing
parent_folder_id = existing[:id_path_ary].fetch(-2)
else
parent_folder_id = existing[:id_path_ary].last
end
end
else
parent_folder_id = 0
end
if contains_asset
additional_asset_create_params = { } unless additional_asset_create_params.is_a?(Hash)
additional_asset_create_params['folderId'] = parent_folder_id
additional_asset_create_params['projectId'] = project_id
additional_asset_create_params[:metadata] = metadata
additional_asset_create_params[:source_url] = asset_url
if asset_missing
asset = asset_create(additional_asset_create_params)
raise "Error Creating Asset: #{asset.inspect} Args: #{additional_asset_create_params.inspect}" unless success?
else
if overwrite_asset
asset_id = existing[:id_path_ary].last
begin
raise "Error Message: #{error_message}" unless asset_delete(asset_id)
rescue => e
raise e.prefix_message("Error Deleting Existing Asset. Asset ID: #{asset_id} Exception: ")
end
asset = asset_create(additional_asset_create_params)
raise "Error Creating Asset: #{asset.inspect} Args: #{additional_asset_create_params.inspect}" unless success?
end
end
additional_asset_create_params = additional_asset_create_params.delete_if { |k,v| asset[k] == v }
asset_edit_extended(asset['id'], additional_asset_create_params) unless additional_asset_create_params.empty?
cp_result[:asset] = asset
end
result = cp_result.merge({ :project_id => project_id, :parent_folder_id => parent_folder_id })
logger.debug { "Create Missing Path Result: #{result.inspect}" }
return result
end
|
#path_delete(path, options = { }) ⇒ Object
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 824
def path_delete(path, options = { })
raise_exception_on_error = options.fetch(:raise_exception_on_error, true)
recursive = (options.fetch(:recursive, false) === true) ? true : false
include_assets = (options.fetch(:include_assets, false) === true) ? true : false
path = path[1..-1] if path.start_with? '/' path_ary = path.split('/')
raise ArgumentError, 'Path is empty. Nothing to do.' if path_ary.empty?
if path_ary.last == '*'
path_ary.pop
if path_ary.empty?
delete_all_projects = options.fetch(:delete_all_projects)
raise ArgumentError, 'Wildcard Project Deletion is not Enabled.' unless (delete_all_projects === true)
projects = projects_get
return projects.map { |project| path_delete(project['name'], options)}
end
delete_contents_only = true
else
delete_contents_only = options.fetch(:delete_contents_only, false)
end
result = check_path(path_ary.join('/'))
raise "Error checking path. '#{error_message}'" unless result
existing_path = result[:existing][:id_path_ary]
missing_path = result[:missing_path]
raise "Path not found. Path: '#{path}' Check Path Result: #{result.inspect}" unless missing_path.empty?
id_path_ary = existing_path
project_id = id_path_ary.shift
if id_path_ary.empty?
folder_id = 0
else
folder_id = id_path_ary.last
end
path_delete_by_id(project_id, folder_id, recursive, include_assets, delete_contents_only, options)
rescue ArgumentError, RuntimeError => e
raise e if raise_exception_on_error
return false
end
|
#path_delete_by_id(project_id, folder_id = 0, recursive = false, include_assets = false, delete_contents_only = true, options = { }) ⇒ Object
Deletes a project’s and/or folder’s contents.
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 892
def path_delete_by_id(project_id, folder_id = 0, recursive = false, include_assets = false, delete_contents_only = true, options = { })
dry_run = options.fetch(:dry_run, false)
delete_assets_only = options.fetch(:delete_assets_only, false)
raise ArgumentError, 'include_assets must be true to use the delete_assets_only option.' if delete_assets_only and !include_assets
@logger.debug { "Deleting Path By ID - Project ID: #{project_id} Folder ID: #{folder_id} Recursive: #{recursive} Include Assets: #{include_assets} Delete Contents Only: #{delete_contents_only} Options: #{options.inspect}" }
if folder_id and folder_id != 0
folders = folders_get_by_parent_id(folder_id) || [ ]
else
folders = folders_get_by_project_id(project_id) || [ ]
end
folders = [ ] unless folders.is_a?(Array)
if recursive
folders = [ ] if folders.is_a?(String)
total_folders = folders.length
folder_counter = 0
folders.delete_if do |folder|
folder_counter += 1
@logger.debug { "Deleting Contents of Folder #{folder_counter} of #{total_folders} - #{folder}" }
path_delete_by_id(project_id, folder['id'], recursive, include_assets, delete_assets_only, options)
end
end
if include_assets
if folder_id == 0
assets = assets_get_by_project_id(:id => project_id)
else
assets = assets_get_by_folder_id(:id => folder_id)
end
assets = [ ] unless assets.is_a?(Array)
total_assets = assets.length
asset_counter = 0
response = assets.map { |asset| asset_delete(asset['id']) }
assets = [ ]
else
assets = [ ] end
unless delete_contents_only or delete_assets_only
if folders.empty? and assets.empty?
if folder_id === 0
@logger.debug { "Deleting Project #{project_id}" }
return ( dry_run ? true : project_delete(project_id) )
else
@logger.debug { "Deleting Folder #{folder_id}" }
return ( dry_run ? true : folder_delete(folder_id) )
end
else
return true if dry_run
warn "Assets remaining in project/folder: #{project_id}/#{folder_id} : Assets: #{assets.inspect}" unless assets.empty?
warn "Folders remaining in project/folder: #{project_id}/#{folder_id} : Folders: #{folders.inspect}" unless folders.empty?
return false
end
end
return true
end
|
#path_resolve(path, path_contains_asset = false, options = { }) ⇒ Object
Also known as:
resolve_path
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 1005
def path_resolve(path, path_contains_asset = false, options = { })
logger.debug { "Resolving Path: '#{path}' Path Contains Asset: #{path_contains_asset} Options: #{options}" }
return_first_matching_asset = options.fetch(:return_first_matching_asset, true)
id_path_ary = [ ]
name_path_ary = [ ]
if path.is_a?(String)
path = path[1..-1] while path.start_with?('/')
path_ary = path.split('/')
elsif path.is_a?(Array)
path_ary = path.dup
else
raise ArgumentError, "path is required to be a String or an Array. Path Class Name: #{path.class.name}"
end
asset_name = path_ary.pop if path_contains_asset
project_name = path_ary.shift
raise ArgumentError, 'path must contain a project name.' unless project_name
logger.debug { "Search for Project Name: #{project_name}" }
project = project_get_by_name(project_name, :return_first_match => true, :projects => options[:projects])
return {
:name_path => '/',
:name_path_ary => [ ],
:id_path => '/',
:id_path_ary => [ ],
:project => nil,
:asset => nil,
:folders => [ ]
} if !project or project.empty?
project_id = project['id']
id_path_ary << project_id
name_path_ary << project_name
parsed_folders = (project && project['folderCount'] > 0) ? resolve_folder_path(project_id, path_ary) : nil
if parsed_folders.nil?
asset_folder_id = 0
folders = []
else
id_path_ary.concat(parsed_folders[:id_path_ary])
name_path_ary.concat(parsed_folders[:name_path_ary])
asset_folder_id = parsed_folders[:id_path_ary].last if path_contains_asset
folders = parsed_folders.fetch(:folder_ary, [])
end
asset = nil
if path_contains_asset and (asset_folder_id or path_ary.length == 2)
asset_name_field = options[:asset_search_field_name] || :filename
case asset_name_field.to_s.downcase.to_sym
when :filename, 'filename'
asset = asset_get_by_filename(asset_name, project_id, asset_folder_id, :return_first_match => return_first_matching_asset)
when :title, 'title'
asset = asset_get_by_title(asset_name, project_id, asset_folder_id, :return_first_match => return_first_matching_asset)
else
raise ArgumentError, ":asset_name_field value is not a valid option. It must be :title or :filename. Current value: #{asset_name_field}"
end
if asset
if asset.empty?
elsif asset.is_a?(Array)
id_path_ary << asset.map { |_asset| _asset['id'] }
name_path_ary << asset.map { |_asset| _asset['fileName'] }
else
id_path_ary << asset['id']
name_path_ary << asset['fileName']
end
end
end
return {
:name_path => "/#{name_path_ary.join('/')}",
:name_path_ary => name_path_ary,
:id_path => "/#{id_path_ary.join('/')}",
:id_path_ary => id_path_ary,
:project => project,
:asset => asset,
:folders => folders
}
end
|
#project_get_by_name(args = { }, options = { }) ⇒ Object
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 976
def project_get_by_name(args = { }, options = { })
project_name = case args
when String; args
when Hash; args[:name] || args[:project_name]
end
case_sensitive = options.fetch(:case_sensitive, default_case_sensitive_search)
return_all_matches = !options.fetch(:return_first_match, false)
projects = options[:projects] || projects_get
logger.debug { "Searching Projects: #{projects.inspect}" }
return false unless projects
project_name.upcase! unless case_sensitive
projects = projects.dup.delete_if do |project|
project_name_to_test = project['name']
project_name_to_test.upcase! unless case_sensitive
no_match = (project_name_to_test != project_name)
logger.debug { "Comparing: #{project_name_to_test} #{no_match ? '!' : '='}= #{project_name}"}
return project unless no_match or return_all_matches
no_match
end
return nil unless return_all_matches
projects
end
|
#refine_asset_get_by_field_search_results(field_name, search_value, assets, options) ⇒ Object
Refines asset_get_by_field_search results to exact (full string case sensitive) matches
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 405
def refine_asset_get_by_field_search_results(field_name, search_value, assets, options)
return unless assets
return_first_match = options.fetch(:return_first_match, false)
match_full_string = options.fetch(:match_full_string, true)
case_sensitive = options.fetch(:case_sensitive, default_case_sensitive_search)
search_value = search_value.to_s.downcase unless case_sensitive
method = return_first_match ? :drop_while : :delete_if
assets.send(method) do |asset|
asset_value = case_sensitive ? asset[field_name] : asset[field_name].to_s.downcase
nomatch = match_full_string ? (asset_value != search_value) : (!asset_value.include?(search_value))
nomatch
end if assets and (match_full_string or case_sensitive)
return assets.first if assets and return_first_match
assets
end
|
#resolve_folder_path(project_id, path, parent_id = nil) ⇒ Object
Takes a file system type path and resolves the MediaSilo id’s for each of the folders of that path
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
|
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 1105
def resolve_folder_path(project_id, path, parent_id = nil)
if path.is_a?(Array)
path_ary = path.dup
elsif path.is_a? String
path = path[1..-1] while path.start_with?('/')
path_ary = path.split('/')
end
return nil if !path_ary or path_ary.empty?
id_path_ary = [ ]
name_path_ary = [ ]
folder_name = path_ary.shift
name_path_ary << folder_name
folder = folder_get_by_name(project_id, folder_name, parent_id, :return_first_match => true)
return nil unless folder
folder_ary = [ folder ]
folder_id = folder['id']
id_path_ary << folder_id.to_s
resolved_folder_path = (folder and folder['folderCount'] > 0) ? resolve_folder_path(project_id, path_ary, folder_id) : nil
unless resolved_folder_path.nil?
id_path_ary.concat(resolved_folder_path[:id_path_ary] || [ ])
name_path_ary.concat(resolved_folder_path[:name_path_ary] || [ ])
folder_ary.concat(resolved_folder_path[:folder_ary] || [ ])
end
return {
:id_path_ary => id_path_ary,
:name_path_ary => name_path_ary,
:folder_ary => folder_ary
}
end
|