Merge branch '14910-uploading-files-to-collection'
[arvados-workbench2.git] / src / common / webdav.ts
index 27e1f22de5be8c642072d7ae42b80f9189fe524b..a09e8fdd6de4a97aab496da4d3651446fee135f8 100644 (file)
@@ -25,9 +25,19 @@ export class WebDAV {
         this.request({
             ...config, url,
             method: 'PUT',
-            data,
+            data
         })
 
+    upload = (url: string, files: File[], config: WebDAVRequestConfig = {}) => {
+        return Promise.all(
+            files.map(file => this.request({
+                ...config, url,
+                method: 'PUT',
+                data: file
+            }))
+        );
+    }
+
     copy = (url: string, destination: string, config: WebDAVRequestConfig = {}) =>
         this.request({
             ...config, url,
@@ -52,7 +62,6 @@ export class WebDAV {
         return new Promise<XMLHttpRequest>((resolve, reject) => {
             const r = this.createRequest();
             r.open(config.method, this.defaults.baseURL + config.url);
-
             const headers = { ...this.defaults.headers, ...config.headers };
             Object
                 .keys(headers)
@@ -62,13 +71,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;
@@ -87,4 +110,4 @@ interface RequestConfig {
     headers?: { [key: string]: string };
     data?: any;
     onUploadProgress?: (event: ProgressEvent) => void;
-}
+}
\ No newline at end of file