Replace loading -> upload with streamed upload, fix reporting error on file remove
[arvados-workbench2.git] / src / common / webdav.ts
index 27e1f22de5be8c642072d7ae42b80f9189fe524b..8aea568af320bf7c91a10d5b476162ad7453d2a0 100644 (file)
@@ -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,
@@ -62,13 +76,27 @@ export class WebDAV {
                 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;