refs #master Merge branch 'origin/master' into 14186-progress-indicator-store
[arvados-workbench2.git] / src / services / collection-service / collection-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { CollectionResource } from "~/models/collection";
6 import { AxiosInstance } from "axios";
7 import { CollectionFile, CollectionDirectory } from "~/models/collection-file";
8 import { WebDAV } from "~/common/webdav";
9 import { AuthService } from "../auth-service/auth-service";
10 import { mapTreeValues } from "~/models/tree";
11 import { parseFilesResponse } from "./collection-service-files-response";
12 import { fileToArrayBuffer } from "~/common/file";
13 import { TrashableResourceService } from "~/services/common-service/trashable-resource-service";
14 import { ApiActions } from "~/services/api/api-actions";
15
16 export type UploadProgress = (fileId: number, loaded: number, total: number, currentTime: number) => void;
17
18 export class CollectionService extends TrashableResourceService<CollectionResource> {
19     constructor(serverApi: AxiosInstance, private webdavClient: WebDAV, private authService: AuthService, actions: ApiActions) {
20         super(serverApi, "collections", actions);
21     }
22
23     async files(uuid: string) {
24         const request = await this.webdavClient.propfind(`c=${uuid}`);
25         if (request.responseXML != null) {
26             const filesTree = parseFilesResponse(request.responseXML);
27             return mapTreeValues(this.extendFileURL)(filesTree);
28         }
29         return Promise.reject();
30     }
31
32     async deleteFiles(collectionUuid: string, filePaths: string[]) {
33         for (const path of filePaths) {
34             await this.webdavClient.delete(`c=${collectionUuid}${path}`);
35         }
36     }
37
38     async uploadFiles(collectionUuid: string, files: File[], onProgress?: UploadProgress) {
39         // files have to be uploaded sequentially
40         for (let idx = 0; idx < files.length; idx++) {
41             await this.uploadFile(collectionUuid, files[idx], idx, onProgress);
42         }
43     }
44
45     moveFile(collectionUuid: string, oldPath: string, newPath: string) {
46         return this.webdavClient.move(
47             `c=${collectionUuid}${oldPath}`,
48             `c=${collectionUuid}${encodeURI(newPath)}`
49         );
50     }
51
52     private extendFileURL = (file: CollectionDirectory | CollectionFile) => ({
53         ...file,
54         url: this.webdavClient.defaults.baseURL + file.url + '?api_token=' + this.authService.getApiToken()
55     })
56
57     private async uploadFile(collectionUuid: string, file: File, fileId: number, onProgress: UploadProgress = () => { return; }) {
58         const fileURL = `c=${collectionUuid}/${file.name}`;
59         const fileContent = await fileToArrayBuffer(file);
60         const requestConfig = {
61             headers: {
62                 'Content-Type': 'text/octet-stream'
63             },
64             onUploadProgress: (e: ProgressEvent) => {
65                 onProgress(fileId, e.loaded, e.total, Date.now());
66             }
67         };
68         return this.webdavClient.put(fileURL, fileContent, requestConfig);
69
70     }
71 }