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=[0-9a-zA-Z\-]*/;
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 || ''}`;
39 .replace(collectionUrlPrefix, '')
40 .replace(nameSuffix, '');
44 id: `${directory}/${name}`,
49 return getTagValue(element, 'D:resourcetype', '')
50 ? createCollectionDirectory(data)
51 : createCollectionFile({ ...data, size });