1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
7 service('ArvadosClient', ArvadosClient);
9 ArvadosClient.$inject = ['arvadosApiToken', 'arvadosDiscoveryUri']
10 function ArvadosClient(arvadosApiToken, arvadosDiscoveryUri) {
12 apiPromise: apiPromise,
13 uniqueNameForManifest: uniqueNameForManifest
16 ////////////////////////////////
22 function apiPromise(controller, action, params) {
23 // Start an API call. Return a promise that will resolve with
25 return getDiscoveryDoc().then(function() {
26 var meth = discoveryDoc.resources[controller].methods[action];
27 var data = $.extend({}, params, {_method: meth.httpMethod});
28 $.each(data, function(k, v) {
29 if (typeof(v) === 'object') {
30 data[k] = JSON.stringify(v);
33 var path = meth.path.replace(/{(.*?)}/, function(_, key) {
36 return encodeURIComponent(val);
39 url: discoveryDoc.baseUrl + path,
45 Authorization: 'OAuth2 ' + arvadosApiToken
51 function uniqueNameForManifest(manifest, newStreamName, origName) {
52 // Return an (escaped) filename starting with (unescaped)
53 // origName that won't conflict with any existing names in the
54 // manifest if saved under newStreamName. newStreamName must
55 // be exactly as given in the manifest, e.g., "." or "./foo"
60 // uniqueNameForManifest('./foo [...] 0:0:bar\\040baz.txt\n', '.',
63 // 'foo/bar\\040baz\\040(1).txt'
65 var nameStub = origName;
68 var lineMatch, linesRe = /(\S+).*/gm;
69 var fileTokenMatch, fileTokensRe = / \d+:\d+:(\S+)/g;
72 // Add ' (N)' before the filename extension, if any.
73 newName = (!suffixInt ? nameStub :
74 nameStub.replace(/(\.[^.]*)?$/, ' ('+suffixInt+')$1')).
75 replace(/ /g, '\\040');
77 (lineMatch = linesRe.exec(manifest))) {
78 // lineMatch is [theEntireLine, streamName]
80 (fileTokenMatch = fileTokensRe.exec(lineMatch[0]))) {
81 // fileTokenMatch is [theEntireToken, fileName]
82 if (lineMatch[1] + '/' + fileTokenMatch[1]
84 newStreamName + '/' + newName) {
89 suffixInt = (suffixInt || 0) + 1;
94 function getDiscoveryDoc() {
95 if (!promiseDiscovery) {
96 promiseDiscovery = $.ajax({
97 url: arvadosDiscoveryUri,
99 }).then(function(data, status, xhr) {
103 return promiseDiscovery;