Class: Vidispine::API::Utilities

Inherits:
Client
  • Object
show all
Defined in:
lib/vidispine/api/utilities.rb,
lib/vidispine/api/utilities/cli.rb,
lib/vidispine/api/utilities/http_server.rb,
lib/vidispine/api/utilities/http_server/cli.rb

Defined Under Namespace

Classes: CLI, HTTPServer

Constant Summary collapse

DEFAULT_REQUIRE_METADATA_MAP_MATCH =
false

Instance Attribute Summary collapse

Attributes inherited from Client

#api_endpoint_prefix, #api_noauth_endpoint_prefix, #http_client, #logger, #request, #response

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Client

#collection_access_add, #collection_access_delete, #collection_access_get, #collection_create, #collection_delete, #collection_get, #collection_items_get, #collection_metadata_get, #collection_metadata_set, #collection_object_add, #collection_object_remove, #collection_rename, #collections_get, #error, #http, #import_placeholder, #import_placeholder_item, #import_using_uri, #item_access_add, #item_access_delete, #item_access_get, #item_access_list, #item_collections_get, #item_delete, #item_export, #item_field_group_get, #item_field_group_set, #item_get, #item_metadata_get, #item_metadata_set, #item_notifications_delete, #item_shape_files_get, #item_shape_get, #item_shape_import, #item_shape_placholder_create, #item_shapes_get, #item_sidecar_import, #item_thumbnail, #item_transcode, #item_uris_get, #items_get, #items_search, #job_abort, #job_get, #jobs_get, #metadata_field_delete, #metadata_field_get, #metadata_field_group_get, #metadata_field_groups_get, #metadata_field_terse_schema, #metadata_fields_get, #process_request, #process_request_using_class, #search, #search_browse, #storage_delete, #storage_file_copy, #storage_file_create, #storage_file_get, #storage_file_item_get, #storage_file_state_set, #storage_files_get, #storage_get, #storage_method_get, #storage_rescan, #storages_get, #success?, #version

Constructor Details

#initialize(args = { }) ⇒ Utilities

Returns a new instance of Utilities.



14
15
16
17
18
19
# File 'lib/vidispine/api/utilities.rb', line 14

def initialize(args = { })
  @default_storage_map = args[:storage_map] || { }
  @default_metadata_map = args[:metadata_map] || { }
  @default_require_metadata_map_match = args.fetch(:require_metadata_map_match, DEFAULT_REQUIRE_METADATA_MAP_MATCH)
  super
end

Instance Attribute Details

#default_metadata_mapObject

Returns the value of attribute default_metadata_map.



12
13
14
# File 'lib/vidispine/api/utilities.rb', line 12

def 
  @default_metadata_map
end

#default_storage_mapObject

Returns the value of attribute default_storage_map.



12
13
14
# File 'lib/vidispine/api/utilities.rb', line 12

def default_storage_map
  @default_storage_map
end

Class Method Details

.build_metadata_document(metadata_in, map = { }, options = { }) ⇒ Hash

Parameters:

  • metadata_in (Hash)
  • map (Hash) (defaults to: { })

    A hash of alias field names to vidispine field or field and groups. Will merge into @default_metadata_map, options, and options

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

Options Hash (options):

  • :default_metadata_map (Hash)

    Allows for overriding @default_metadata_map

  • :metadata_map (Hash)

    An option for passing the map in options

Returns:

  • (Hash)


322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/vidispine/api/utilities.rb', line 322

def self.(, map = { }, options = { })
  # map = (options[:default_metadata_map]).merge((options[:metadata_map] || { }).merge(map))
  require_map_match = options.fetch(:require_metadata_map_match, DEFAULT_REQUIRE_METADATA_MAP_MATCH)
  groups = { }
  .each do |k,v|
    _map = map[k]
    _map = k if _map.nil? && !require_map_match
    next unless _map
    _map = [ _map ] unless _map.is_a?(Array)
    _map.each do |_map_|
      _map_ = { :field => _map_ } if _map_.is_a?(String)
      #puts "##{_map_[:group].inspect} #{_map_.class.name} #{_map_.inspect}"
      (groups[_map_[:group]] ||= { })[_map_[:field]] = v
    end
  end

  map_options = map[:__build_metadata_document_options] || map['__build_metadata_document_options'] || { }
  options.merge!(map_options)
  parent_group_name = options[:parent_group_name]

  _field = groups.delete(nil) { { } }.map { |fname, values| { :name => fname, :value => [*values].map { |v| { :value => v } } } }
  _group = groups.map { |name, fields| { :name => name, :field => fields.map { |fname, values| { :name => fname, :value => [*values].map { |v| { :value => v } } } } } }

  # metadata_out = { }
  # metadata_out[:field] = _field unless _field.empty?
  # metadata_out[:group] = _group unless _group.empty?
  # metadata_out

  #{ :field => _field, :group => _group }
  if (_field.empty? && _group.length == 1 && (!parent_group_name || parent_group_name.empty?))
    _group = _group.first
    group_name = _group[:name]
    group_fields = _group[:field]
    return { :group => [ group_name ], :timespan => [ { :start => '-INF', :end => '+INF', :field => group_fields } ] }
  end

  _data_out = { :timespan => [ { :start => '-INF', :end => '+INF', :field => _field, :group => _group } ] }
  _data_out[:group] ||= [ parent_group_name ] if parent_group_name

  _data_out
end

.build_metadata_documents(metadata_in, map = { }, options = { }) ⇒ Object



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/vidispine/api/utilities.rb', line 371

def self.(, map = { }, options = { })
  require_map_match = options.fetch(:require_metadata_map_match, DEFAULT_REQUIRE_METADATA_MAP_MATCH)
  groups = { }
  .each do |k,v|
    _map = require_map_match ? map[k] : map.fetch(k, k)
    next unless _map
    _map = [ _map ] unless _map.is_a?(Array)
    _map.each do |_map_|
      _map_ = { :field => _map_ } if _map_.is_a?(String) || _map.is_a?(Symbol)
      #puts "##{_map_[:group].inspect} #{_map_.class.name} #{_map_.inspect}"
      (groups[_map_[:group]] ||= { })[_map_[:field]] = v
    end
  end

  _field = groups.delete(nil) { { } }.map { |fname, values| { :name => fname, :value => [*values].map { |v| { :value => v } } } }
  _groups = groups.map { |name, fields| { :name => name, :field => fields.map { |fname, values| { :name => fname, :value => [*values].map { |v| { :value => v } } } } } }

  docs = [ ]

  if !_field.empty?
    docs << { :timespan => [ { :start => '-INF', :end => '+INF', :field => _field, :group => [ ] } ] }
  end

  _groups.each do |_group|
    group_name = _group[:name]
    group_fields = _group[:field]
    docs << { :group => [ group_name ], :timespan => [ { :start => '-INF', :end => '+INF', :field => group_fields } ] }
  end

  docs
end

.transform_metadata_get_response_to_hash(_response, options = { }) ⇒ Hash

Reads an item’s metadata array and return a hash version of the metadata

{

"item": [
  {
    "metadata": {
      "revision": "VX-348,VX-766,VX-350,VX-352,VX-767,VX-353,VX-816,VX-815,VX-346",
      "group": [
        "Film"
      ],
      "timespan": [
        {
          "start": "-INF",
          "end": "+INF",
          "field": [
            {
              "name": "portal_mf778031",
              "uuid": "4150479f-b15e-475b-bc48-80ef85d3c2cf",
              "change": "VX-767",
              "user": "admin",
              "value": [
                {
                  "uuid": "a7f91e7c-ffc6-4ba1-9658-3458bec886e9",
                  "change": "VX-767",
                  "user": "admin",
                  "value": "556dd36a02a760d6bd000071",
                  "timestamp": "2015-07-06T22:25:17.926+0000"
                }
              ],
              "timestamp": "2015-07-06T22:25:17.926+0000"
            },
            {
              "name": "portal_mf897662"
            },
            {
              "name": "portal_mf396149"
            }
          ],
          "group": [

          ]
        }
      ]
    },
    "id": "VX-84"
  }
]

}

Parameters:

  • _response (Hash)

    A vidispine item or hash with the item key

Returns:

  • (Hash)


242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/vidispine/api/utilities.rb', line 242

def self.(_response, options = { })
  items = _response['item'] || _response
  item = items.is_a?(Array) ? items.first : items
   = item['metadata'] || item
   = { }

  # group = item_metadata['group'].first
  timespans = ['timespan']
  timespans.each do |t|
    .merge!((t))
  end
  
end

.transform_metadata_group(group, breadcrumbs = [ ]) ⇒ Hash

Parameters:

  • group (Hash)
  • breadcrumbs (Array) (defaults to: [ ])

Returns:

  • (Hash)


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
290
291
292
293
294
295
# File 'lib/vidispine/api/utilities.rb', line 263

def self.(group, breadcrumbs = [ ])
   = { }
  name = group['name']

  _breadcrumbs = breadcrumbs.dup << name
  bc = _breadcrumbs.compact.join(':')
  bc << ':' unless bc.empty?

  groups = group['group']
  if groups.length == 1 and (_group_name = groups.first).is_a?(String)
    # group_name = _group_name.is_a?(String) ? _group_name : ''
  else
    # group_name = ''
    groups.each do |g|
      .merge!((g, _breadcrumbs))
    end
  end

  fields = group['field']
  fields.each do |f|
    # field_name = "#{group_name}#{group_name.empty? ? '' : ':'}#{f['name']}"
    field_name = "#{bc}#{f['name']}"
    field_value_raw = f['value']
    if field_value_raw.is_a?(Array)
      field_value = field_value_raw.map { |v| v['value'] }
      field_value = field_value.first if field_value.length == 1
    else
      field_value = field_value_raw
    end
    [field_name] = field_value
  end
  
