Merge branch '18947-githttpd'
[arvados.git] / apps / workbench / app / assets / javascripts / arvados_client.js
index 584928f6b4feb61152ba0edeb592ae2972474c5b..3fe8968eca7bb7ebd973c87d551fda118a158d14 100644 (file)
@@ -1,3 +1,7 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
 angular.
     module('Arvados', []).
     service('ArvadosClient', ArvadosClient);
@@ -11,7 +15,6 @@ function ArvadosClient(arvadosApiToken, arvadosDiscoveryUri) {
     return this;
     ////////////////////////////////
 
-    var that = this;
     var promiseDiscovery;
     var discoveryDoc;
 
@@ -22,7 +25,7 @@ function ArvadosClient(arvadosApiToken, arvadosDiscoveryUri) {
             var meth = discoveryDoc.resources[controller].methods[action];
             var data = $.extend({}, params, {_method: meth.httpMethod});
             $.each(data, function(k, v) {
-                if (typeof(v) == 'object') {
+                if (typeof(v) === 'object') {
                     data[k] = JSON.stringify(v);
                 }
             });
@@ -44,38 +47,45 @@ function ArvadosClient(arvadosApiToken, arvadosDiscoveryUri) {
         });
     }
 
-    function uniqueNameForManifest(manifest, streamName, origName) {
+    function uniqueNameForManifest(manifest, newStreamName, origName) {
         // Return an (escaped) filename starting with (unescaped)
-        // origName that won't conflict with any existing names in
-        // the manifest if saved under streamName. streamName must
-        // be exactly as given in the manifest, e.g., "." or
-        // "./foo" or "./foo/bar".
+        // origName that won't conflict with any existing names in the
+        // manifest if saved under newStreamName. newStreamName must
+        // be exactly as given in the manifest, e.g., "." or "./foo"
+        // or "./foo/bar".
         //
         // Example:
         //
-        // unique('./foo [...] 0:0:bar\040baz\n', '.', 'foo/bar baz')
+        // uniqueNameForManifest('./foo [...] 0:0:bar\\040baz.txt\n', '.',
+        //                       'foo/bar baz.txt')
         // =>
-        // 'foo/bar\\040baz\\040(1)'
+        // 'foo/bar\\040baz\\040(1).txt'
         var newName;
         var nameStub = origName;
         var suffixInt = null;
         var ok = false;
+        var lineMatch, linesRe = /(\S+).*/gm;
+        var fileTokenMatch, fileTokensRe = / \d+:\d+:(\S+)/g;
         while (!ok) {
             ok = true;
             // Add ' (N)' before the filename extension, if any.
             newName = (!suffixInt ? nameStub :
                        nameStub.replace(/(\.[^.]*)?$/, ' ('+suffixInt+')$1')).
                 replace(/ /g, '\\040');
-            $.each(manifest.split('\n'), function(_, line) {
-                var i, match, foundName;
-                var toks = line.split(' ');
-                for (var i=1; i<toks.length && ok; i++)
-                    if (match = toks[i].match(/^\d+:\d+:(\S+)/))
-                        if (toks[0] + '/' + match[1] === streamName + '/' + newName) {
-                            suffixInt = (suffixInt || 0) + 1;
-                            ok = false;
-                        }
-            });
+            while (ok && null !==
+                   (lineMatch = linesRe.exec(manifest))) {
+                // lineMatch is [theEntireLine, streamName]
+                while (ok && null !==
+                       (fileTokenMatch = fileTokensRe.exec(lineMatch[0]))) {
+                    // fileTokenMatch is [theEntireToken, fileName]
+                    if (lineMatch[1] + '/' + fileTokenMatch[1]
+                        ===
+                        newStreamName + '/' + newName) {
+                        ok = false;
+                    }
+                }
+            }
+            suffixInt = (suffixInt || 0) + 1;
         }
         return newName;
     }