X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/e13e7dd672160e4ab5569c24133f4f6032db4a9a..dee0cb67a02c25f0f3174681579d0898d880caa3:/src/services/collection-service/collection-service-files-response.ts diff --git a/src/services/collection-service/collection-service-files-response.ts b/src/services/collection-service/collection-service-files-response.ts index 4545096f..f3a2147e 100644 --- a/src/services/collection-service/collection-service-files-response.ts +++ b/src/services/collection-service/collection-service-files-response.ts @@ -2,45 +2,47 @@ // // SPDX-License-Identifier: AGPL-3.0 -import { createCollectionFilesTree, CollectionDirectory, CollectionFile, CollectionFileType, createCollectionDirectory, createCollectionFile } from "../../models/collection-file"; -import { Tree, mapTree, getNodeChildren, getNode, TreeNode } from "../../models/tree"; -import { getTagValue } from "../../common/xml"; - -export const parseFilesResponse = (document: Document) => { - const files = extractFilesData(document); - const tree = createCollectionFilesTree(files); - return sortFilesTree(tree); -}; +import { CollectionDirectory, CollectionFile, CollectionFileType, createCollectionDirectory, createCollectionFile } from "../../models/collection-file"; +import { getTagValue } from "~/common/xml"; +import { getNodeChildren, Tree, mapTree } from '~/models/tree'; export const sortFilesTree = (tree: Tree) => { - return mapTree(node => { - const children = getNodeChildren(node.id)(tree).map(id => getNode(id)(tree)) as TreeNode[]; + return mapTree(node => { + const children = getNodeChildren(node.id)(tree); + children.sort((a, b) => a.value.type !== b.value.type ? a.value.type === CollectionFileType.DIRECTORY ? -1 : 1 : a.value.name.localeCompare(b.value.name) ); - return { ...node, children: children.map(child => child.id) } as TreeNode; + return { ...node, children: children.map(child => child.id) }; })(tree); }; export const extractFilesData = (document: Document) => { - const collectionUrlPrefix = /\/c=[0-9a-zA-Z\-]*/; + const collectionUrlPrefix = /\/c=([^\/]*)/; return Array .from(document.getElementsByTagName('D:response')) .slice(1) // omit first element which is collection itself .map(element => { const name = getTagValue(element, 'D:displayname', ''); const size = parseInt(getTagValue(element, 'D:getcontentlength', '0'), 10); - const pathname = getTagValue(element, 'D:href', ''); + const url = getTagValue(element, 'D:href', ''); const nameSuffix = `/${name || ''}`; - const directory = pathname + const collectionUuidMatch = collectionUrlPrefix.exec(url); + const collectionUuid = collectionUuidMatch ? collectionUuidMatch.pop() : ''; + const directory = url .replace(collectionUrlPrefix, '') .replace(nameSuffix, ''); + const data = { - url: pathname, - id: `${directory}/${name}`, + url, + id: [ + collectionUuid ? collectionUuid : '', + directory ? '/' + directory.replace(/^\//, '') : '', + '/' + name + ].join(''), name, path: directory, }; @@ -51,3 +53,6 @@ export const extractFilesData = (document: Document) => { }); }; + +export const getFileFullPath = ({ name, path }: CollectionFile | CollectionDirectory) => + `${path}/${name}`;