Class: Ubiquity::MediaSilo::API::V3::Utilities

Inherits:
Client
  • Object
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

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

Constructor Details

This class inherits a constructor from Ubiquity::MediaSilo::API::V3::Client

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

   = 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

  #logger.info { "Creating Asset on MediaSilo Using File Path: '#{file_path}'. File URL: #{file_url}" }

  #ms_uuid = ms.asset_create(file_url, { }, ms_project)
  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, , path_create_options)
    # rescue => e
    #   raise e, "Exception Creating Asset Using File Path. #{e.message}", e.backtrace
  end

  output_values = { }
  output_values[:result] = result

  return false unless result and result.has_key?(:asset)
  #return publish_error('Error creating asset.') 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'] # if not result[:existing][:asset]['uuid'].nil?
  elsif result_asset.is_a?(Array)

    #Metadata creation failed but asset was created Array(false, "uuid")
    #TODO: HANDLE CASE WHERE METADATA IS NOT CREATED BUT THE ASSET IS
    ms_asset_id = result_asset[1]

  else
    ms_asset_id = result_asset['id']
    ms_asset_id ||= result_asset
  end
  #Setting metadata during asset_create doesn't work so we set it here
  #response = ms.metadata_create(ms_uuid, ms_metadata) unless ms_metadata.nil?

  if ms_asset_id
    output_values[:asset_id] = ms_asset_id
    return output_values
  else
    #return publish_error("Error creating asset.\nResponse: #{response}\nMS UUID: #{ms_uuid}\nMS Creating Missing Path Result: #{result}")
    return false
  end
end

#asset_derivatives_transform_to_hash(derivatives) ⇒ Object



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

Parameters:

  • [String] (Hash)

    a customizable set of options



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

Parameters:

  • asset (String|Hash)
  • destination_file_path (String)
  • overwrite (Boolean) (defaults to: false)

Returns:

  • (Hash)

    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

Parameters:

  • download_file_path (String)
  • destination_file_path (String)
  • overwrite (Boolean) (defaults to: false)


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)
  # download_file_path = URI.encode(download_file_path)

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

Parameters:

  • asset (String|Hash)
  • destination_file_path (String)
  • overwrite (Boolean) (defaults to: false)

Returns:

  • (Hash)

    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

Parameters:

  • asset_uuid (String)
  • args (Hash) (defaults to: { })

    A hash of arguments

Options Hash (args):

  • :asset_id (String)

    The uuid of the asset to edit

  • :metadata (Hash)

    The asset’s metadata.

  • :mirror_metadata (Boolean)

    If set to true then the metadata will mirror :metadata which means that any keys existing on MediaSilo that don’t exists in :metadata will be deleted from MediaSilo.

  • :tags_to_add_to_asset (Array)

    An array of tag names to add to the asset

  • :tags_to_remove_from_asset (Array)

    An array of tag names to remove from the asset

  • :add_quicklink_to_asset (Boolean|Hash|Array)


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

   = args.delete(:metadata) { false }
   = 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 }
  #
  # add_quicklink_to_asset = args.delete(:add_quicklink_to_asset) { false }

  _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 .is_a?(Hash)
    if 
      result = (:asset_id => asset_id, :metadata => )
    else
      result = (:asset_id => asset_id, :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

  #
  # if add_quicklink_to_asset
  #   add_quicklink_to_asset.is_a?(Hash) ? quicklink_create(asset_id, add_quicklink_to_asset) : quicklink_create(asset_id)
  #   _response[:quicklink_create_response] = response.body_parsed
  #   _response[:quicklink_create_success] = success?
  #   unless success?
  #     _response[:error_message] = "Error Adding Quicklink to Asset. #{error_message}"
  #     return _response
  #   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

Parameters:

  • search_field_name (String)
  • search_value (String)
  • project_id (String|nil) (defaults to: nil)
  • folder_id (String|nil) (defaults to: nil)
  • options (Hash) (defaults to: { })

Options Hash (options):

  • :limit_to_project_root (Boolean) — default: true

Returns:

  • (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 = VALID_ASSET_SEARCH_FIELDS[search_field_name.to_s.downcase.to_sym]
  # raise ArgumentError, "The argument for the search_field_name parameter is invalid. '#{search_field_name}' not found in #{VALID_ASSET_SEARCH_FIELDS.values.inspect}" unless _search_field_name

  _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 = { :type => '{"in":"video,image,document,archive,audio"}' }.merge((options[:query] || { })).merge(search_field_query)
  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 = { })
   = options.delete(:include_metadata)
  include_derivatives_hash = options.delete(:include_derivatives_hash) { true }
   = options.delete(:transform_metadata) { true }

  asset_id = args[:asset_id]

  asset = asset_get_by_id({ :asset_id => asset_id }, options)

  if 
    md = ( { :asset_id => asset['id'] }, options )
    md = (md) if 
    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

Parameters:

  • args (Hash) (defaults to: { })
  • options (Hash) (defaults to: { })

Options Hash (options):

  • :include_metadata (Boolean)
  • :transform_metadata (Boolean)

    Will cause metadata to be transformed to a simple Hash



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 = { })
   = options.delete(:include_metadata)
  include_derivatives_hash = options.delete(:include_derivatives_hash) { true }
   = options.delete(:transform_metadata) { true }

  assets = assets_get(args, options)

  assets.map! do |a|

    if 
      md = (:asset_id => a['id'])
      md = (md) if 
      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_derivatives_hash)

  assets