end

Instance Method Details

#build_item_search_document(criteria, options = { }) ⇒ Hash

Parameters:

  • criteria (Hash)

Returns:

  • (Hash)


299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/vidispine/api/utilities.rb', line 299

def build_item_search_document(criteria, options = { })
  fields = criteria[:fields] || criteria
  item_search_document = {
      :field => fields.map { |fname, values|
        if values.is_a?(Hash)
          #_values = values.map { |k, values| { k => [ { :value => [*values].map { |v| { :value => v } } } ] } }.inject({}) { |hash, value| hash.merge(value) }
          _values = Hash[ values.map { |k, values| [ k, [ { :value => [*values].map { |v| { :value => v } } } ] ] } ]
        else
          _values = { :value => [*values].map { |v| { :value => v } } }
        end
        { :name => fname }.merge(_values)
      }
  }
  item_search_document
end

#build_metadata_document(metadata_in, map = { }, options = { }) ⇒ Object



364
365
366
367
368
369
# File 'lib/vidispine/api/utilities.rb', line 364

def (, map = { }, options = { })
  map = (options[:default_metadata_map] || ).merge((options[:metadata_map] || { }).merge(map))
   = options[:require_metadata_map_match]
  options[:require_metadata_map_match] = @default_require_metadata_map_match if .nil?
  self.class.(, map, options)
end

#build_metadata_documents(metadata_in, map = { }, options = { }) ⇒ Array

Returns:

  • (Array)


405
406
407
408
409
410
# File 'lib/vidispine/api/utilities.rb', line 405

def (, map = { }, options = { })
  map = (options[:default_metadata_map] || ).merge(map.merge(options[:metadata_map] || { }))
   = options[:require_metadata_map_match]
  options[:require_metadata_map_match] = @default_require_metadata_map_match if .nil?
  self.class.(, map, options)
end

#cantemo_metadata_field_group_map(field_group, options = { }) ⇒ Hash

Parameters:

  • field_group (String|Hash)

Returns:

  • (Hash)


414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/vidispine/api/utilities.rb', line 414

def (field_group, options = { })
  field_group = (:group_name => field_group, :include_values => true, :traverse => true) if field_group.is_a?(String)

  field_map = { }
  group_name = field_group['schema']['name']
  fields = field_group['field']
  fields.each do |field|
    cp_field = (field)
    cp_field[:group] = group_name
    cp_field[:vs_def] = field

    field_map[cp_field[:label]] = cp_field
  end

  field_map
end

#cantemo_metadata_field_process(field, options = { }) ⇒ Hash

Parameters:

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

Returns:

  • (Hash)


434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/vidispine/api/utilities.rb', line 434

def (field, options = { })
  field_name = field['name']
  field_data = field['data']
  field_data = JSON.parse(field['data']) if field_data.is_a?(String) && field_data.start_with?('{')

  field_extra_data = field_data.find { |fd| fd['key'] == 'extradata' } || { }
  field_extra_data_value = field_extra_data['value']
  field_extra_data_value = JSON.parse(field_extra_data_value) if field_extra_data_value.is_a?(String) && field_extra_data_value.start_with?('{')
  return nil unless field_extra_data_value.is_a?(Hash)

  field_label = field_extra_data_value['name']

  cp_field_type = field_extra_data_value['type']
  cp_field = {
      :name => field_name,
      :type => cp_field_type,
      :label => field_label,
      :cp_def => field_extra_data_value
  }

  case cp_field_type
    when 'checkbox', 'dropdown'
      _values = field_extra_data_value['values']
      choices = Hash[ _values.map { |v| [ v['key'], v['value'] ] } ]
      cp_field[:choices] = choices
    when 'lookup'
      field_data__values = (field_data.find { |fd| fd['key'] == '__values' } || { })['value']
      if field_data__values
        __values_xml = field_data__values.gsub('\"', '"')
        __values_doc = REXML::Document.new(StringIO.new(__values_xml))

        choices = { }
        __values_doc.elements.each('SimpleMetadataDocument/field') do |e|
          choices[e.elements['key'].text] = e.elements['value'].text
        end
        cp_field[:choices] = choices
      end
  end

  cp_field
end

#collection_create_if_not_exists(args = { }, options = { }) ⇒ Hash

Searches for a collection by name and if a match is not found then a new collection is created This method will only return the first match if an existing collection is found.

Parameters:

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

Options Hash (args):

  • :collection_name (String)

Options Hash (options):

  • :case_sensitive (Boolean) — default: true

Returns:

  • (Hash)

Raises:

  • (ArgumentError)


485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/vidispine/api/utilities.rb', line 485

def collection_create_if_not_exists(args = { }, options = { })
  return args.map { |v| collection_create_if_not_exists(v, options) } if args.is_a?(Array)
  args = args.is_a?(Hash) ? args : { :collection_name => args }

  collection_name = args[:collection_name] || args[:name]
  raise ArgumentError, 'collection_name is required.' unless collection_name
  case_sensitive = options.fetch(:case_sensitive, true)

  collection = collection_get_by_name( :collection_name => collection_name, :case_sensitive => case_sensitive )
  collection_already_existed = collection && !collection.empty? ? true : false
  collection ||= collection_create(collection_name)
  options[:extended_response] ?
      { :collection => collection, :collection_already_existed => collection_already_existed } :
      collection
end

#collection_file_add_using_path(args = { }, options = { }) ⇒ Object

Parameters:

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

Options Hash (args):

  • :file_path (String) — default: Required
  • :metadata_file_path_field_id (String) — default: Required
  • :storage_path_map (Hash) — default: Required
  • :collection_id (String)

    Required if :collection_name or :file_path_collection_name_position is not set

  • :collection_name (String)

    Required if :collection_id or :file_path_collection_name_position is not set

  • :file_path_collection_name_position (Integer)

    Required if :collection_id or :collection_name is not set

  • :placeholder_args (Hash) — default: { :container => 1, :video => 1 }

Raises:

  • (ArgumentError)


1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
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
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
# File 'lib/vidispine/api/utilities.rb', line 1093

