1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
17 export type UploadProgress = (fileId: number, loaded: number, total: number, currentTime: number) => void;
19 export class CollectionService extends TrashableResourceService<CollectionResource> {
20 constructor(serverApi: AxiosInstance, private webdavClient: WebDAV, private authService: AuthService, actions: ApiActions) {
21 super(serverApi, "collections", actions, [
24 'replicationConfirmed',
25 'replicationConfirmedAt',
26 'storageClassesConfirmed',
27 'storageClassesConfirmedAt',
28 'unsignedManifestText',
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);
39 create(data?: Partial<CollectionResource>, showErrors?: boolean) {
40 return super.create({ ...data, preserveVersion: true }, showErrors);
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);
48 async files(uuid: string) {
49 const request = await this.webdavClient.propfind(`c=${uuid}`);
50 if (request.responseXML != null) {
51 return extractFilesData(request.responseXML);
53 return Promise.reject();
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);
62 if (!parentPathFound) {
63 return [...acc, currentPath];
69 for (const path of sortedUniquePaths) {
70 if (path.indexOf(collectionUuid) === -1) {
71 await this.webdavClient.delete(`c=${collectionUuid}${path}`);
73 await this.webdavClient.delete(`c=${path}`);
76 await this.update(collectionUuid, { preserveVersion: true });
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);
85 await this.update(collectionUuid, { preserveVersion: true });
88 async moveFile(collectionUuid: string, oldPath: string, newPath: string) {
89 await this.webdavClient.move(
90 `c=${collectionUuid}${oldPath}`,
91 `c=${collectionUuid}/${customEncodeURI(newPath)}`
93 await this.update(collectionUuid, { preserveVersion: true });
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('/')}`;
111 async getFileContents(file: CollectionFile) {
112 return (await this.webdavClient.get(`c=${file.id}`)).response;
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 = {
119 'Content-Type': 'text/octet-stream'
121 onUploadProgress: (e: ProgressEvent) => {
122 onProgress(fileId, e.loaded, e.total, Date.now());
125 return this.webdavClient.upload(fileURL, [file], requestConfig);
128 batchFileDelete(collectionUuid: string, files: string[], showErrors?: boolean) {
131 preserve_version: true
133 replace_files: files.reduce((obj, filePath) => {
134 const pathStart = filePath.startsWith('/') ? '' : '/';
137 [`${pathStart}${filePath}`]: ''
142 return CommonService.defaultResponse(
144 .put<CollectionResource>(`/${this.resourceType}/${collectionUuid}`, payload),
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}`;
157 preserve_version: true
159 replace_files: files.reduce((obj, sourceFile) => {
160 const sourcePath = sourceFile.startsWith('/') ? sourceFile : `/${sourceFile}`;
163 [`${destinationPath}${sourceFile.split('/').slice(-1)}`]: `${sourcePdh}${sourcePath}`
168 return CommonService.defaultResponse(
170 .put<CollectionResource>(`/${this.resourceType}/${destinationCollectionUuid}`, payload),
177 batchFileMove(sourceUuid: string, sourcePdh: string, files: string[], destinationCollectionUuid: string, destinationPath: string, showErrors?: boolean) {
178 return this.batchFileCopy(sourcePdh, files, destinationCollectionUuid, destinationPath, showErrors)
180 return this.batchFileDelete(sourceUuid, files, showErrors);
184 createDirectory(collectionUuid: string, path: string) {
185 return this.webdavClient.mkdir(`c=${collectionUuid}/${customEncodeURI(path)}`);