17337: Added more tests, fixed whitespace issue
[arvados-workbench2.git] / src / services / collection-service / collection-service-files-response.ts
index b8a7970d58d4310d8228b8d0c80fb04188614810..6f8b3de3a9b99bc0aac4ecb1100b95f71d6af309 100644 (file)
@@ -2,15 +2,10 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { createCollectionFilesTree, CollectionDirectory, CollectionFile, CollectionFileType, createCollectionDirectory, createCollectionFile } from "../../models/collection-file";
+import { CollectionDirectory, CollectionFile, CollectionFileType, createCollectionDirectory, createCollectionFile } from "../../models/collection-file";
 import { getTagValue } from "~/common/xml";
 import { getNodeChildren, Tree, mapTree } from '~/models/tree';
-
-export const parseFilesResponse = (document: Document) => {
-    const files = extractFilesData(document);
-    const tree = createCollectionFilesTree(files);
-    return sortFilesTree(tree);
-};
+import { escapeHashIfRequired } from "~/common/url";
 
 export const sortFilesTree = (tree: Tree<CollectionDirectory | CollectionFile>) => {
     return mapTree<CollectionDirectory | CollectionFile>(node => {
@@ -26,7 +21,7 @@ export const sortFilesTree = (tree: Tree<CollectionDirectory | CollectionFile>)
 };
 
 export const extractFilesData = (document: Document) => {
-    const collectionUrlPrefix = /\/c=[0-9a-zA-Z\-]*/;
+    const collectionUrlPrefix = /\/c=([^\/]*)/;
     return Array
         .from(document.getElementsByTagName('D:response'))
         .slice(1) // omit first element which is collection itself
@@ -34,21 +29,31 @@ export const extractFilesData = (document: Document) => {
             const name = getTagValue(element, 'D:displayname', '');
             const size = parseInt(getTagValue(element, 'D:getcontentlength', '0'), 10);
             const url = getTagValue(element, 'D:href', '');
-            const nameSuffix = `/${name || ''}`;
+            const nameSuffix = `/${escapeHashIfRequired(name) || ''}`;
+            const collectionUuidMatch = collectionUrlPrefix.exec(url);
+            const collectionUuid = collectionUuidMatch ? collectionUuidMatch.pop() : '';
             const directory = url
                 .replace(collectionUrlPrefix, '')
                 .replace(nameSuffix, '');
 
+            const parentPath = directory.replace(/\/$/, '');
             const data = {
                 url,
-                id: `${directory}/${name}`,
+                id: [
+                    collectionUuid ? collectionUuid : '',
+                    directory ? parentPath : '',
+                    '/' + name
+                ].join(''),
                 name,
-                path: directory,
+                path: parentPath,
             };
 
             return getTagValue(element, 'D:resourcetype', '')
                 ? createCollectionDirectory(data)
                 : createCollectionFile({ ...data, size });
-
         });
 };
+
+export const getFileFullPath = ({ name, path }: CollectionFile | CollectionDirectory) => {
+    return `${path}/${name}`;
+};
\ No newline at end of file