def collection_file_add_using_path(args = { }, options = { })
  args = symbolize_keys(args, false)
  _response = { }

  # 1. Receive a File Path
  file_path = args[:file_path]
  raise ArgumentError, ':file_path is a required argument.' unless file_path

   = args[:metadata_file_path_field_id]
  raise ArgumentError, ':metadata_file_path_field_id is a required argument.' unless 

  # 2. Determine Storage ID
  storage_path_map = args[:storage_path_map] || args[:storage_map]
  raise ArgumentError, ':storage_path_map is a required argument.' unless storage_path_map

  # Make sure the keys are strings
  storage_path_map = Hash[storage_path_map.map { |k,v| [k.to_s, v] }] if storage_path_map.is_a?(Hash)

  volume_path, storage = storage_path_map.find { |path, _| file_path.start_with?(path) }
  raise "Unable to find match in storage path map for '#{file_path}'. Storage Map: #{storage_path_map.inspect}" unless volume_path

  file_path_relative_to_storage_path = file_path.sub(volume_path, '')
  logger.debug { "File Path Relative to Storage Path: #{file_path_relative_to_storage_path}" }

  storage = storage_get(:id => storage) if storage.is_a?(String)
  _response[:storage] = storage
  raise 'Error Retrieving Storage Record. Storage Id: #{' unless storage

  storage_id = storage['id']
  storage_uri_raw = storage['method'].first['uri']
  storage_uri = URI.parse(storage_uri_raw)

  vidispine_file_path = File.join(storage_uri.path, file_path_relative_to_storage_path)
  logger.debug { "Vidispine File Path: '#{vidispine_file_path}'" }
  _response[:vidispine_file_path] = vidispine_file_path

  # 3 Get Collection
  collection_id = args[:collection_id]
  unless collection_id
    collection_name = args[:collection_name]
    unless collection_name
      file_path_collection_name_position = args[:file_path_collection_name_position]
      raise ArgumentError, ':collection_id, :collection_name, or :file_path_collection_name_position argument is required.' unless file_path_collection_name_position

      file_path_split = (file_path_relative_to_storage_path.start_with?('/') ? file_path_relative_to_storage_path[1..-1] : file_path_relative_to_storage_path).split('/')
      collection_name = file_path_split[file_path_collection_name_position]
      raise ArgumentError, 'Unable to determine collection name from path.' unless collection_name
      logger.debug { "Using '#{collection_name}' as collection_name. File Path Array: #{file_path_split.inspect}" }
    end
    # Determine Collection
    collection = collection_create_if_not_exists(:collection_name => collection_name)
    collection_id = collection['id']
  else
    collection = collection_get(:collection_id => collection_id)
    raise ArgumentError, 'Collection not found.' unless collection
  end
  _response[:collection] = collection
  #return

  # 4.1 Search for Item using File Path
  search_response = search(:content => 'metadata', :item_search_document => { :field => [ { :name => , :value => [ { :value => vidispine_file_path } ] } ] } ) || { 'entry' => [ ] }
  _response[:search] = search_response

  item = (search_response['entry'].first || { })['item']
  unless item
    storage_file_get_or_create_response = storage_file_get_or_create(storage_id, file_path_relative_to_storage_path, :extended_response => true)
    _response[:storage_file_get_or_create_response] = storage_file_get_or_create_response
    file = storage_file_get_or_create_response[:file]

    item = placeholder = file['item']

    unless item
      # 4.2 Create a Placeholder
      logger.debug { 'Creating Placeholder.' }
      #placeholder_args = args[:placeholder_args] ||= { :container => 1, :video => 1, :metadata_document => { :group => [ 'Film' ], :timespan => [ { :field => [ { :name => metadata_file_path_field_id, :value => [ { :value => vidispine_file_path } ] } ], :start => '-INF', :end => '+INF' } ] } }
      placeholder_args = args[:placeholder_args] ||= { :container => 1, :video => 1, :metadata_document => { :timespan => [ { :field => [ { :name => , :value => [ { :value => vidispine_file_path } ] } ], :start => '-INF', :end => '+INF' } ] } }
      item = placeholder = import_placeholder(placeholder_args)
    end
    _response[:placeholder] = placeholder
  end

  _response[:item] = item
  item_id = item['id']

  # 5. Add Item to the Collection
  logger.debug { 'Adding Item to Collection.' }
  collection_object_add_response = collection_object_add(:collection_id => collection_id, :object_id => item_id)
  _response[:collection_object_add] = collection_object_add_response

  # Item was already in the system so exit here
  return _response unless file

  file_id = file['id']
  raise "File Id Not Found. #{file.inspect}" unless file_id

  # 6. Add the file as the original shape
  logger.debug { 'Adding the file as the Original Shape.' }
  item_shape_import_response = item_shape_import(:item_id => item_id, :file_id => file_id, :tag => 'original')
  _response[:item_shape_import] = item_shape_import_response

  job_id = item_shape_import_response['jobId']
  if job_id
    job_monitor_response = wait_for_job_completion(:job_id => job_id) { |env|
      logger.debug { "Waiting for Item Shape Import Job to Complete. Time Elapsed: #{Time.now - env[:time_started]} seconds" }
    }
    last_response = job_monitor_response[:last_response]
    raise "Error Adding file As Original Shape. Response: #{last_response.inspect}" unless last_response['status'] == 'FINISHED'
    _response[:item_shape_import_job] = job_monitor_response
  end

  # 7. Generate the Transcode of the item
  transcode_tag = args[:transcode_tag] || 'lowres'
  logger.debug { 'Generating Transcode of the Item.' }
  item_transcode_response = item_transcode(:item_id => item_id, :tag => transcode_tag)
  _response[:item_transcode] = item_transcode_response

  # 8. Generate the Thumbnails and Poster Frame
  create_thumbnails = args.fetch(:create_thumbnails, true)
  create_posters = args[:create_posters] || 3
  logger.debug { 'Generating Thumbnails(s) and Poster Frame.' }
  item_thumbnail_response = item_thumbnail(:item_id => item_id, :createThumbnails => create_thumbnails, :createPosters => create_posters)
  _response[:item_thumbnail] = item_thumbnail_response

  _response
end

#collection_get_by_name(args = { }, options = { }) ⇒ Hash|Array|nil

Searches for a collection by name

Parameters:

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

Options Hash (args):

  • :collection_name (String)

Options Hash (options):

  • :return_first_match (Boolean) — default: true
  • :case_sensitive (Boolean) — default: true

Returns:

  • (Hash|Array|nil)


509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/vidispine/api/utilities.rb', line 509

def collection_get_by_name(args = { }, options = { })
  return collection_create_if_not_exists(args, options) if options[:collection_create_if_not_exists]
  args = args.is_a?(Hash) ? args : { :collection_name => args }

  collection_name = args[:collection_name] || args[:name]
  return_first_match = options.fetch(:return_first_match, true)

  unless collection_name
    raise ArgumentError, 'collection_name is required.'
  end

  # collections = ( (collections_get || { })['collection'] || [ ] )

  comparison_method, comparison_value = options.fetch(:case_sensitive, true) ? [ :eql?, true ] : [ :casecmp, 0 ]
  collections_search_method = return_first_match ? :find : :select

  collection = nil
  first = 1
  limit = 1000
  collections = [ ]
  loop do
    r = collections_get(:first => first, :number => limit)
    _collections = r['collection']
    break if _collections.empty?

    if return_first_match
      collection = _collections.send(collections_search_method) { |c| c['name'].send(comparison_method, collection_name) == comparison_value }
      break if collection
    end

    collections.concat _collections
    first += _collections.length
  end
  return_first_match ? collection : collections.send(collections_search_method) { |c| c['name'].send(comparison_method, collection_name) == comparison_value }
end

#collection_item_add_extended(args = { }, options = { }) ⇒ Object

Adds an Item to a Collection but Gives Multiple ways of Determining the Collection

Parameters:

  • :item (Hash)
  • :collection (Hash)
  • :collection_id (String)
  • :collection_name (String)
  • :file_path (String)

    Required if using :file_path_collection_name_position

  • :file_path_collection_name_position (Integer)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/vidispine/api/utilities.rb', line 93

def collection_item_add_extended(args = { }, options = { })
  args = symbolize_keys(args, false)

  _response = { }

  item = args[:item] || { }
  item_id = args[:item_id] || item['id']

  # # 3 Get Collection
  # collection_id = args[:collection_id] || collection['id']
  # unless collection_id
  #   collection_name = args[:collection_name]
  #   unless collection_name
  #     file_path_collection_name_position = args[:file_path_collection_name_position]
  #     raise ArgumentError, ':collection_id, :collection_name, or :file_path_collection_name_position argument is required.' unless file_path_collection_name_position
  #
  #     file_path_split = (file_path.start_with?('/') ? file_path[1..-1] : file_path).split('/')
  #     collection_name = file_path_split[file_path_collection_name_position]
  #     raise ArgumentError, 'Unable to determine collection name from path.' unless collection_name
  #     logger.debug { "Using '#{collection_name}' as collection_name. File Path Array: #{file_path_split.inspect}" }
  #   end
  #   # Determine Collection
  #   collection = collection_create_if_not_exists(:collection_name => collection_name)
  #   collection_id = collection['id']
  # else
  #   collection ||= collection_get(:collection_id => collection_id)
  #   raise ArgumentError, 'Collection not found.' unless collection
  # end
  collection = determine_collection(args)
  _response[:collection] = collection
  collection_id = collection['id']

  # 5. Add Item to the Collection
  logger.debug { 'Adding Item to Collection.' }
  collection_object_add_response = collection_object_add(:collection_id => collection_id, :object_id => item_id)
  _response[:collection_object_add] = collection_object_add_response

  _response
end

#determine_collection(args, options = { }) ⇒ Object

Tries to find a collection using either the collection id, name, or a file path with a collection name position. For use in other methods that need to perform the same type of lookup

Parameters:

  • args (Hash)

    a customizable set of options

Options Hash (args):

  • :collection (Hash)
  • :collection_id (String)
  • :collection_name (String)
  • :collections (Array) — default: nil

    If not nil then the each element will be run through determine_collection and the array returned

  • :file_path (String)

    Used when :file_path_collection_name_position is set

  • :file_path_collection_name_position (Integer)

    Will split the file path and be used as the index for the collection name location



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
# File 'lib/vidispine/api/utilities.rb', line 54

def determine_collection(args, options = { })
  collections = args[:collections]
  return collections.map { |v| determine_collection(v, options) } if collections.is_a?(Array)

  collection = args[:collection] || { }

  # 3 Get Collection
  collection_id = args[:collection_id] || collection[:id] || collection['id']
  unless collection_id
    collection_name = args[:collection_name] || args[:name]
    unless collection_name
      file_path_collection_name_position = args[:file_path_collection_name_position]
      raise ArgumentError, ':collection_id, :collection_name, or :file_path_collection_name_position argument is required.' unless file_path_collection_name_position

      file_path = args[:file_path]
      raise ArgumentError, ':file_path is a required argument when using :file_path_collection_name_position' unless file_path

      file_path_split = (file_path.start_with?('/') ? file_path[1..-1] : file_path).split('/')
      collection_name = file_path_split[file_path_collection_name_position]
      raise ArgumentError, 'Unable to determine collection name from path.' unless collection_name
      logger.debug { "Using '#{collection_name}' as collection_name. File Path Array: #{file_path_split.inspect}" }
    end
    # Determine Collection
    #collection = collection_create_if_not_exists(:collection_name => collection_name)
    collection = collection_get_by_name({ :collection_name => collection_name }, options)
  else
    collection = collection_get(:collection_id => collection_id)
    raise ArgumentError, 'Collection not found.' unless collection and collection['id']
  end
  collection
end

#item_add_using_file_path(args = { }, options = { }) ⇒ Hash

Adds a file using the files path

Parameters:

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

Options Hash (args):

  • :file_path (String)
  • :storage_path_map (Hash|null)
  • :storage_method_type (String) — default: 'file'
  • :metadata (Hash) — default: {}
  • :metadata_map (Hash) — default: {}
  • :file (Hash)
  • :file_id (String)
  • :create_thumbnails (Boolean) — default: true
  • :create_posters (Integer|false) — default: 3
  • :import_args (Hash) — default: {}

