1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
9 export const sortFilesTree = (tree: Tree<CollectionDirectory | CollectionFile>) => {
10 return mapTree<CollectionDirectory | CollectionFile>(node => {
11 const children = getNodeChildren(node.id)(tree);
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)
18 return { ...node, children: children.map(child => child.id) };
22 export const extractFilesData = (document: Document) => {
23 const collectionUrlPrefix = /\/c=([^\/]*)/;
25 .from(document.getElementsByTagName('D:response'))
26 .slice(1) // omit first element which is collection itself
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() : '';
35 .replace(collectionUrlPrefix, '')
36 .replace(nameSuffix, '');
38 const parentPath = directory.replace(/\/$/, '');
42 collectionUuid ? collectionUuid : '',
43 directory ? parentPath : '',
50 return getTagValue(element, 'D:resourcetype', '')
51 ? createCollectionDirectory(data)
52 : createCollectionFile({ ...data, size });
57 export const getFileFullPath = ({ name, path }: CollectionFile | CollectionDirectory) =>