CR fixes II
[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 import * as _ from 'lodash';
15
16 export type UploadProgress = (fileId: number, loaded: number, total: number, currentTime: number) => void;
17
18 export class CollectionService extends CommonResourceService<CollectionResource> {
19     constructor(serverApi: AxiosInstance, private webdavClient: WebDAV, private authService: AuthService) {
20         super(serverApi, "collections");
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
72     trash(uuid: string): Promise<CollectionResource> {
73         return this.serverApi
74             .post(this.resourceType + `${uuid}/trash`)
75             .then(CommonResourceService.mapResponseKeys);
76     }
77
78     untrash(uuid: string): Promise<CollectionResource> {
79         const params = {
80             ensure_unique_name: true
81         };
82         return this.serverApi
83             .post(this.resourceType + `${uuid}/untrash`, {
84                 params: CommonResourceService.mapKeys(_.snakeCase)(params)
85             })
86             .then(CommonResourceService.mapResponseKeys);
87     }
88
89 }