Options Hash (options):

  • :add_item_to_collection (Boolean)
  • :wait_for_import_job (Boolean) — default: true
  • :wait_for_transcode_job (Boolean) — default: false
  • :skip_transcode_if_shape_with_tag_exists (Boolean) — default: true
  • :use_placeholder_import (Boolean) — default: true

Returns:

  • (Hash)

Raises:

  • (ArgumentError)


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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
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
# File 'lib/vidispine/api/utilities.rb', line 567

def item_add_using_file_path(args = { }, options = { })
  args = symbolize_keys(args, false)
  _response = { }

  original_shape_tag_name = args[:original_shape_tag_name] || 'original'

  # 1. Receive a File Path
  file_path = args[:file_path]
  raise ArgumentError, ':file_path is a required argument.' unless file_path

  # 2. Determine Storage ID
  storage_method = args[:storage_method] || 'file'
  storage_path_map = args[:storage_path_map]
  storage_path_map = storage_file_path_map_create(:storage_method => storage_method) unless storage_path_map and !storage_path_map.empty?

  storage_id = args[:storage_id]
  if storage_id
    volume_path, storage = storage_path_map.find { |_, id| id == storage_id }
    raise "Unable to find match in storage path map for '#{storage_id}'. Storage Map: #{storage_path_map.inspect}" unless volume_path
  else
    volume_path, storage = storage_path_map.find { |path, _| file_path.start_with?(path) }
    raise "Unable to find match in storage path map for '#{file_path}'. Storage Map: #{storage_path_map.inspect}" unless volume_path
  end


  file_path_relative_to_storage_path = file_path.sub(volume_path, '')
  logger.debug { "File Path Relative to Storage Path: #{file_path_relative_to_storage_path}" }

  storage = storage_get(:id => storage) if storage.is_a?(String)
  _response[:storage] = storage
  storage_id = storage['id']
  raise "Error Retrieving Storage Record. Storage: #{storage.inspect}" unless storage_id

  # The URI Lookup part was commented out as it should be handled by the storage_file_path_map_create
  # The method type of the URI to lookup
  # storage_method_type = args[:storage_method_type] ||= 'file'
  #
  # storage_uri_method = "#{storage_method_type}:"
  # storage_uri_raw = (storage['method'].find { |v| v['uri'].start_with?(storage_uri_method) } || { })['uri'] rescue nil
  # raise "Error Getting URI from Storage Method. Storage: #{storage.inspect}" unless storage_uri_raw
  # storage_uri = URI.parse(storage_uri_raw)
  #
  # vidispine_file_path = File.join(storage_uri.path, file_path_relative_to_storage_path)

  vidispine_file_path = File.join(volume_path, file_path_relative_to_storage_path)
  logger.debug { "Vidispine File Path: '#{vidispine_file_path}'" }
  _response[:vidispine_file_path] = vidispine_file_path

   = args[:metadata] || { }
   = args[:metadata_map] || { }
  field_group = args[:field_group]

  # map metadata assuming 1 value per field
  #_metadata_as_fields = transform_metadata_to_fields(_metadata, _metadata_map, options)
  #metadata_document = build_metadata_document(_metadata, _metadata_map, options)
   = (, , options)
   = .shift || { }

  # Allow the file to be passed in
  file = args[:file]
  if file
    file_id = file['id']
  else
    file_id = args[:file_id]
    file = { 'id' => file_id }
  end

  if file_id && file && !file['item']
    # If the passed file doesn't have an item then requery to verify that the item is absent
    storage_file_get_response = storage_file_get(:storage_id => storage_id,
                                                 :file_id => file_id, :include_item => true)

    unless storage_file_get_response and storage_file_get_response['id']
      raise "Error Getting Storage File. '#{storage_file_get_response.inspect}'"
    end
    _response[:storage_file_get_response] = storage_file_get_response

    file_found = true

    file = storage_file_get_response
  else
      storage_file_get_or_create_response = storage_file_get_or_create(storage_id,
                                                                       file_path_relative_to_storage_path,
                                                                       :extended_response => true)
      _response[:storage_file_get_or_create_response] = storage_file_get_or_create_response
      file = storage_file_get_or_create_response[:file]
      file_found = storage_file_get_or_create_response[:file_already_existed]
  end

  if file
    _response[:item] = item = file['item']
  end

  _response[:file_already_existed] = file_found
  _response[:item_already_existed] = !!item
  return _response if item # We already have an item so nothing to do

  file_id ||= file['id']

  # 4.2 Create a Placeholder
  logger.debug { 'Creating Placeholder.' }
  #placeholder_args = args[:placeholder_args] ||= { :container => 1, :video => 1, :metadata_document => { :group => [ 'Film' ], :timespan => [ { :field => [ { :name => metadata_file_path_field_id, :value => [ { :value => vidispine_file_path } ] } ], :start => '-INF', :end => '+INF' } ] } }
  #placeholder_args = args[:placeholder_args] ||= { :container => 1, :video => 1, :metadata_document => { :timespan => [ { :start => '-INF', :end => '+INF' }.merge(_metadata_as_fields) ] } }
  #placeholder_args = args[:placeholder_args] ||= { :container => 1, :metadata_document => { :timespan => [ { :start => '-INF', :end => '+INF' }.merge(_metadata_as_fields) ] } }
  placeholder_args = args[:placeholder_args] ||= { :container => 1, :metadata_document =>  }
  _response[:item] = item = import_placeholder(placeholder_args)

  item_id = item['id']
  raise "Error Creating Placeholder: #{item.inspect}" unless item_id

  if field_group
    logger.debug { 'Setting Item Field-Group' }
    item_field_group_set(:item_id => item_id, :field_group => field_group)
  end

  # Add any additional metadata (Vidispine will only take one group at a time)
  .each do ||
    logger.debug { 'Setting Item Metadata' }
    (:item_id => item_id, :metadata_document => )
  end

  should_add_to_collection = options.fetch(:add_item_to_collection,
                                           [ :collections, :collection, :collection_name, :collection_id,
                                             :file_path_collection_name_position
                                           ].any? { |v|  args.keys.include?(v) })
  if should_add_to_collection
    logger.debug { 'Determining Collection to Add the Item to.' }
    collections = determine_collection(args, options)
    if collections.is_a?(Array)
      single_collection = false
      _response[:collections] = collections
    else
      single_collection = true
      _response[:collection] = collections
      collections = [ collections ]
    end
    collection_object_add_responses = collections.map do |collection|
      collection_id = collection['id']
      logger.debug { 'Adding Item to Collection.' }
      collection_object_add(:collection_id => collection_id, :object_id => item_id)
    end

    _response[:collection_object_add] = single_collection ?
                                            collection_object_add_responses.first :
                                            collection_object_add_responses
  end

  shape = item['shape']
  unless shape
    import_args_in = args[:import_args]

    import_args = { :item_id => item_id, :file_id => file_id }
    import_args.merge!(symbolize_keys(import_args_in, false)) if import_args_in.is_a?(Hash)

    use_placeholder_import = options.fetch(:use_placeholder_import, true)
    if use_placeholder_import
      import_args[:item_type] ||= 'container'
    else
      import_args[:tag] ||= original_shape_tag_name
    end

    # 6. Add the file as the original shape
    logger.debug { 'Adding the file as the Original Shape.' }
    item_shape_import_response =  use_placeholder_import ?
             import_placeholder_item(import_args) :
             item_shape_import(import_args)
    _response[:item_shape_import] = item_shape_import_response

    job_id = item_shape_import_response['jobId']
    unless job_id
      invalid_input = item_shape_import_response['invalidInput']
      if invalid_input
        explanation = invalid_input['explanation']
        job_id = $1 if explanation.match(/.*\[(.*)\]$/)
      end
    end
    raise "Error Creating Item Shape Import Job. Response: #{item_shape_import_response.inspect}" unless job_id


    # 7. Generate the Transcode of the item
    transcode_tag = args.fetch(:transcode_tag, false)
    should_transcode = transcode_tag and !transcode_tag.empty? and transcode_tag.to_s.downcase != 'false'

    should_wait_for_import_job_completion = should_transcode || options.fetch(:wait_for_import_job, true)
    if should_wait_for_import_job_completion
      job_monitor_response = wait_for_job_completion(:job_id => job_id) { |env|
        logger.debug { "Waiting for Item Shape Import Job to Complete. Time Elapsed: #{Time.now - env[:time_started]} seconds" }
      }
      last_response = job_monitor_response[:last_response]
      _response[:item_shape_import_job_result] = last_response
      raise "Error Importing File. Response: #{last_response.inspect}" unless %(FINISHED_WARNING FINISHED).include?(last_response['status'])
    end

    if should_transcode
      wait_for_transcode_job = options[:wait_for_transcode_job]
      skip_transcode_if_shape_with_tag_exists = options.fetch(:skip_transcode_if_shape_with_tag_exists, true)
      [*transcode_tag].each do |_transcode_tag|
        transcode_response = item_transcode_shape({
                                                       :item_id => item_id,
                                                       :transcode_tag => _transcode_tag
                                                     },
                                                     {
                                                       :wait_for_transcode_job => wait_for_transcode_job,
                                                       :skip_if_shape_with_tag_exists => skip_transcode_if_shape_with_tag_exists
                                                     })
        (_response[:transcode] ||= { })[transcode_tag] = transcode_response

        # each transcode_tag
      end

      # if transcode_tag
    end

    # 8. Generate the Thumbnails and Poster Frame
    # create_thumbnails = args.fetch(:create_thumbnails, true)
    # create_posters = args.fetch(:create_posters, 3)
    create_thumbnails = args.fetch(:create_thumbnails, false)
    create_posters = args.fetch(:create_posters, false)

    if (create_thumbnails or create_posters)
      logger.debug { 'Generating Thumbnails(s) and Poster Frame.' }
      args_out = { :item_id => item_id }
      args_out[:create_thumbnails] = create_thumbnails if create_thumbnails
      args_out[:create_posters] = create_posters if create_posters
      item_thumbnail_response = item_thumbnail(args_out)
      _response[:item_thumbnail] = item_thumbnail_response
    end
  end

  _response
