X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/f62ba0aeff590ef1c57669d4f46e43b5ee169522..2a7fd99c212c33a1ec9911f8529fa5afc59a7bb2:/src/common/webdav.ts diff --git a/src/common/webdav.ts b/src/common/webdav.ts index c4d8acaea2..1f3da0d614 100644 --- a/src/common/webdav.ts +++ b/src/common/webdav.ts @@ -2,19 +2,33 @@ // // SPDX-License-Identifier: AGPL-3.0 +import { customEncodeURI } from "./url"; + export class WebDAV { - defaults: WebDAVDefaults = { + private defaults: WebDAVDefaults = { baseURL: '', - headers: {}, + headers: { + 'Cache-Control': 'no-cache' + }, }; constructor(config?: Partial, private createRequest = () => new XMLHttpRequest()) { if (config) { - this.defaults = { ...this.defaults, ...config }; + this.defaults = { + ...this.defaults, + ...config, + headers: { + ...this.defaults.headers, + ...config.headers + }, + }; } } + getBaseUrl = (): string => this.defaults.baseURL; + setAuthorization = (token?) => this.defaults.headers.Authorization = token; + propfind = (url: string, config: WebDAVRequestConfig = {}) => this.request({ ...config, url, @@ -28,6 +42,12 @@ export class WebDAV { data }) + get = (url: string, config: WebDAVRequestConfig = {}) => + this.request({ + ...config, url, + method: 'GET' + }) + upload = (url: string, files: File[], config: WebDAVRequestConfig = {}) => { return Promise.all( files.map(file => this.request({ @@ -74,13 +94,23 @@ export class WebDAV { this.defaults.baseURL = this.defaults.baseURL.replace(/\/+$/, ''); r.open(config.method, `${this.defaults.baseURL - ? this.defaults.baseURL+'/' - : ''}${encodeURI(config.url)}`); + ? this.defaults.baseURL + '/' + : ''}${customEncodeURI(config.url)}`); + const headers = { ...this.defaults.headers, ...config.headers }; Object .keys(headers) .forEach(key => r.setRequestHeader(key, headers[key])); + if (!(window as any).cancelTokens) { + Object.assign(window, { cancelTokens: {} }); + } + + (window as any).cancelTokens[config.url] = () => { + resolve(r); + r.abort(); + } + if (config.onUploadProgress) { r.upload.addEventListener('progress', config.onUploadProgress); } @@ -126,4 +156,4 @@ interface RequestConfig { headers?: { [key: string]: string }; data?: any; onUploadProgress?: (event: ProgressEvent) => void; -} \ No newline at end of file +}