Extract file utils
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Mon, 13 Aug 2018 15:04:55 +0000 (17:04 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Mon, 13 Aug 2018 15:04:55 +0000 (17:04 +0200)
Feature #14013

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/common/file.ts [new file with mode: 0644]
src/services/collection-service/collection-service.ts

diff --git a/src/common/file.ts b/src/common/file.ts
new file mode 100644 (file)
index 0000000..3f17a38
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+export const fileToArrayBuffer = (file: File) =>
+    new Promise<ArrayBuffer>(resolve => {
+        const reader = new FileReader();
+        reader.onload = () => {
+            resolve(reader.result as ArrayBuffer);
+        };
+        reader.readAsArrayBuffer(file);
+    });
index 3b98f750ec1f24233478fd07e562d0ae7532ba4c..c611f3668d9637917b6e7f02c2c4b97ef5d6d012 100644 (file)
@@ -10,6 +10,7 @@ import { WebDAV } from "../../common/webdav";
 import { AuthService } from "../auth-service/auth-service";
 import { mapTreeValues } from "../../models/tree";
 import { parseFilesResponse } from "./collection-service-files-response";
+import { fileToArrayBuffer } from "../../common/file";
 
 export type UploadProgress = (fileId: number, loaded: number, total: number, currentTime: number) => void;
 
@@ -42,27 +43,19 @@ export class CollectionService extends CommonResourceService<CollectionResource>
         url: this.webdavClient.defaults.baseURL + file.url + '?api_token=' + this.authService.getApiToken()
     })
 
-    private uploadFile(collectionUuid: string, file: File, fileId: number, onProgress: UploadProgress = () => { return; }) {
-        return this.readFile(file).then(content =>
-            this.webdavClient.put(`/c=${collectionUuid}/${file.name}`, content, {
-                headers: {
-                    'Content-Type': 'text/octet-stream'
-                },
-                onUploadProgress: (e: ProgressEvent) => {
-                    onProgress(fileId, e.loaded, e.total, Date.now());
-                }
-            })
-        );
-    }
+    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'
+            },
+            onUploadProgress: (e: ProgressEvent) => {
+                onProgress(fileId, e.loaded, e.total, Date.now());
+            }
+        };
+        return this.webdavClient.put(fileURL, fileContent, requestConfig);
 
-    private readFile(file: File): Promise<ArrayBuffer> {
-        return new Promise<ArrayBuffer>(resolve => {
-            const reader = new FileReader();
-            reader.onload = () => {
-                resolve(reader.result as ArrayBuffer);
-            };
-            reader.readAsArrayBuffer(file);
-        });
     }
 
 }