3 service('ArvadosClient', ArvadosClient);
5 ArvadosClient.$inject = ['arvadosApiToken', 'arvadosDiscoveryUri']
6 function ArvadosClient(arvadosApiToken, arvadosDiscoveryUri) {
8 apiPromise: apiPromise,
9 uniqueNameForManifest: uniqueNameForManifest
12 ////////////////////////////////
18 function apiPromise(controller, action, params) {
19 // Start an API call. Return a promise that will resolve with
21 return getDiscoveryDoc().then(function() {
22 var meth = discoveryDoc.resources[controller].methods[action];
23 var data = $.extend({}, params, {_method: meth.httpMethod});
24 $.each(data, function(k, v) {
25 if (typeof(v) == 'object') {
26 data[k] = JSON.stringify(v);
29 var path = meth.path.replace(/{(.*?)}/, function(_, key) {
32 return encodeURIComponent(val);
35 url: discoveryDoc.baseUrl + path,
41 Authorization: 'OAuth2 ' + arvadosApiToken
47 function uniqueNameForManifest(manifest, newStreamName, origName) {
48 // Return an (escaped) filename starting with (unescaped)
49 // origName that won't conflict with any existing names in the
50 // manifest if saved under newStreamName. newStreamName must
51 // be exactly as given in the manifest, e.g., "." or "./foo"
56 // uniqueNameForManifest('./foo [...] 0:0:bar\\040baz.txt\n', '.',
59 // 'foo/bar\\040baz\\040(1).txt'
61 var nameStub = origName;
64 var lineMatch, linesRe = /[^\n]+/g;
65 var streamNameMatch, streamNameRe = /^\S+/;
66 var fileTokenMatch, fileTokensRe = / \d+:\d+:(\S+)/g;
69 // Add ' (N)' before the filename extension, if any.
70 newName = (!suffixInt ? nameStub :
71 nameStub.replace(/(\.[^.]*)?$/, ' ('+suffixInt+')$1')).
72 replace(/ /g, '\\040');
74 (lineMatch = linesRe.exec(manifest))) {
75 streamNameMatch = streamNameRe.exec(lineMatch[0]);
77 (fileTokenMatch = fileTokensRe.exec(lineMatch[0]))) {
78 if (streamNameMatch[0] + '/' + fileTokenMatch[1]
80 newStreamName + '/' + newName) {
85 suffixInt = (suffixInt || 0) + 1;
90 function getDiscoveryDoc() {
91 if (!promiseDiscovery) {
92 promiseDiscovery = $.ajax({
93 url: arvadosDiscoveryUri,
95 }).then(function(data, status, xhr) {
99 return promiseDiscovery;