77ad5d385f199ecae7c7c9290b0d6cb03f0482b5
[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, defaultCollectionSelectedFields } 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 { extractFilesData } from "./collection-service-files-response";
11 import { TrashableResourceService } from "services/common-service/trashable-resource-service";
12 import { ApiActions } from "services/api/api-actions";
13 import { customEncodeURI } from "common/url";
14 import { Session } from "models/session";
15 import { CommonService } from "services/common-service/common-service";
16
17 export type UploadProgress = (fileId: number, loaded: number, total: number, currentTime: number) => void;
18
19 export class CollectionService extends TrashableResourceService<CollectionResource> {
20     constructor(serverApi: AxiosInstance, private webdavClient: WebDAV, private authService: AuthService, actions: ApiActions) {
21         super(serverApi, "collections", actions, [
22             'fileCount',
23             'fileSizeTotal',
24             'replicationConfirmed',
25             'replicationConfirmedAt',
26             'storageClassesConfirmed',
27             'storageClassesConfirmedAt',
28             'unsignedManifestText',
29             'version',
30         ]);
31     }
32
33     async get(uuid: string, showErrors?: boolean, select?: string[], session?: Session) {
34         super.validateUuid(uuid);
35         const selectParam = select || defaultCollectionSelectedFields;
36         return super.get(uuid, showErrors, selectParam, session);
37     }
38
39     create(data?: Partial<CollectionResource>, showErrors?: boolean) {
40         return super.create({ ...data, preserveVersion: true }, showErrors);
41     }
42
43     update(uuid: string, data: Partial<CollectionResource>, showErrors?: boolean) {
44         const select = [...Object.keys(data), 'version', 'modifiedAt'];
45         return super.update(uuid, { ...data, preserveVersion: true }, showErrors, select);
46     }
47
48     async files(uuid: string) {
49         const request = await this.webdavClient.propfind(`c=${uuid}`);
50         if (request.responseXML != null) {
51             return extractFilesData(request.responseXML);
52         }
53         return Promise.reject();
54     }
55
56     async deleteFiles(collectionUuid: string, filePaths: string[]) {
57         const sortedUniquePaths = Array.from(new Set(filePaths))
58             .sort((a, b) => a.length - b.length)
59             .reduce((acc, currentPath) => {
60                 const parentPathFound = acc.find((parentPath) => currentPath.indexOf(`${parentPath}/`) > -1);
61
62                 if (!parentPathFound) {
63                     return [...acc, currentPath];
64                 }
65
66                 return acc;
67             }, []);
68
69         for (const path of sortedUniquePaths) {
70             if (path.indexOf(collectionUuid) === -1) {
71                 await this.webdavClient.delete(`c=${collectionUuid}${path}`);
72             } else {
73                 await this.webdavClient.delete(`c=${path}`);
74             }
75         }
76         await this.update(collectionUuid, { preserveVersion: true });
77     }
78
79     async uploadFiles(collectionUuid: string, files: File[], onProgress?: UploadProgress, targetLocation: string = '') {
80         if (collectionUuid === "" || files.length === 0) { return; }
81         // files have to be uploaded sequentially
82         for (let idx = 0; idx < files.length; idx++) {
83             await this.uploadFile(collectionUuid, files[idx], idx, onProgress, targetLocation);
84         }
85         await this.update(collectionUuid, { preserveVersion: true });
86     }
87
88     async moveFile(collectionUuid: string, oldPath: string, newPath: string) {
89         await this.webdavClient.move(
90             `c=${collectionUuid}${oldPath}`,
91             `c=${collectionUuid}/${customEncodeURI(newPath)}`
92         );
93         await this.update(collectionUuid, { preserveVersion: true });
94     }
95
96     extendFileURL = (file: CollectionDirectory | CollectionFile) => {
97         const baseUrl = this.webdavClient.getBaseUrl().endsWith('/')
98             ? this.webdavClient.getBaseUrl().slice(0, -1)
99             : this.webdavClient.getBaseUrl();
100         const apiToken = this.authService.getApiToken();
101         const encodedApiToken = apiToken ? encodeURI(apiToken) : '';
102         const userApiToken = `/t=${encodedApiToken}/`;
103         const splittedPrevFileUrl = file.url.split('/');
104         const url = `${baseUrl}/${splittedPrevFileUrl[1]}${userApiToken}${splittedPrevFileUrl.slice(2).join('/')}`;
105         return {
106             ...file,
107             url
108         };
109     }
110
111     async getFileContents(file: CollectionFile) {
112         return (await this.webdavClient.get(`c=${file.id}`)).response;
113     }
114
115     private async uploadFile(collectionUuid: string, file: File, fileId: number, onProgress: UploadProgress = () => { return; }, targetLocation: string = '') {
116         const fileURL = `c=${targetLocation !== '' ? targetLocation : collectionUuid}/${file.name}`.replace('//', '/');
117         const requestConfig = {
118             headers: {
119                 'Content-Type': 'text/octet-stream'
120             },
121             onUploadProgress: (e: ProgressEvent) => {
122                 onProgress(fileId, e.loaded, e.total, Date.now());
123             },
124         };
125         return this.webdavClient.upload(fileURL, [file], requestConfig);
126     }
127
128     batchFileDelete(collectionUuid: string, files: string[], showErrors?: boolean) {
129         const payload = {
130             collection: {
131                 preserve_version: true
132             },
133             replace_files: files.reduce((obj, filePath) => {
134                 const pathStart = filePath.startsWith('/') ? '' : '/';
135                 return {
136                     ...obj,
137                     [`${pathStart}${filePath}`]: ''
138                 }
139             }, {})
140         };
141
142         return CommonService.defaultResponse(
143             this.serverApi
144                 .put<CollectionResource>(`/${this.resourceType}/${collectionUuid}`, payload),
145             this.actions,
146             true, // mapKeys
147             showErrors
148         );
149     }
150
151     batchFileCopy(sourcePdh: string, files: string[], destinationCollectionUuid: string, destinationCollectionPath: string, showErrors?: boolean) {
152         const pathStart = destinationCollectionPath.startsWith('/') ? '' : '/';
153         const separator = destinationCollectionPath.endsWith('/') ? '' : '/';
154         const destinationPath = `${pathStart}${destinationCollectionPath}${separator}`;
155         const payload = {
156             collection: {
157                 preserve_version: true
158             },
159             replace_files: files.reduce((obj, sourceFile) => {
160                 const sourcePath = sourceFile.startsWith('/') ? sourceFile : `/${sourceFile}`;
161                 return {
162                     ...obj,
163                     [`${destinationPath}${sourceFile.split('/').slice(-1)}`]: `${sourcePdh}${sourcePath}`
164                 };
165             }, {})
166         };
167
168         return CommonService.defaultResponse(
169             this.serverApi
170                 .put<CollectionResource>(`/${this.resourceType}/${destinationCollectionUuid}`, payload),
171             this.actions,
172             true, // mapKeys
173             showErrors
174         );
175     }
176
177     batchFileMove(sourceUuid: string, sourcePdh: string, files: string[], destinationCollectionUuid: string, destinationPath: string, showErrors?: boolean) {
178         return this.batchFileCopy(sourcePdh, files, destinationCollectionUuid, destinationPath, showErrors)
179             .then(() => {
180                 return this.batchFileDelete(sourceUuid, files, showErrors);
181             });
182     }
183
184     createDirectory(collectionUuid: string, path: string) {
185         return this.webdavClient.mkdir(`c=${collectionUuid}/${customEncodeURI(path)}`);
186     }
187
188 }