end

#item_add_using_file_path_metadata(args = { }, options = { }) ⇒ Object

Add an item to the system using file path metadata field as the key

  1. Search for pre existing asset

  2. Create a placeholder with metadata (if asset doesn’t exist)

  3. Create an original shape.

  4. Poll the Job status of the shape creation

  5. Trigger the Transcode of the Proxy, thumbnails

  6. Trigger the Transcode of the thumbnail.

  7. Trigger the Transcode of the poster frame

This was an early experiment

Raises:

  • (ArgumentError)


808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
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
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
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
# File 'lib/vidispine/api/utilities.rb', line 808

def (args = { }, options = { })
  args = symbolize_keys(args, false)
  _response = { }

  # 1. Receive a File Path
  file_path = args[:file_path]
  raise ArgumentError, ':file_path is a required argument.' unless file_path

   = args[:metadata_file_path_field_id]
  raise ArgumentError, ':metadata_file_path_field_id is a required argument.' unless 

  # 2. Determine Storage ID
  storage_path_map = args[:storage_path_map]
  raise ArgumentError, ':storage_path_map is a required argument.' unless storage_path_map

  # Make sure the keys are strings
  storage_path_map = Hash[storage_path_map.map { |k,v| [k.to_s, v] }] if storage_path_map.is_a?(Hash)

  volume_path, storage = storage_path_map.find { |path, _| file_path.start_with?(path) }
  raise "Unable to find match in storage path map for '#{file_path}'. Storage Map: #{storage_path_map.inspect}" unless volume_path

  file_path_relative_to_storage_path = file_path.sub(volume_path, '')
  logger.debug { "File Path Relative to Storage Path: #{file_path_relative_to_storage_path}" }

  storage = storage_get(:id => storage) if storage.is_a?(String)
  _response[:storage] = storage
  raise "Error Retrieving Storage Record. Storage Id: #{storage.inspect}" unless storage

  storage_id = storage['id']
  storage_uri_raw = storage['method'].first['uri']
  storage_uri = URI.parse(storage_uri_raw)

  vidispine_file_path = File.join(storage_uri.path, file_path_relative_to_storage_path)
  logger.debug { "Vidispine File Path: '#{vidispine_file_path}'" }
  _response[:vidispine_file_path] = vidispine_file_path

   = args[:metadata] || { }
  [] ||= vidispine_file_path

   = args[:metadata_map] || { }

  # map metadata assuming 1 value per field
   = (, , options)

  # 4.1 Search for Item using File Path
  search_response = search(:content => 'metadata', :item_search_document => { :field => [ { :name => , :value => [ { :value => vidispine_file_path } ] } ] } ) || { 'entry' => [ ] }
  _response[:search] = search_response

  item = (search_response['entry'].first || { })['item']
  unless item
    # If the item wasn't found then get the file id for the file
    # 4.1 Search for the storage file record
    storage_file_get_response = storage_files_get(:storage_id => storage_id, :path => file_path_relative_to_storage_path) || { 'file' => [ ] }
    raise "Error Getting Storage File. '#{response.inspect}'" unless storage_file_get_response and storage_file_get_response['id']
    file = storage_file_get_response['file'].first
    _response[:storage_file_get_response] = storage_file_get_response

    unless file
      # 4.1.1 Create the storage file record if it does not exist
      file = storage_file_create_response = storage_file_create(:storage_id => storage_id, :path => file_path_relative_to_storage_path, :state => 'CLOSED')
      raise "Error Creating File on Storage. Response: #{response}" unless file
      _response[:storage_file_create_response] = storage_file_create_response
    end

    # 4.2 Create a Placeholder
    logger.debug { 'Creating Placeholder.' }
    #placeholder_args = args[:placeholder_args] ||= { :container => 1, :video => 1, :metadata_document => { :group => [ 'Film' ], :timespan => [ { :field => [ { :name => metadata_file_path_field_id, :value => [ { :value => vidispine_file_path } ] } ], :start => '-INF', :end => '+INF' } ] } }
    placeholder_args = args[:placeholder_args] ||= { :container => 1, :video => 1, :metadata_document => { :timespan => [ { :start => '-INF', :end => '+INF' }.merge() ] } }
    item = placeholder = import_placeholder(placeholder_args)
    _response[:placeholder] = placeholder
  end
  _response[:item] = item
  item_id = item['id']

  if options[:add_item_to_collection]
    logger.debug { 'Determining Collection to Add the Item to.' }
    collection = determine_collection(args, options)
    _response[:collection] = collection
    collection_id = collection['id']

    logger.debug { 'Adding Item to Collection.' }
    collection_object_add_response = collection_object_add(:collection_id => collection_id, :object_id => item_id)
    _response[:collection_object_add] = collection_object_add_response
  end

  # Item was already in the system so exit here
  return _response unless file

  file_id = file['id']
  raise "File Id Not Found. #{file.inspect}" unless file_id

  # 6. Add the file as the original shape
  logger.debug { 'Adding the file as the Original Shape.' }
  item_shape_import_response = item_shape_import(:item_id => item_id, :file_id => file_id, :tag => 'original')
  _response[:item_shape_import] = item_shape_import_response

  job_id = item_shape_import_response['jobId']
  job_monitor_response = wait_for_job_completion(:job_id => job_id) { |env|
    logger.debug { "Waiting for Item Shape Import Job to Complete. Time Elapsed: #{Time.now - env[:time_started]} seconds" }
  }
  last_response = job_monitor_response[:last_response]
  raise "Error Adding file As Original Shape. Response: #{last_response.inspect}" unless last_response['status'] == 'FINISHED'

  # 7. Generate the Transcode of the item
  transcode_tag = args[:transcode_tag] || 'lowres'
  logger.debug { 'Generating Transcode of the Item.' }
  item_transcode_response = item_transcode(:item_id => item_id, :tag => transcode_tag)
  _response[:item_transcode] = item_transcode_response

  # 8. Generate the Thumbnails and Poster Frame
  create_thumbnails = args.fetch(:create_thumbnails, true)
  create_posters = args[:create_posters] || 3
  logger.debug { 'Generating Thumbnails(s) and Poster Frame.' }
  item_thumbnail_response = item_thumbnail(:item_id => item_id, :createThumbnails => create_thumbnails, :createPosters => create_posters)
  _response[:item_thumbnail] = item_thumbnail_response

  _response
end

#item_annotation_create(args = { }, options = { }) ⇒ Object

Note:

THIS IS A CANTEMO SPECIFIC CALL



1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
# File 'lib/vidispine/api/utilities.rb', line 1311

def item_annotation_create(args = { }, options = { })
  # _args = args
  # item_id = _args[:item_id]
  #
  # in_point = _args[:in_point]
  # out_point = _args[:out_point]
  # title = _args[:title]
  #
  # body = { }
  # body[:title] = title if title
  # body[:inpoint] = in_point if in_point
  # body[:outpoint] = out_point if out_point
  #
  # http(:post, "v1/item/#{item_id}/annotation", :body => body)

  _request = Requests::BaseRequest.new(
    args,
    {
      :http_path => 'v1/item/#{arguments[:item_id]}/annotation',
      :http_method => :post,
      :default_parameter_send_in_value => :body,
      :parameters => [
        { :name => :item_id, :aliases => [ :id ], :required => true, :send_in => :path },

        :inpoint,
        :outpoint,
        { :name => :title, :default => '' },
      ]
    }.merge(options)
  )
  process_request(_request)
end

#item_annotation_get(args = { }, options = { }) ⇒ Object

Note:

THIS IS A CANTEMO SPECIFIC CALL



1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
# File 'lib/vidispine/api/utilities.rb', line 1345

def item_annotation_get(args = { }, options = { })
  args = { :item_id => args } if args.is_a?(String)
  # item_id = args[:item_id]
  # http(:get, "v1/item/#{item_id}/annotation")

  _request = Requests::BaseRequest.new(
    args,
    {
      :http_path => 'v1/item/#{arguments[:item_id]}/annotation',
      :parameters => [
        { :name => :item_id, :aliases => [ :id ], :required => true, :send_in => :path }
      ]
    }.merge(options)
  )
  process_request(_request)
end

#item_create_with_proxy_using_file_uri(args = { }, options = { }) ⇒ Object

Deprecated.

SEQUENCE THAT CREATE AN ITEM AND THE PROXY USING THE FILE URI/PATH



1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
# File 'lib/vidispine/api/utilities.rb', line 1285

