// 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";
+import { getTagValue } from "~/common/xml";
+import { getNodeChildren, Tree, mapTree } from '~/models/tree';
export const parseFilesResponse = (document: Document) => {
const files = extractFilesData(document);
};
export const sortFilesTree = (tree: Tree<CollectionDirectory | CollectionFile>) => {
- return mapTree(node => {
- const children = getNodeChildren(node.id)(tree)
- .map(id => getNode(id)(tree))
- .filter(node => node !== undefined) as TreeNode<CollectionDirectory | CollectionFile>[];
+ return mapTree<CollectionDirectory | CollectionFile>(node => {
+ const children = getNodeChildren(node.id)(tree);
children.sort((a, b) =>
a.value.type !== b.value.type
: a.value.name.localeCompare(b.value.name)
);
return { ...node, children: children.map(child => child.id) };
- })(tree) as Tree<CollectionDirectory | CollectionFile>;
+ })(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
const size = parseInt(getTagValue(element, 'D:getcontentlength', '0'), 10);
const url = getTagValue(element, 'D:href', '');
const nameSuffix = `/${name || ''}`;
+ const collectionUuidMatch = collectionUrlPrefix.exec(url);
+ const collectionUuid = collectionUuidMatch ? collectionUuidMatch.pop() : '';
const directory = url
.replace(collectionUrlPrefix, '')
.replace(nameSuffix, '');
+
const data = {
url,
- id: `${directory}/${name}`,
+ id: [
+ collectionUuid ? collectionUuid : '',
+ directory ? '/' + directory.replace(/^\//, '') : '',
+ '/' + name
+ ].join(''),
name,
path: directory,
};
});
};
+
+export const getFileFullPath = ({ name, path }: CollectionFile | CollectionDirectory) =>
+ `${path}/${name}`;