end

#default_case_sensitive_searchObject



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

Parameters:

  • download_file_path (String)

    The source path of the file being downloaded

  • destination_file_path (String)

    The destination path for the file being downloaded

  • overwrite (Boolean) (defaults to: false)

    Determines if the destination file will be overwritten if it is found to exist

Returns:

  • (Hash)
    • :download_file_path [String] The source path of the file being downloaded

    • :overwrite [Boolean] The value of the overwrite parameter when the method was called

    • :file_downloaded [Boolean] Indicates if the file was downloaded, will be false if overwrite was true and the file existed

    • :destination_file_existed [String|Boolean] The value will be ‘unknown’ if overwrite is true because the file exist check will not have been run inside of the method

    • :destination_file_path [String] The destination path for the file being downloaded



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

  # Use delete_if instead of keep_if to be ruby 1.8 compatible
  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

#metadata_transform_to_hash(metadata_in) ⇒ Object

Parameters:

  • metadata_in (Array)


543
544
545
546
547
548
549
550
551
552
553
# File 'lib/ubiquity/mediasilo/api/v3/utilities.rb', line 543

def ()
  case 
    when Array
      # This is what we want
      return Hash[ .map { |m| [m['key'], m['value'] ] } ]
    when Hash
      return 
    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)

Parameters:

  • path (String)

    The path to be checked for existence. Format: project[/filename]

  • path_contains_asset (Boolean) (defaults to: false)

    (false) Indicates that the path contains an asset filename

  • options (Hash) (defaults to: { })

    ({ })

Options Hash (options):

  • :asset_name_field (Boolean) — default: :filename


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

  # Remove any and all instances of '/' from the beginning of the 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

  # Get a count of the number of elements which were found to exist
  existing_path_length = existing_path_ary.length

  # Drop the first n elements of the array which corresponds to the number of elements found to be existing
  missing_path = path_ary.drop(existing_path_length)
  # In the following logic tree the goal is indicate what was searched for and what was found. If we didn't search
  # for the component (folder/asset) then we don't want to set the missing indicator var
  # (folder_missing/asset_missing) for component as a boolean but instead leave it nil.
  missing_path_length = missing_path.length
  if missing_path_length > 0
    # something is missing

    if missing_path_length == check_path_length
      # everything is missing in our path

      project_missing = true
      if path_contains_asset
        # we are missing everything and we were looking for an asset so it must be missing
        asset_missing = true

        if check_path_length > 2
          #if we were looking for more than two things (project, folder, and asset) and we are missing everything then folders are missing also
          searched_folders = true
          folder_missing = true
        else

          #if we are only looking for 2 things then that is only project and asset, folders weren't in the path so we aren't missing them
          searched_folders = false
          folder_missing = false
        end
      else
        if check_path_length > 1
          # If we are looking for more than one thing then it was project and folder and both are missing
          searched_folders = true
          folder_missing = true
        else
          searched_folders = false
          folder_missing = false
        end
      end
    else
      #we have found at least one thing and it starts with project
      project_missing = false
      if path_contains_asset
        #missing at least 1 and the asset is at the end so we know it's missing
        asset_missing = true
        if missing_path_length == 1
          #if we are only missing one thing and it's the asset then it's not a folder!
          folder_missing = false
          searched_folders = check_path_length > 2
        else
          # missing_path_length is more than 1
          if check_path_length > 2
            #we are looking for project, folder, and asset and missing at least 3 things so they are all missing
            searched_folders = true
            folder_missing = true
          else
            #we are only looking for project and asset so no folders are missing
            searched_folders = false
            folder_missing = false
          end
        end
      else
        #if we are missing something and the project was found and there was no asset then it must be a folder
        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

Parameters:

  • path (String)

    The path to create inside of MediaSilo

  • contains_asset (Boolean) (defaults to: false)

    see #path_resolve

  • asset_url (String|nil) (defaults to: nil)
  • metadata (Hash|nil) (defaults to: nil)
  • options (Hash) (defaults to: { })

Options Hash (options):

  • :additional_asset_create_params (Hash)

    Additional arguments to pass to the asset_create call

  • :overwrite_asset (Boolean)

    Will cause the asset to be deleted and recreated

  • :path_creation_delay (Boolean)

    A delay between folder_create calls to allow

