1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { createCollectionFilesTree, 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 parseFilesResponse = (document: Document) => {
10 const files = extractFilesData(document);
11 const tree = createCollectionFilesTree(files);
12 return sortFilesTree(tree);
15 export const sortFilesTree = (tree: Tree<CollectionDirectory | CollectionFile>) => {
16 return mapTree<CollectionDirectory | CollectionFile>(node => {
17 const children = getNodeChildren(node.id)(tree);
19 children.sort((a, b) =>
20 a.value.type !== b.value.type
21 ? a.value.type === CollectionFileType.DIRECTORY ? -1 : 1
22 : a.value.name.localeCompare(b.value.name)
24 return { ...node, children: children.map(child => child.id) };
28 export const extractFilesData = (document: Document) => {
29 const collectionUrlPrefix = /\/c=([^\/]*)/;
31 .from(document.getElementsByTagName('D:response'))
32 .slice(1) // omit first element which is collection itself
34 const name = getTagValue(element, 'D:displayname', '');
35 const size = parseInt(getTagValue(element, 'D:getcontentlength', '0'), 10);
36 const url = getTagValue(element, 'D:href', '');
37 const nameSuffix = `/${name || ''}`;
38 const collectionUuidMatch = collectionUrlPrefix.exec(url);
39 const collectionUuid = collectionUuidMatch ? collectionUuidMatch.pop() : '';
41 .replace(collectionUrlPrefix, '')
42 .replace(nameSuffix, '');
48 collectionUuid ? collectionUuid : '',
49 directory ? '/' + directory.replace(/^\//, '') : '',
56 return getTagValue(element, 'D:resourcetype', '')
57 ? createCollectionDirectory(data)
58 : createCollectionFile({ ...data, size });
63 export const getFileFullPath = ({ name, path }: CollectionFile | CollectionDirectory) =>