X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/f45dd93f467fe02c33908c0b4b3ff8ba01827bc9..f5f72a4ee9b00aab5492f8991677b6503a6f2ac3:/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 581a6fa661..9a05fb61ef 100644 --- a/src/services/collection-service/collection-service-files-response.ts +++ b/src/services/collection-service/collection-service-files-response.ts @@ -2,21 +2,14 @@ // // 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'; +import { customDecodeURI } from "~/common/url"; export const sortFilesTree = (tree: Tree) => { - return mapTree(node => { - const children = getNodeChildren(node.id)(tree) - .map(id => getNode(id)(tree)) - .filter(node => node !== undefined) as TreeNode[]; + return mapTree(node => { + const children = getNodeChildren(node.id)(tree); children.sort((a, b) => a.value.type !== b.value.type @@ -24,33 +17,46 @@ export const sortFilesTree = (tree: Tree) : a.value.name.localeCompare(b.value.name) ); return { ...node, children: children.map(child => child.id) }; - })(tree) as Tree; + })(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 url = getTagValue(element, 'D:href', ''); - const nameSuffix = `/${name || ''}`; + const url = customDecodeURI(getTagValue(element, 'D:href', '')); + const nameSuffix = name; + const collectionUuidMatch = collectionUrlPrefix.exec(url); + const collectionUuid = collectionUuidMatch ? collectionUuidMatch.pop() : ''; const directory = url .replace(collectionUrlPrefix, '') - .replace(nameSuffix, ''); + .replace(nameSuffix, '') + .replace(/\/\//g, '/'); + const parentPath = directory.replace(/\/$/, ''); const data = { url, - id: `${directory}/${name}`, + id: [ + collectionUuid ? collectionUuid : '', + directory ? parentPath : '', + '/' + name + ].join(''), name, - path: directory, + path: parentPath, }; - return getTagValue(element, 'D:resourcetype', '') + const result = getTagValue(element, 'D:resourcetype', '') ? createCollectionDirectory(data) : createCollectionFile({ ...data, size }); + return result; }); }; + +export const getFileFullPath = ({ name, path }: CollectionFile | CollectionDirectory) => { + return `${path}/${name}`; +}; \ No newline at end of file