X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/3b0011c6f2312bd6517cf470a80b921bd65e86fe..3a525b4ef49ca90050183713292c95cd244f3123:/src/common/webdav.ts diff --git a/src/common/webdav.ts b/src/common/webdav.ts index 671c0e36..8aea568a 100644 --- a/src/common/webdav.ts +++ b/src/common/webdav.ts @@ -25,9 +25,23 @@ export class WebDAV { this.request({ ...config, url, method: 'PUT', - data, + data }) + upload = (url: string, path: string, files: File[], config: WebDAVRequestConfig = {}) => { + const fd = new FormData(); + fd.append('path', path); + files.forEach((f, idx) => { + fd.append(`file-${idx}`, f); + }); + + return this.request({ + ...config, url, + method: 'PUT', + data: fd + }); + } + copy = (url: string, destination: string, config: WebDAVRequestConfig = {}) => this.request({ ...config, url, @@ -48,7 +62,6 @@ export class WebDAV { method: 'DELETE' }) - private request = (config: RequestConfig) => { return new Promise((resolve, reject) => { const r = this.createRequest(); @@ -59,23 +72,36 @@ export class WebDAV { .keys(headers) .forEach(key => r.setRequestHeader(key, headers[key])); - if (config.onProgress) { - r.addEventListener('progress', config.onProgress); + if (config.onUploadProgress) { + r.upload.addEventListener('progress', config.onUploadProgress); } - r.addEventListener('load', () => resolve(r)); - r.addEventListener('error', () => reject(r)); + r.addEventListener('load', () => { + if (r.status === 404) { + return reject(r); + } else { + return resolve(r); + } + }); + + r.addEventListener('error', () => { + return reject(r); + }); + + r.upload.addEventListener('error', () => { + return reject(r); + }); r.send(config.data); }); - } } + export interface WebDAVRequestConfig { headers?: { [key: string]: string; }; - onProgress?: (event: ProgressEvent) => void; + onUploadProgress?: (event: ProgressEvent) => void; } interface WebDAVDefaults { @@ -88,5 +114,5 @@ interface RequestConfig { url: string; headers?: { [key: string]: string }; data?: any; - onProgress?: (event: ProgressEvent) => void; + onUploadProgress?: (event: ProgressEvent) => void; }