+ async files(uuid: string) {
+ const request = await this.webdavClient.propfind(`/c=${uuid}`);
+ if (request.responseXML != null) {
+ const files = this.extractFilesData(request.responseXML);
+ const tree = createCollectionFilesTree(files);
+ const sortedTree = mapTree(node => {
+ const children = getNodeChildren(node.id)(tree).map(id => getNode(id)(tree)) as TreeNode<CollectionDirectory | CollectionFile>[];
+ 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) };
+ })(tree);
+ return sortedTree;
+ }
+ return Promise.reject();
+ }
+
+ async deleteFile(collectionUuid: string, filePath: string) {
+ return this.webdavClient.delete(`/c=${collectionUuid}${filePath}`);
+ }
+
+ extractFilesData(document: Document) {
+ const collectionUrlPrefix = /\/c=[0-9a-zA-Z\-]*/;
+ 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 nameSuffix = `/${name || ''}`;
+ const directory = pathname
+ .replace(collectionUrlPrefix, '')
+ .replace(nameSuffix, '');
+ const href = this.webdavClient.defaults.baseURL + pathname + '?api_token=' + this.authService.getApiToken();
+
+ const data = {
+ url: href,
+ id: `${directory}/${name}`,
+ name,
+ path: directory,
+ };
+
+ return getTagValue(element, 'D:resourcetype', '')
+ ? createCollectionDirectory(data)
+ : createCollectionFile({ ...data, size });
+
+ });
+ }
+
+