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';
8 import { customDecodeURI } from "~/common/url";
10 export const sortFilesTree = (tree: Tree<CollectionDirectory | CollectionFile>) => {
11 return mapTree<CollectionDirectory | CollectionFile>(node => {
12 const children = getNodeChildren(node.id)(tree);
14 children.sort((a, b) =>
15 a.value.type !== b.value.type
16 ? a.value.type === CollectionFileType.DIRECTORY ? -1 : 1
17 : a.value.name.localeCompare(b.value.name)
19 return { ...node, children: children.map(child => child.id) };
23 export const extractFilesData = (document: Document) => {
24 const collectionUrlPrefix = /\/c=([^\/]*)/;
26 .from(document.getElementsByTagName('D:response'))
27 .slice(1) // omit first element which is collection itself
29 const name = getTagValue(element, 'D:displayname', '');
30 const size = parseInt(getTagValue(element, 'D:getcontentlength', '0'), 10);
31 const href = getTagValue(element, 'D:href', '');
32 const url = customDecodeURI(href);
33 const nameSuffix = name;
34 const collectionUuidMatch = collectionUrlPrefix.exec(url);
35 const collectionUuid = collectionUuidMatch ? collectionUuidMatch.pop() : '';
37 .replace(collectionUrlPrefix, '')
38 .replace(nameSuffix, '')
39 .replace(/\/\//g, '/');
41 const parentPath = directory.replace(/\/$/, '');
45 collectionUuid ? collectionUuid : '',
46 directory ? parentPath : '',
53 const result = getTagValue(element, 'D:resourcetype', '')
54 ? createCollectionDirectory(data)
55 : createCollectionFile({ ...data, size });
61 export const getFileFullPath = ({ name, path }: CollectionFile | CollectionDirectory) => {
62 return `${path}/${name}`;