Returns:

  • (Hash)

    {

    :check_path_ary=>["create_missing_path_test"],
    :existing=>{
        :name_path=>"/",
        :id_path=>"/",
        :name_path_ary=>[],
        :id_path_ary=>[],
        :project=>false,
        :asset=>nil,
        :folders=>[]
    },
    :missing_path=>[],
    :searched_folders=>false,
    :project_missing=>true,
    :folder_missing=>false,
    :asset_missing=>nil,
    :project=>{
        "id"=>30620,
        "datecreated"=>"June, 05 2013 15:20:15",
        "description"=>"",
        "uuid"=>"15C84A5F-B2D9-0E2F-507D94189F8A1FDC",
        "name"=>"create_missing_path_test"
    },
    :project_id=>30620,
    :parent_folder_id=>0
    

    }



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,  = 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]

  # This was meant as a Bypass if nothing needed to be done, complicated code maintenance
  # unless project_missing or folder_missing or asset_missing or (!asset_missing and overwrite_asset)
  #   project_id = cp_result[:existing][:id_path_ary].first
  #   asset = cp_result[:existing][:asset]
  #   if contains_asset
  #     asset_id = cp_result[:existing][:id_path_ary].last
  #     parent_folder_id = cp_result[:existing][:id_path_ary].fetch(-2)
  #
  #     asset_edit_extended(asset_id, additional_asset_create_params) if metadata and !metadata.empty?
  #     # metadata_create_if_not_exists(asset_id, metadata) if metadata and !metadata.empty?
  #   else
  #     asset_id = nil
  #     parent_folder_id = cp_result[:existing][:id_path_ary].last
  #   end
  #
  #   result = cp_result.merge({ :project_id => project_id, :parent_folder_id => parent_folder_id, :asset_id => asset_id, :asset => asset })
  #   logger.debug { "Create Missing Path Result: #{result.inspect}" }
  #   return result
  # end
  searched_folders = cp_result[:searched_folders]

  missing_path = cp_result[:missing_path]

  project_name = cp_result[:check_path_ary][0]
  #logger.debug "PMP: #{missing_path}"
  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
      # logger.debug "FMP: #{missing_path}"

      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] = 
    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?
    # metadata_create_if_not_exists(asset['id'], metadata) if metadata and !metadata.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? '/' # Remove the leading slash if it is present
  path_ary = path.split('/') # Turn the path into an array of names

  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]

  #The path was not found
  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 # Pull the project_id out of the beginning of the array

  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.

Parameters:

  • project_id (String|Integer)

    The id of the project that you wish to delete.

  • folder_id (String|Integer) (defaults to: 0)

    (0) The parent_folder in the project that you would want to delete the contents of. Defaults to 0 which is the root folder of the project.

  • recursive (Boolean) (defaults to: false)

    (false) Tells the method to recurse into any sub-folders. Usually you would want this to be true, but the default requires you to be explicit about wanting to delete sub-folders so the default is false.

  • include_assets (Boolean) (defaults to: false)

    (false) Tells the method to delete any assets in any directory that it traverses into. Usually you would want this to be true, but the default requires you to be explicit about wanting to delete assets so the default is false

  • delete_contents_only (Boolean) (defaults to: true)

    (true) Tells the method to not delete the parent object (project or folder) unless this is set to true.

  • options (Hash) (defaults to: { })
  • option (Hash)

    a customizable set of options

Raises:

  • (ArgumentError)


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}" }

      # Pass delete_assets_only as the delete_contents_only argument. This way if we aren't deleting assets only then
      # sub-folders and assets will get deleted recursively, otherwise only assets will be deleted
      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

    # batch_execute {
    #   assets.each { |asset| asset_delete(asset['id']) }
    # }

    response = assets.map { |asset| asset_delete(asset['id']) }

    # if dry_run
    #   assets = [ ]
    # else
    #   assets = response.dup.delete_if { |r| %w(204 404).include?(r['httpStatus']) } unless assets.empty?
    # end

    assets = [ ]

    # assets.delete_if { |asset|
    #   asset_counter += 1
    #
    #   dry_run ? true : asset_delete(asset['id'])
    # }
  else
    assets = [ ] # make assets.empty? pass later on. let ms throw an error if the project/folder isn't empty
  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

Raises:

  • (ArgumentError)


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)
    # Remove any leading slashes
    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

  # The first element must be the name of the project
  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)
    # The name of the attribute to search the asset name for (Valid options are :title or :filename)
    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)
        # Just add the whole array to the 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

  # Use delete_if instead of keep_if to be ruby 1.8 compatible
  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

Parameters:

  • search_value (String)
  • field_name (String)

    The field to search. [“approvalstatus”, “archivestatus”, “averagerating”, “datecreated”, “datemodified”, “description”,

    "duration", "external", "filename", "height", "progress", "rating", "secure", "size", "thumbnail_large",
    "thumbnail_small", "title", "totalcomments", "transcriptstatus", "type", "uploaduser", "uuid", "width"]
    
  • assets (Array)

    The assets as returned by asset_adavanced_search_by_results

  • options (Hash)

Options Hash (options):

  • :return_first_match (Boolean) — default: false
  • :match_full_string (Boolean) — default: true
  • :case_sensitive (Boolean) — default: #default_case_sensitive_search


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))
    #logger.debug "COMPARING: '#{search_value}' #{nomatch ? '!' : '='}= '#{asset_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

Parameters:

  • project_id (Integer)

    The id of the project the folder resides in

  • path (String)

    A directory path separated by / of folders to traverse

  • parent_id (Integer) (defaults to: nil)

    The ID of the parent folder to begin the search in



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