15027: Cleans up unused imports.
[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 { TrashableResourceService } from "~/services/common-service/trashable-resource-service";
13 import { ApiActions } from "~/services/api/api-actions";
14
15 export type UploadProgress = (fileId: number, loaded: number, total: number, currentTime: number) => void;
16
17 export class CollectionService extends TrashableResourceService<CollectionResource> {
18     constructor(serverApi: AxiosInstance, private webdavClient: WebDAV, private authService: AuthService, actions: ApiActions) {
19         super(serverApi, "collections", actions);
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             const splittedPath = path.split('/');
34             if (collectionUuid) {
35                 await this.webdavClient.delete(`c=${collectionUuid}/${splittedPath[1]}`);
36             } else {
37                 await this.webdavClient.delete(`c=${collectionUuid}${path}`);
38             }
39         }
40     }
41
42     async uploadFiles(collectionUuid: string, files: File[], onProgress?: UploadProgress) {
43         // files have to be uploaded sequentially
44         for (let idx = 0; idx < files.length; idx++) {
45             await this.uploadFile(collectionUuid, files[idx], idx, onProgress);
46         }
47     }
48
49     moveFile(collectionUuid: string, oldPath: string, newPath: string) {
50         return this.webdavClient.move(
51             `c=${collectionUuid}${oldPath}`,
52             `c=${collectionUuid}${encodeURI(newPath)}`
53         );
54     }
55
56     private extendFileURL = (file: CollectionDirectory | CollectionFile) => {
57         const baseUrl = this.webdavClient.defaults.baseURL.endsWith('/')
58             ? this.webdavClient.defaults.baseURL.slice(0, -1)
59             : this.webdavClient.defaults.baseURL;
60         const apiToken = this.authService.getApiToken();
61         const splittedApiToken = apiToken ? apiToken.split('/') : [];
62         const userApiToken = `/t=${splittedApiToken[2]}/`;
63         const splittedPrevFileUrl = file.url.split('/');
64         const url = `${baseUrl}/${splittedPrevFileUrl[1]}${userApiToken}${splittedPrevFileUrl[2]}`;
65         return {
66             ...file,
67             url
68         };
69     }
70
71     private async uploadFile(collectionUuid: string, file: File, fileId: number, onProgress: UploadProgress = () => { return; }) {
72         const fileURL = `c=${collectionUuid}/${file.name}`;
73         const requestConfig = {
74             headers: {
75                 'Content-Type': 'text/octet-stream'
76             },
77             onUploadProgress: (e: ProgressEvent) => {
78                 onProgress(fileId, e.loaded, e.total, Date.now());
79             }
80         };
81         return this.webdavClient.upload(fileURL, [file], requestConfig);
82     }
83 }