18947: Remove errant uses of runsu.sh.
[arvados.git] / apps / workbench / app / assets / javascripts / arvados_client.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 angular.
6     module('Arvados', []).
7     service('ArvadosClient', ArvadosClient);
8
9 ArvadosClient.$inject = ['arvadosApiToken', 'arvadosDiscoveryUri']
10 function ArvadosClient(arvadosApiToken, arvadosDiscoveryUri) {
11     $.extend(this, {
12         apiPromise: apiPromise,
13         uniqueNameForManifest: uniqueNameForManifest
14     });
15     return this;
16     ////////////////////////////////
17
18     var that = this;
19     var promiseDiscovery;
20     var discoveryDoc;
21
22     function apiPromise(controller, action, params) {
23         // Start an API call. Return a promise that will resolve with
24         // the API response.
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);
31                 }
32             });
33             var path = meth.path.replace(/{(.*?)}/, function(_, key) {
34                 var val = data[key];
35                 delete data[key];
36                 return encodeURIComponent(val);
37             });
38             return $.ajax({
39                 url: discoveryDoc.baseUrl + path,
40                 type: 'POST',
41                 crossDomain: true,
42                 dataType: 'json',
43                 data: data,
44                 headers: {
45                     Authorization: 'OAuth2 ' + arvadosApiToken
46                 }
47             });
48         });
49     }
50
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"
56         // or "./foo/bar".
57         //
58         // Example:
59         //
60         // uniqueNameForManifest('./foo [...] 0:0:bar\\040baz.txt\n', '.',
61         //                       'foo/bar baz.txt')
62         // =>
63         // 'foo/bar\\040baz\\040(1).txt'
64         var newName;
65         var nameStub = origName;
66         var suffixInt = null;
67         var ok = false;
68         var lineMatch, linesRe = /(\S+).*/gm;
69         var fileTokenMatch, fileTokensRe = / \d+:\d+:(\S+)/g;
70         while (!ok) {
71             ok = true;
72             // Add ' (N)' before the filename extension, if any.
73             newName = (!suffixInt ? nameStub :
74                        nameStub.replace(/(\.[^.]*)?$/, ' ('+suffixInt+')$1')).
75                 replace(/ /g, '\\040');
76             while (ok && null !==
77                    (lineMatch = linesRe.exec(manifest))) {
78                 // lineMatch is [theEntireLine, streamName]
79                 while (ok && null !==
80                        (fileTokenMatch = fileTokensRe.exec(lineMatch[0]))) {
81                     // fileTokenMatch is [theEntireToken, fileName]
82                     if (lineMatch[1] + '/' + fileTokenMatch[1]
83                         ===
84                         newStreamName + '/' + newName) {
85                         ok = false;
86                     }
87                 }
88             }
89             suffixInt = (suffixInt || 0) + 1;
90         }
91         return newName;
92     }
93
94     function getDiscoveryDoc() {
95         if (!promiseDiscovery) {
96             promiseDiscovery = $.ajax({
97                 url: arvadosDiscoveryUri,
98                 crossDomain: true
99             }).then(function(data, status, xhr) {
100                 discoveryDoc = data;
101             });
102         }
103         return promiseDiscovery;
104     }
105 }