def item_create_with_proxy_using_file_uri(args = { }, options = { })
  original_file_uri = args[:original_file_uri] || args[:original]
  file_type = args[:file_type] || 'video'

  import_tag = args[:import_tag] || 'lowres'

  placeholder_args = args[:placeholder_args] ||= { :container => 1, :video => 1 }

  storage_id = args[:storage_id]

  # /API/import/placeholder/?container=1&video=1
  placeholder = import_placeholder(placeholder_args)
  placeholder_item_id = placeholder['id']

  # /API/import/placeholder/VX-99/video/?uri=file%3A%2F%2F%2Fsrv%2Fmedia1%2Ftest_orginal2.mp4&tag=lowres
  item = import_placeholder_item(:item_id => placeholder_item_id, :type => file_type, :uri => original_file_uri, :tag => import_tag)
  item_id = item['file'].first['id']

  # /API/item/VX-99/shape?tag=lowres&fileId=VX-136
  #item_shape_import(:item_id => item_id, :tag => nil, :file_id => nil)

  # /API/storage/VX-2/file/?path=storages/test/test_orginal2.mp4
  #storage_file_get(:storage_id => storage_id)
end

#item_create_with_proxy_using_storage_file_paths(args = { }, options = { }) ⇒ Hash

Deprecated.

SEQUENCE THAT CREATES AN ITEM AND THE PROXY USING THE FILE ID

Parameters:

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

Options Hash (args):

  • :original_file_path (String)
  • :lowres_file_path (String)
  • :storage_id (String)
  • :placeholder_args (Hash) — default: { :container => 1, :video => 1 }
  • :create_posters (Boolean) — default: False
  • :create_thumbnails (Boolean) — default: True

Returns:

  • (Hash)

    :item_id, :original_file_id, :lowres_file_id



1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
# File 'lib/vidispine/api/utilities.rb', line 1231

def item_create_with_proxy_using_storage_file_paths(args = { }, options = { })

  original_file_path = args[:original_file_path] || args[:original]
  lowres_file_path = args[:lowres_file_path] || args[:lowres]

  placeholder_args = args[:placeholder_args] ||= { :container => 1, :video => 1 }

  storage_id = args[:storage_id]

  create_posters = args[:create_posters] #|| '300@NTSC'
  create_thumbnails = args.fetch(:create_thumbnails, true)

  # Create a placeholder
  # /API/import/placeholder/?container=1&video=1
  logger.debug { "Creating Placeholder: #{placeholder_args.inspect}" }
  place_holder = import_placeholder(placeholder_args)
  item_id = place_holder['id']

  raise 'Placeholder Create Failed.' unless success?

  # /API/storage/VX-2/file/?path=storages/test/test_orginal2.mp4
  _original_file = original_file = storage_file_get(:storage_id => storage_id, :path => original_file_path)
  raise "Unexpected Response Format. Expecting Hash instead of #{original_file.class.name} #{original_file}" unless _original_file.is_a?(Hash)

  original_file = original_file['file']
  begin
    original_file = original_file.first
  rescue => e
    raise "Error Getting File from Response. #{$!}\n#{original_file.inspect}\n#{_original_file.inspect}"
  end
  raise "File Not Found. '#{original_file_path}' in storage '#{storage_id}'" unless original_file
  original_file_id = original_file['id']

  # /API/item/VX-98/shape?tag=original&fileId=[FileIDofOriginal]
  item_shape_import(:item_id => item_id, :tag => 'original', :file_id => original_file_id)

  # /API/storage/VX-2/file/?path=storages/test/test_proxy2.mp4
  lowres_file = storage_file_get(:storage_id => storage_id, :path => lowres_file_path)
  lowres_file_id = lowres_file['file'].first['id']

  # /API/item/VX-98/shape?tag=lowres&fileId=[FileIDofProxy]
  item_shape_import(:item_id => item_id, :tag => 'lowres', :file_id => lowres_file_id)

  # /API/item/VX-98/thumbnail/?createThumbnails=true&createPoster
  item_thumbnail_args = { :item_id => item_id }
  item_thumbnail_args[:createThumbnails] = create_thumbnails
  item_thumbnail_args[:createPosters] = create_posters if create_posters
  item_thumbnail(item_thumbnail_args)

  { :item_id => item_id, :original_file_id => original_file_id, :lowres_file_id => lowres_file_id }
end

#item_export_extended(args = { }, options = { }) ⇒ Object



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
# File 'lib/vidispine/api/utilities.rb', line 927

def item_export_extended(args = { }, options = { })
  logger.debug { "#{__method__} Args: #{args.inspect} Opts: #{options.inspect}" }
  _options = options.dup
  wait_for_job_completion = _options.delete(:wait_for_job_completion) { }
  item_export_response = item_export(args, _options)

  if wait_for_job_completion
    job_id = item_export_response['jobId']
    job_monitor_callback = options[:job_monitor_callback_function]
    job_monitor_response = wait_for_job_completion(:job_id => job_id) do |env|
      logger.debug { "Waiting for '#{args.inspect}' Export Job to Complete. Time Elapsed: #{Time.now - env[:time_started]} seconds" }
      job_monitor_callback.call(env) if job_monitor_callback
    end

    last_response = job_monitor_response[:last_response]
    # if last_response['status'] == 'FINISHED'
    #   data = last_response['data']
    #   data = Hash[ data.map { |d| [ d['key'], d['value'] ] } ]
    # end

    # if wait_for_transcode_job
  end

  last_response || item_export_response
end

#item_shape_add_using_file_path(args = { }, options = { }) ⇒ Object Also known as: item_add_shape_using_file_path



954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
# File 'lib/vidispine/api/utilities.rb', line 954

def item_shape_add_using_file_path(args = { }, options = { })
  logger.debug { "#{__method__}:#{args.inspect}" }
  _response = { }

  storage_path_map = args[:storage_path_map]

  item = args[:item] || { }
  item_id = args[:item_id] || item['id']

  tag = args[:tag]

  file = args[:file] || { }
  file_id = args[:file_id] || file['id']

  unless file_id

    storage = args[:storage] || { }
    storage_id = args[:storage_id] || storage['id']

    file_path = args[:file_path]
    file_path_relative_to_storage_path = args[:relative_file_path]

    unless file_path_relative_to_storage_path
      if storage_id
        # Process file path using storage information
      else
        process_file_path_response = process_file_path_using_storage_map(file_path, storage_path_map)
      end

      file_path_relative_to_storage_path = process_file_path_response[:relative_file_path]
      storage_id ||= process_file_path_response[:storage_id]
    end

    file = storage_file_get_or_create(storage_id, file_path_relative_to_storage_path)

    file_id = file['id']
    raise "File Error: #{file} Response: #{_response}" unless file_id
  end

  item_shape_import_args = { :item_id => item_id, :file_id => file_id, :tag => tag }
  item_shape_import(item_shape_import_args)
end

#item_shapes_get_extended(args = { }, options = { }) ⇒ Object

Parameters:

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


1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
# File 'lib/vidispine/api/utilities.rb', line 1001

def item_shapes_get_extended(args = { }, options = { })
  item_id = args[:item_id]
  tag = args[:tag]
  return_as_hash = options.fetch(:return_as_hash, false)

  if return_as_hash
    key_by_field = options[:hash_key] || 'id'
  end

  shapes_response = item_shapes_get(:item_id => item_id, :tag => tag)

  shape_ids = shapes_response['uri'] || [ ]
  shapes = [ ]
  shape_ids.each do |shape_id|
    shape = item_shape_get(:item_id => item_id, :shape_id => shape_id)
    shape.dup.each do |k, v|
      shape[k] = v.first if v.is_a?(Array) and v.length == 1 and !['metadata'].include?(k)
    end
    shapes << shape
  end

  shapes_formatted = return_as_hash ? Hash[ shapes.map { |v| [ v[key_by_field], v ] } ] : shapes

  shapes_response['shapes'] = shapes_formatted
  shapes_response
end

#item_transcode_shape(args = { }, options = { }) ⇒ Object

Parameters:

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

Options Hash (options):

  • :skip_if_shape_with_tag_exists (Boolean)


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
# File 'lib/vidispine/api/utilities.rb', line 1031

def item_transcode_shape(args = { }, options = { })
  _response = { }
  item_id = args[:item_id]
  transcode_tag = args[:tag] || args[:transcode_tag] || 'lowres'
  skip_if_tag_exists = options.fetch(:skip_if_shape_with_tag_exists, false)

  if skip_if_tag_exists
    item_shapes_response = item_shapes_get(:item_id => item_id, :tag => transcode_tag)
    shape_ids = item_shapes_response['uri'] || [ ]
    proxy_shape_id = shape_ids.last
    _response[:tag_existed_on_shape] = !!proxy_shape_id
  end

  unless proxy_shape_id
    logger.debug { "Generating Transcode of the Item. Tag: '#{transcode_tag}'" }
    item_transcode_response = item_transcode(:item_id => item_id, :tag => transcode_tag)
    _response[:item_transcode] = item_transcode_response

    job_id = item_transcode_response['jobId']
    _response[:job_id] = job_id
    if options[:wait_for_transcode_job]
      job_monitor_callback = options[:job_monitor_callback_function]
      job_monitor_response = wait_for_job_completion(:job_id => job_id) do |env|
        logger.debug { "Waiting for '#{transcode_tag}' Transcode Job to Complete. Time Elapsed: #{Time.now - env[:time_started]} seconds" }
        job_monitor_callback.call(env) if job_monitor_callback
      end

      last_response = job_monitor_response[:last_response]
      if last_response['status'] == 'FINISHED'
        data = last_response['data']
        data = Hash[ data.map { |d| [ d['key'], d['value'] ] } ]

        proxy_shape_ids = data['shapeIds']
        proxy_shape_id = proxy_shape_ids
      end

      # if wait_for_transcode_job
    end

  end

  if proxy_shape_id
    item_shape_files = item_shape_files_get(:item_id => item_id, :shape_id => proxy_shape_id)
    proxy_file = (((item_shape_files || { })['file'] || [ ]).first || { })
    proxy_file_uri = (proxy_file['uri'] || [ ]).first
    _response[:file] = proxy_file
    _response[:shape_id] = proxy_shape_id
    _response[:file_uri] = proxy_file_uri
    _response[:file_path] = URI.decode(URI(proxy_file_uri).path)
  end

  _response
