17426: Add "enableWhenPristine" option for dialog boxes.
[arvados-workbench2.git] / src / services / collection-service / collection-service-files-response.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { CollectionDirectory, CollectionFile, CollectionFileType, createCollectionDirectory, createCollectionFile } from "../../models/collection-file";
6 import { getTagValue } from "~/common/xml";
7 import { getNodeChildren, Tree, mapTree } from '~/models/tree';
8
9 export const sortFilesTree = (tree: Tree<CollectionDirectory | CollectionFile>) => {
10     return mapTree<CollectionDirectory | CollectionFile>(node => {
11         const children = getNodeChildren(node.id)(tree);
12
13         children.sort((a, b) =>
14             a.value.type !== b.value.type
15                 ? a.value.type === CollectionFileType.DIRECTORY ? -1 : 1
16                 : a.value.name.localeCompare(b.value.name)
17         );
18         return { ...node, children: children.map(child => child.id) };
19     })(tree);
20 };
21
22 export const extractFilesData = (document: Document) => {
23     const collectionUrlPrefix = /\/c=([^\/]*)/;
24     return Array
25         .from(document.getElementsByTagName('D:response'))
26         .slice(1) // omit first element which is collection itself
27         .map(element => {
28             const name = getTagValue(element, 'D:displayname', '');
29             const size = parseInt(getTagValue(element, 'D:getcontentlength', '0'), 10);
30             const url = getTagValue(element, 'D:href', '');
31             const nameSuffix = `/${name || ''}`;
32             const collectionUuidMatch = collectionUrlPrefix.exec(url);
33             const collectionUuid = collectionUuidMatch ? collectionUuidMatch.pop() : '';
34             const directory = url
35                 .replace(collectionUrlPrefix, '')
36                 .replace(nameSuffix, '');
37
38             const parentPath = directory.replace(/\/$/, '');
39             const data = {
40                 url,
41                 id: [
42                     collectionUuid ? collectionUuid : '',
43                     directory ? parentPath : '',
44                     '/' + name
45                 ].join(''),
46                 name,
47                 path: parentPath,
48             };
49
50             return getTagValue(element, 'D:resourcetype', '')
51                 ? createCollectionDirectory(data)
52                 : createCollectionFile({ ...data, size });
53         });
54 };
55
56 export const getFileFullPath = ({ name, path }: CollectionFile | CollectionDirectory) =>
57     `${path}/${name}`;