Merge branch '18947-githttpd'
[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 promiseDiscovery;
19     var discoveryDoc;
20
21     function apiPromise(controller, action, params) {
22         // Start an API call. Return a promise that will resolve with
23         // the API response.
24         return getDiscoveryDoc().then(function() {
25             var meth = discoveryDoc.resources[controller].methods[action];
26             var data = $.extend({}, params, {_method: meth.httpMethod});
27             $.each(data, function(k, v) {
28                 if (typeof(v) === 'object') {
29                     data[k] = JSON.stringify(v);
30                 }
31             });
32             var path = meth.path.replace(/{(.*?)}/, function(_, key) {
33                 var val = data[key];
34                 delete data[key];
35                 return encodeURIComponent(val);
36             });
37             return $.ajax({
38                 url: discoveryDoc.baseUrl + path,
39                 type: 'POST',
40                 crossDomain: true,
41                 dataType: 'json',
42                 data: data,
43                 headers: {
44                     Authorization: 'OAuth2 ' + arvadosApiToken
45                 }
46             });
47         });
48     }
49
50     function uniqueNameForManifest(manifest, newStreamName, origName) {
51         // Return an (escaped) filename starting with (unescaped)
52         // origName that won't conflict with any existing names in the
53         // manifest if saved under newStreamName. newStreamName must
54         // be exactly as given in the manifest, e.g., "." or "./foo"
55         // or "./foo/bar".
56         //
57         // Example:
58         //
59         // uniqueNameForManifest('./foo [...] 0:0:bar\\040baz.txt\n', '.',
60         //                       'foo/bar baz.txt')
61         // =>
62         // 'foo/bar\\040baz\\040(1).txt'
63         var newName;
64         var nameStub = origName;
65         var suffixInt = null;
66         var ok = false;
67         var lineMatch, linesRe = /(\S+).*/gm;
68         var fileTokenMatch, fileTokensRe = / \d+:\d+:(\S+)/g;
69         while (!ok) {
70             ok = true;
71             // Add ' (N)' before the filename extension, if any.
72             newName = (!suffixInt ? nameStub :
73                        nameStub.replace(/(\.[^.]*)?$/, ' ('+suffixInt+')$1')).
74                 replace(/ /g, '\\040');
75             while (ok && null !==
76                    (lineMatch = linesRe.exec(manifest))) {
77                 // lineMatch is [theEntireLine, streamName]
78                 while (ok && null !==
79                        (fileTokenMatch = fileTokensRe.exec(lineMatch[0]))) {
80                     // fileTokenMatch is [theEntireToken, fileName]
81                     if (lineMatch[1] + '/' + fileTokenMatch[1]
82                         ===
83                         newStreamName + '/' + newName) {
84                         ok = false;
85                     }
86                 }
87             }
88             suffixInt = (suffixInt || 0) + 1;
89         }
90         return newName;
91     }
92
93     function getDiscoveryDoc() {
94         if (!promiseDiscovery) {
95             promiseDiscovery = $.ajax({
96                 url: arvadosDiscoveryUri,
97                 crossDomain: true
98             }).then(function(data, status, xhr) {
99                 discoveryDoc = data;
100             });
101         }
102         return promiseDiscovery;
103     }
104 }