refs #master Merge branch 'origin/master' into 13828-trash-view
[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 { CommonResourceService } from "~/common/api/common-resource-service";
6 import { CollectionResource } from "~/models/collection";
7 import { AxiosInstance } from "axios";
8 import { CollectionFile, CollectionDirectory } from "~/models/collection-file";
9 import { WebDAV } from "~/common/webdav";
10 import { AuthService } from "../auth-service/auth-service";
11 import { mapTreeValues } from "~/models/tree";
12 import { parseFilesResponse } from "./collection-service-files-response";
13 import { fileToArrayBuffer } from "~/common/file";
14
15 export type UploadProgress = (fileId: number, loaded: number, total: number, currentTime: number) => void;
16
17 export class CollectionService extends CommonResourceService<CollectionResource> {
18     constructor(serverApi: AxiosInstance, private webdavClient: WebDAV, private authService: AuthService) {
19         super(serverApi, "collections");
20     }
21
22     async files(uuid: string) {
23         const request = await this.webdavClient.propfind(`c=${uuid}`);
24         if (request.responseXML != null) {
25             const filesTree = parseFilesResponse(request.responseXML);
26             return mapTreeValues(this.extendFileURL)(filesTree);
27         }
28         return Promise.reject();
29     }
30
31     async deleteFiles(collectionUuid: string, filePaths: string[]) {
32         for (const path of filePaths) {
33             await this.webdavClient.delete(`c=${collectionUuid}${path}`);
34         }
35     }
36
37     async uploadFiles(collectionUuid: string, files: File[], onProgress?: UploadProgress) {
38         // files have to be uploaded sequentially
39         for (let idx = 0; idx < files.length; idx++) {
40             await this.uploadFile(collectionUuid, files[idx], idx, onProgress);
41         }
42     }
43
44     moveFile(collectionUuid: string, oldPath: string, newPath: string) {
45         return this.webdavClient.move(
46             `c=${collectionUuid}${oldPath}`,
47             `c=${collectionUuid}${encodeURI(newPath)}`
48         );
49     }
50
51     private extendFileURL = (file: CollectionDirectory | CollectionFile) => ({
52         ...file,
53         url: this.webdavClient.defaults.baseURL + file.url + '?api_token=' + this.authService.getApiToken()
54     })
55
56     private async uploadFile(collectionUuid: string, file: File, fileId: number, onProgress: UploadProgress = () => { return; }) {
57         const fileURL = `c=${collectionUuid}/${file.name}`;
58         const fileContent = await fileToArrayBuffer(file);
59         const requestConfig = {
60             headers: {
61                 'Content-Type': 'text/octet-stream'
62             },
63             onUploadProgress: (e: ProgressEvent) => {
64                 onProgress(fileId, e.loaded, e.total, Date.now());
65             }
66         };
67         return this.webdavClient.put(fileURL, fileContent, requestConfig);
68
69     }
70
71     trash(uuid: string): Promise<CollectionResource> {
72         return this.serverApi
73             .post(this.resourceType + `${uuid}/trash`)
74             .then(CommonResourceService.mapResponseKeys);
75     }
76
77     untrash(uuid: string): Promise<CollectionResource> {
78         const params = {
79             ensure_unique_name: true
80         };
81         return this.serverApi
82             .post(this.resourceType + `${uuid}/untrash`, {
83                 params: CommonResourceService.mapKeys(_.snakeCase)(params)
84             })
85             .then(CommonResourceService.mapResponseKeys);
86     }
87     
88 }