Replace loading -> upload with streamed upload, fix reporting error on file remove
authorDaniel Kos <daniel.kos@contractors.roche.com>
Mon, 19 Nov 2018 23:20:48 +0000 (00:20 +0100)
committerDaniel Kos <daniel.kos@contractors.roche.com>
Mon, 19 Nov 2018 23:20:48 +0000 (00:20 +0100)
Bug #14310

Arvados-DCO-1.1-Signed-off-by: Daniel Kos <daniel.kos@contractors.roche.com>

src/common/webdav.ts
src/services/collection-service/collection-service.ts
src/store/collection-panel/collection-panel-files/collection-panel-files-actions.ts
src/store/collections/collection-upload-actions.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;
index 00ba85478bedcac9665a64087b913e3ef3a11e5d..b0d5cb1445db854e8d88d43e10c95a6b59e80569 100644 (file)
@@ -62,7 +62,6 @@ export class CollectionService extends TrashableResourceService<CollectionResour
 
     private async uploadFile(collectionUuid: string, file: File, fileId: number, onProgress: UploadProgress = () => { return; }) {
         const fileURL = `c=${collectionUuid}/${file.name}`;
-        const fileContent = await fileToArrayBuffer(file);
         const requestConfig = {
             headers: {
                 'Content-Type': 'text/octet-stream'
@@ -71,8 +70,7 @@ export class CollectionService extends TrashableResourceService<CollectionResour
                 onProgress(fileId, e.loaded, e.total, Date.now());
             }
         };
-        return this.webdavClient.put(fileURL, fileContent, requestConfig);
-
+        return this.webdavClient.upload(fileURL, '', [file], requestConfig);
     }
 
     update(uuid: string, data: Partial<CollectionResource>) {
index 4764d436e022e532b3d3d1b09039bec23e72940c..6ee5ca0d2fd4e502196c9541eabddd453a845dbf 100644 (file)
@@ -7,7 +7,7 @@ import { Dispatch } from "redux";
 import { CollectionFilesTree, CollectionFileType } from "~/models/collection-file";
 import { ServiceRepository } from "~/services/services";
 import { RootState } from "../../store";
-import { snackbarActions } from "../../snackbar/snackbar-actions";
+import { snackbarActions, SnackbarKind } from "../../snackbar/snackbar-actions";
 import { dialogActions } from '../../dialog/dialog-actions';
 import { getNodeValue } from "~/models/tree";
 import { filterCollectionFilesBySelection } from './collection-panel-files-state';
@@ -37,10 +37,18 @@ export const removeCollectionFiles = (filePaths: string[]) =>
     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
         const currentCollection = getState().collectionPanel.item;
         if (currentCollection) {
-            dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
-            await services.collectionService.deleteFiles(currentCollection.uuid, filePaths);
-            dispatch<any>(loadCollectionFiles(currentCollection.uuid));
-            dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000 }));
+            dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing...' }));
+            try {
+                await services.collectionService.deleteFiles(currentCollection.uuid, filePaths);
+                dispatch<any>(loadCollectionFiles(currentCollection.uuid));
+                dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000 }));
+            } catch (e) {
+                dispatch(snackbarActions.OPEN_SNACKBAR({
+                    message: 'Could not remove file.',
+                    hideDuration: 2000,
+                    kind: SnackbarKind.ERROR
+                }));
+            }
         }
     };
 
index c410cf04920f77c32a187657ac768865e9426a5e..cf8c37c890f6d24c79de79c37ab776f690ca6f61 100644 (file)
@@ -52,7 +52,7 @@ export const submitCollectionFiles = () =>
                     hideDuration: 2000,
                     kind: SnackbarKind.ERROR
                 }));
-                dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPLOAD_FILES_DIALOG));                
+                dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPLOAD_FILES_DIALOG));
             }
         }
     };