File: src/components/Tree/WebDAV.ts

Recommend this page to a friend!
  Classes of Dom Hastings   JS Webdav Client   src/components/Tree/WebDAV.ts   Download  
File: src/components/Tree/WebDAV.ts
Role: Class source
Content type: text/plain
Description: Class source
Class: JS Webdav Client
Access files of a Webdav server
Author: By
Last change:
Date: 6 months ago
Size: 2,288 bytes
 

Contents

Class file image Download
import { leadingAndTrailingSlash, trimSlashes } from '../../lib/joinPath'; import DataProvider from './DataProvider'; import Node from './Node'; import Response from '../../lib/Response'; interface WebDAVOptions { debug: boolean; depth: number; } export class WebDAV implements DataProvider { #cache: Map<string, Promise<Node[]>> = new Map(); #options: WebDAVOptions = { debug: true, depth: 2, }; constructor(options: Partial<WebDAVOptions> = {}) { this.#options = { ...this.#options, ...options, }; } async getChildren( node: Node, bypassCache: boolean = false, depth: number = this.#options.depth ): Promise<Node[] | null> { if (depth === 0) { return null; } const path = node.fullPath().length === 0 ? '/' : leadingAndTrailingSlash(node.fullPath().join('/')); if (!this.#cache.has(path) || bypassCache) { this.#cache.set( path, fetch(path, { method: 'PROPFIND', headers: { Depth: '1', }, }) .then(async (response) => { if (!response.ok) { if (this.#options.debug) { console.error(response); } return []; } const davResponse = new Response( await response.text() ).responseToPrimitives(), directories = davResponse .filter((entry) => entry.directory && entry.fullPath !== path) .sort((a, b) => (a.fullPath < b.fullPath ? -1 : 1)), children = directories.map((entry) => { const fullPath = trimSlashes(entry.fullPath).split('/'); const name = decodeURIComponent(fullPath.slice(0).pop()), childNode = new Node(fullPath, name); this.getChildren(childNode, bypassCache, depth - 1); return childNode; }); node.setChildren(children); return children; }) .catch((e) => { if (this.#options.debug) { console.error(e); } return []; }) ); } return this.#cache.get(path); } } export default WebDAV;