end

#items_search_extended(args = { }, options = { }) ⇒ Object Also known as: item_search_extended



1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
# File 'lib/vidispine/api/utilities.rb', line 1362

def items_search_extended(args = { }, options = { })
  _args = symbolize_keys(args, false)
  _data = Requests::BaseRequest.process_parameters([ { :name => :fields }, { :name => :item_search_document } ], _args)
  _args = _args.merge(_data[:arguments_out])

  fields = _args.delete(:fields)
  _args[:item_search_document] ||= build_item_search_document(:fields => fields)

  items_search(_args, options)
end

#process_file_path_using_storage_map(file_path, storage_path_map = nil) ⇒ Hash

Will process a file path through a storage path map

Parameters:

  • file_path (String)
  • storage_path_map (Hash) (defaults to: nil)

    (#storage_file_path_map_create)

Returns:

  • (Hash)

    :relative_file_path, :storage_id, :storage_path_map, :volume_path



1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
# File 'lib/vidispine/api/utilities.rb', line 1378

def process_file_path_using_storage_map(file_path, storage_path_map = nil)
  logger.debug { "Method: #{__method__} Args: #{{:file_path => file_path, :storage_path_map => storage_path_map}.inspect}"}

  storage_path_map = storage_file_path_map_create unless storage_path_map and !storage_path_map.empty?

  volume_path, storage_id = storage_path_map.find { |path, _| file_path.start_with?(path) }
  file_path_relative_to_storage_path = file_path.sub(volume_path, '')

  _response = { :relative_file_path => file_path_relative_to_storage_path,
                :storage_id => storage_id,
                :storage_path_map => storage_path_map,
                :volume_path => volume_path }

  logger.debug { "Method: #{__method__} Response: #{_response.inspect}" }
  _response
end

#storage_file_copy_extended(args = { }, options = { }) ⇒ Object



1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
# File 'lib/vidispine/api/utilities.rb', line 1396

def storage_file_copy_extended(args = { }, options = { })
  _args = symbolize_keys(args, false)
  _data = Requests::BaseRequest.process_parameters([ { :name => :use_source_filename }, { :name => :file_id }, { :name => :filename }, { :name => :source_storage_id } ], _args)
  _args = _args.merge(_data[:arguments_out])

  # Get the source file name and set it as the destination file name
  if options[:use_source_filename]
    file_id = _args[:file_id]
    source_storage_id = _args[:source_storage_id]
    file = storage_file_get(:storage_id => source_storage_id, :file_id => file_id)
    args[:filename] = file[:filename]
  end

  storage_file_copy(args, options)
end

#storage_file_create_extended(args = { }, options = { }) ⇒ Object



1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
# File 'lib/vidispine/api/utilities.rb', line 1412

def storage_file_create_extended(args = { }, options = { })
  _args = symbolize_keys(args, false)
  _params = Requests::StorageFileCreate::PARAMETERS.concat [ { :name => :directory, :aliases => [ :dir ], :send_in => :none }, { :name => :storage_map, :send_in => :none } ]
  _data = Requests::BaseRequest.process_parameters(_params, _args)
  _args = _args.merge(_data[:arguments_out])

  storage_path_map = _args.delete(:storage_map) { }
  if storage_path_map.empty?
    storage_path_map = storage_file_path_map_create
  else
    storage_path_map = Hash[storage_path_map.map { |k,v| [k.to_s, v] }] if storage_path_map.is_a?(Hash)
  end


  dir = _args.delete(:directory) { }
  if dir
    # raise ArgumentError, ':storage_map is a required argument.' unless storage_path_map

    volume_path, storage = storage_path_map.find { |path, _| dir.start_with?(path) }
    raise "Unable to find match in storage path map for '#{dir}'. Storage Map: #{storage_path_map.inspect}" unless volume_path

    dir_path_relative_to_storage = dir.sub(volume_path, '')

    storage = storage_get(:id => storage) if storage.is_a?(String)
    raise 'Error Retrieving Storage Record' unless storage

    storage_id = storage['id']
    _args[:storage_id] = storage_id
    storage_uri_raw = storage['method'].first['uri']
    storage_uri = URI.parse(storage_uri_raw)

    vidispine_dir_path = File.join(storage_uri.path, dir_path_relative_to_storage)
    logger.debug { "Vidispine Dir Path: '#{vidispine_dir_path}'" }


    glob_path = dir.end_with?('*') ? vidispine_dir_path : File.join(vidispine_dir_path, '*')
    paths = Dir.glob(glob_path)


    return paths.map do |local_absolute_path|
      logger.debug { "Found Path: '#{local_absolute_path}'" }
      _path = local_absolute_path
      file_path_relative_to_storage_path = _path.sub(volume_path, '')
      logger.debug { "File Path Relative to Storage Path: #{file_path_relative_to_storage_path}" }

      _args[:path] = file_path_relative_to_storage_path
      storage_file_create(_args, options)
    end
  end

  storage_file_create(_args, options)
end

#storage_file_get_or_create(storage_id, file_path_relative_to_storage_path, options = { }) ⇒ Hash

Will search for a relative file path on a storage and if not found will trigger a storage_file_create

Parameters:

  • storage_id (String)
  • file_path_relative_to_storage_path (String)

Returns:

  • (Hash)


1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
# File 'lib/vidispine/api/utilities.rb', line 1549

def storage_file_get_or_create(storage_id, file_path_relative_to_storage_path, options = { })
  logger.debug { "Method: #{__method__} Args:#{{:storage_id => storage_id, :file_path_relative_to_storage_path => file_path_relative_to_storage_path, :options => options }.inspect}" }
  include_item = options.fetch(:include_item, true)
  creation_state = options[:creation_state] || 'CLOSED'
  storage_file_get_response = storage_files_get(:storage_id => storage_id, :path => file_path_relative_to_storage_path, :include_item => include_item) || { 'file' => [ ] }
  file = ((storage_file_get_response || { })['file'] || [ ]).first
  if file
    file_already_existed = true
  else
    file_already_existed = false
    # 4.1.1 Create the storage file record if it does not exist
    storage_file_create_response = file = storage_file_create(:storage_id => storage_id, :path => file_path_relative_to_storage_path, :state => creation_state)
    if (file || { })['fileAlreadyExists']
      file_already_existed = true
      _message = file['fileAlreadyExists']
      logger.warn { "Running Re-creation of Existing File Workaround: #{_message}" }
      storage_file_get_response = file = storage_file_get(:storage_id => storage_id, :file_id => _message['fileId'], :include_item => include_item)
    end
    raise "Error Creating File on Storage. Response: #{response.inspect}" unless (file || { })['id']
  end

  logger.debug { "Method: #{__method__} Response: #{file.inspect}" }

  if options[:extended_response]
    return {
      :file => file,
      :file_already_existed => file_already_existed,
      :storage_file_get_response => storage_file_get_response,
      :storage_file_create_response => storage_file_create_response
    }
  end
  file
end

#storage_file_get_using_file_path(args = { }) ⇒ Hash|nil

Parameters:

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

Options Hash (args):

  • :file_path (String)
  • :storage_path_map (Hash)
  • :create_if_not_exists (Boolean) — default: True
  • :include_item (Boolean) — default: True

Returns:

  • (Hash|nil)


1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
# File 'lib/vidispine/api/utilities.rb', line 1471

def storage_file_get_using_file_path(args = { })
  _response = { }
  file_path = args[:file_path]
  return_extended_response = args.fetch(:extended_response, true)
  volume_path = args[:volume_path]

  # The method type of the URI to lookup
  storage_method_type = (args[:storage_method_type] ||= 'file').to_s.downcase
  logger.debug { "Storage Method Type: '#{storage_method_type}'" }

  storage_path_map = args[:storage_path_map]
  storage_path_map = storage_file_path_map_create(:storage_method => storage_method_type) unless storage_path_map && !storage_path_map.empty?
  logger.debug { "Storage Path Map: #{storage_path_map.inspect}" }

  sm_volume_path, storage = storage_path_map.find { |path, _| file_path.start_with?(path) }
  raise "Unable to find match in storage path map for '#{file_path}'. Storage Map: #{storage_path_map.inspect}" unless sm_volume_path

  storage = storage_get(:id => storage) if storage.is_a?(String)
  _response[:storage] = storage
  storage_id = storage['id']
  raise "Error Retrieving Storage Record. Storage: #{storage}" unless storage_id
  logger.debug { "Storage ID: #{storage_id}" }

  ## Storage Determined Now Start Digging for the File

  case storage_method_type
    when 'file', 'http', 's3'
      storage_uri_method = "#{storage_method_type}:"
      storage_uri_raw = (storage['method'].find do |v|
        ((v['lastSuccess'] || '') >= (v['lastFailure'] || '')) && (storage_method_type == 'any' || v['uri'].start_with?(storage_uri_method))
      end || { })['uri'] rescue nil
      raise "Error Getting URI from Storage Method. Storage: #{storage.inspect}" unless storage_uri_raw

      if storage_method_type == 's3'
        # URI was returning URI::InvalidURIError: the scheme s3 does not accept registry part
        volume_path = storage_uri_raw.split('@').last.sub('s3://', '').split('?').first
      else
        storage_uri = URI.parse(storage_uri_raw)
        volume_path = storage_uri.path
      end

    when 'vxa'
      vxa_local_path = storage['metadata']['field'].find { |m| m['key'] == 'vxaLocalPath' }['value']
      volume_path = vxa_local_path

    else
      volume_path = sm_volume_path

  end unless volume_path
  logger.debug { "Volume Path: '#{volume_path}'" }

  file_path_relative_to_storage_path = file_path.sub(volume_path, '')
  logger.debug { "File Path Relative to Storage Path: '#{file_path_relative_to_storage_path}'" }

  vidispine_file_path = File.join(volume_path, file_path_relative_to_storage_path)
  logger.debug { "Vidispine File Path: '#{vidispine_file_path}'" }

  create_if_not_exists = args.fetch(:create_if_not_exists, true)
  include_item = args.fetch(:include_item, true)
  options_out = { :include_item => include_item }
  if create_if_not_exists
    storage_file_get_or_create_response = storage_file_get_or_create(storage_id, file_path_relative_to_storage_path, options_out.merge(:extended_response => true))
    _response[:storage_file_get_or_create_response] = storage_file_get_or_create_response
    file = storage_file_get_or_create_response[:file]
  else
    storage_file_get_response = storage_files_get({ :storage_id => storage_id, :path => file_path_relative_to_storage_path }.merge(options_out)) || { 'file' => [ ] }
    file = ((storage_file_get_response || { })['file'] || [ ]).first
  end

  _response[:file] = file

  return_extended_response ? _response : file
end

#storage_file_path_map_create(args = {}) ⇒ Hash

Generates a storage file path map from the current storages. This is meant as a default, on most methods you can provide your own storage map that can be used to resolve local paths to storage paths.

Parameters:

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

Options Hash (args):

  • :storage_method (String) — default: 'file'
  • :storages_response (Hash) — default: #storages_get
  • :storages (Array)

Returns:

  • (Hash)


1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
# File 'lib/vidispine/api/utilities.rb', line 1593

def storage_file_path_map_create(args = {})
  method = args[:storage_method] || 'file'

  storages = args[:storages] || begin
    storages_response = args[:storages_response] || storages_get
    storages_response['storage']
  end

  storage_file_path_map = { }
  storages.each do |storage|
    storage_methods = storage['method']
    file_storage_method = storage_methods.find { |m| m['uri'].start_with?("#{method}:") }
    next unless file_storage_method
    case method
      when 'vxa'
        md = Hash[ storage['metadata']['field'].map { |m| [ m['key'], m['value'] ] } ]
        address = md['vxaLocalPath']
        address.concat('/') if address && !(address.empty? || address.end_with?('/'))
      when 's3'
        # "s3://{access_key}:_VSENC__{encrypted_secret_access_key}@{bucket_name}/?region=us-east-1?sseAlgorithm=aws:kms?signer=AWSS3V4SignerType/"
        uri = file_storage_method['uri']
        , bucket_info = uri.split('@')
        address, args = bucket_info.split('?').first
        address.chomp!('/') if address.end_with?('/')
      else
        uri = file_storage_method['uri']
        match = uri.match(/(.*):\/\/(.*)/)
        address = match[2]
    end
    storage_file_path_map[address] = storage['id']
  end
  storage_file_path_map
end

#symbolize_keys(value, recursive = true) ⇒ Object

Converts hash keys to symbols

Parameters:

  • value (Hash)

    hash

  • recursive (Boolean) (defaults to: true)

    Will recurse into any values that are hashes or arrays



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vidispine/api/utilities.rb', line 25

def symbolize_keys (value, recursive = true)
  case value
    when Hash
      new_val = {}
      value.each { |k,v|
        k = (k.to_sym rescue k)
        v = symbolize_keys(v, true) if recursive and (v.is_a? Hash or v.is_a? Array)
        new_val[k] = v
      }
      return new_val
    when Array
      return value.map { |v| symbolize_keys(v, true) }
    else
      return value
  end
end

#transform_metadata_get_response_to_hash(_response, options = { }) ⇒ Object



256
257
258
# File 'lib/vidispine/api/utilities.rb', line 256

def (_response, options = { })
  self.class.(_response, options)
end

#transform_metadata_to_fields(metadata_in, map = { }, options = { }) ⇒ Object

Transforms metadata from key value to MetadataDocument field and group sequences { k1 => v1, k2 => v2 } becomes

{
  :field => [
    { :name => map[k1][:field], :value => [ { :value => v1 } ] },
    { :name => map[k2][:field], :value => [ { :value => v2 } ] }
  ],
  :group => [ ]
}

Metadata Map Example metadata_map = {

'Campaign Title' => { :group => 'Film', :field => 'portal_mf409876' },
'Client' => { :group => 'Editorial and Film', :field => 'portal_mf982459' },
'Product' => { :group => 'Film', :field => 'portal_mf264604' },
'Studio Tracking ID' => { :group => 'Film', :field => 'portal_mf846239' },
#'File Path' => { :field => 'portal_mf48881', :group => 'Film' }
#'File Path' => { :field => 'portal_mf48881' }
'File Path' => 'portal_mf48881'

}

metadata group name { ‘Some Field’ => { :field => ‘properFieldName’, :group => ‘groupName’ } }

Parameters:

  • metadata_in (Hash)

    Key value pair where the key is an alias for a vidispine metadata field name

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

    A mapping of metadata field name aliases to proper metadata field name and optionally

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

Options Hash (options):

  • :default_metadata_map (Hash) — default: default_metadata_map
  • :metadata_map (Hash)

See Also:



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/vidispine/api/utilities.rb', line 169

def (, map = { }, options = { })
  map = (options[:default_metadata_map] || ).merge(map.merge(options[:metadata_map] || { }))
  groups = { }
  .each do |k,v|
    _map = map[k]
    next unless _map
    [*_map].each do |_map_|
      _map_ = { :field => _map_ } if _map_.is_a?(String)
      (groups[_map_[:group]] ||= { })[_map_[:field]] = v
    end
  end

  _field = groups.delete(nil) { { } }.map { |fname, values| { :name => fname, :value => [*values].map { |v| { :value => v } } } }
  _group = groups.map { |name, fields| { :name => name, :field => fields.map { |fname, values| { :name => fname, :value => [*values].map { |v| { :value => v } } } } } }

  # metadata_out = { }
  # metadata_out[:field] = _field unless _field.empty?
  # metadata_out[:group] = _group unless _group.empty?
  # metadata_out
  { :field => _field, :group => _group }
end

#wait_for_job_completion(args = { }) ⇒ Hash

Waits for a job to complete

Accepts a block where the following is exposed:

:time_started [TIme]
:poll_interval [Integer]
:latest_response [Hash]
:job_status [String]
:continue_monitoring [Boolean] Can be set to false and job monitoring will exit
:delay [Integer]

Parameters:

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

Options Hash (args):

  • :job_id (String) — default: Required
  • :delay (Integer) — default: 15
  • :timeout (Integer)

    The timeout in seconds

Returns:

  • (Hash)

    :last_response [Hash] :time_started [Time] :time_ended [Time] Includes the last poll interval :timed_out [Boolean]

Raises:

  • (ArgumentError)


1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
# File 'lib/vidispine/api/utilities.rb', line 1647

def wait_for_job_completion(args = { })
  job_id = args[:job_id]
  raise ArgumentError, 'job_id is a required argument.' unless job_id
  delay = args[:delay] || 15

  timeout = args[:timeout]
  time_started = Time.now

  _response = { }
  continue_monitoring = true
  timed_out = false
  loop do
    _response = job_get(:job_id => job_id)
    break unless _response

    job_status = _response['status']
    break if %w(FAILED_TOTAL FINISHED FINISHED_WARNING FINISHED_TOTAL ABORTED).include?(job_status)

    break if timeout and (timed_out = ((Time.now - time_started) > timeout))

    if block_given?
      yield_out = {
        :time_started => time_started,
        :poll_interval => delay,
        :latest_response => _response,
        :job_status => job_status,
        :continue_monitoring => continue_monitoring,
        :delay => delay
      }
      yield yield_out
      break unless continue_monitoring
    else
      logger.debug { "Waiting for job completion. Job: #{job_status} Poll Interval: #{delay}" }
    end

    sleep(delay)
  end
  time_ended = Time.now

  { :last_response => _response, :time_started => time_started, :time_ended => time_ended, :timed_out => timed_out }
end