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 { Session } from "models/session";
14 import { CommonService } from "services/common-service/common-service";
15 import { snakeCase } from "lodash";
16 import { CommonResourceServiceError } from "services/common-service/common-resource-service";
18 export type UploadProgress = (fileId: number, loaded: number, total: number, currentTime: number) => void;
19 type CollectionPartialUpdateOrCreate =
20 | (Partial<CollectionResource> & Pick<CollectionResource, "uuid">)
21 | (Partial<CollectionResource> & Pick<CollectionResource, "ownerUuid">);
23 export const emptyCollectionPdh = "d41d8cd98f00b204e9800998ecf8427e+0";
24 export const SOURCE_DESTINATION_EQUAL_ERROR_MESSAGE = "Source and destination cannot be the same";
26 export class CollectionService extends TrashableResourceService<CollectionResource> {
27 constructor(serverApi: AxiosInstance, private keepWebdavClient: WebDAV, private authService: AuthService, actions: ApiActions) {
28 super(serverApi, "collections", actions, [
31 "replicationConfirmed",
32 "replicationConfirmedAt",
33 "storageClassesConfirmed",
34 "storageClassesConfirmedAt",
35 "unsignedManifestText",
40 async get(uuid: string, showErrors?: boolean, select?: string[], session?: Session) {
41 super.validateUuid(uuid);
42 const selectParam = select || defaultCollectionSelectedFields;
43 return super.get(uuid, showErrors, selectParam, session);
46 create(data?: Partial<CollectionResource>, showErrors?: boolean) {
47 return super.create({ ...data, preserveVersion: true }, showErrors);
50 update(uuid: string, data: Partial<CollectionResource>, showErrors?: boolean) {
51 const select = [...Object.keys(data), "version", "modifiedAt"];
52 return super.update(uuid, { ...data, preserveVersion: true }, showErrors, select);
55 async files(uuid: string) {
57 const request = await this.keepWebdavClient.propfind(`c=${uuid}`);
58 if (request.responseXML != null) {
59 return extractFilesData(request.responseXML);
62 return Promise.reject(e);
64 return Promise.reject();
67 private combineFilePath(parts: string[]) {
68 return parts.reduce((path, part) => {
69 // Trim leading and trailing slashes
70 const trimmedPart = part.split("/").filter(Boolean).join("/");
71 if (trimmedPart.length) {
72 const separator = path.endsWith("/") ? "" : "/";
73 return `${path}${separator}${trimmedPart}`;
80 private replaceFiles(data: CollectionPartialUpdateOrCreate, fileMap: {}, showErrors?: boolean) {
83 preserve_version: true,
84 ...CommonService.mapKeys(snakeCase)(data),
85 // Don't send uuid in payload when creating
88 replace_files: fileMap,
91 return CommonService.defaultResponse(
92 this.serverApi.put<CollectionResource>(`/${this.resourceType}/${data.uuid}`, payload),
98 return CommonService.defaultResponse(
99 this.serverApi.post<CollectionResource>(`/${this.resourceType}`, payload),
107 async uploadFiles(collectionUuid: string, files: File[], onProgress?: UploadProgress, targetLocation: string = "") {
108 if (collectionUuid === "" || files.length === 0) {
111 // files have to be uploaded sequentially
112 for (let idx = 0; idx < files.length; idx++) {
113 await this.uploadFile(collectionUuid, files[idx], idx, onProgress, targetLocation);
115 await this.update(collectionUuid, { preserveVersion: true });
118 async renameFile(collectionUuid: string, collectionPdh: string, oldPath: string, newPath: string) {
119 return this.replaceFiles(
120 { uuid: collectionUuid },
122 [this.combineFilePath([newPath])]: `${collectionPdh}${this.combineFilePath([oldPath])}`,
123 [this.combineFilePath([oldPath])]: "",
128 extendFileURL = (file: CollectionDirectory | CollectionFile) => {
129 const baseUrl = this.keepWebdavClient.getBaseUrl().endsWith("/")
130 ? this.keepWebdavClient.getBaseUrl().slice(0, -1)
131 : this.keepWebdavClient.getBaseUrl();
132 const apiToken = this.authService.getApiToken();
133 const encodedApiToken = apiToken ? encodeURI(apiToken) : "";
134 const userApiToken = `/t=${encodedApiToken}/`;
135 const splittedPrevFileUrl = file.url.split("/");
136 const url = `${baseUrl}/${splittedPrevFileUrl[1]}${userApiToken}${splittedPrevFileUrl.slice(2).join("/")}`;
143 async getFileContents(file: CollectionFile) {
144 return (await this.keepWebdavClient.get(`c=${file.id}`)).response;
147 private async uploadFile(
148 collectionUuid: string,
151 onProgress: UploadProgress = () => {
154 targetLocation: string = ""
156 const fileURL = `c=${targetLocation !== "" ? targetLocation : collectionUuid}/${file.name}`.replace("//", "/");
157 const requestConfig = {
159 "Content-Type": "text/octet-stream",
161 onUploadProgress: (e: ProgressEvent) => {
162 onProgress(fileId, e.loaded, e.total, Date.now());
165 return this.keepWebdavClient.upload(fileURL, [file], requestConfig);
168 deleteFiles(collectionUuid: string, files: string[], showErrors?: boolean) {
169 const optimizedFiles = files
170 .sort((a, b) => a.length - b.length)
171 .reduce((acc, currentPath) => {
172 const parentPathFound = acc.find(parentPath => currentPath.indexOf(`${parentPath}/`) > -1);
174 if (!parentPathFound) {
175 return [...acc, currentPath];
181 const fileMap = optimizedFiles.reduce((obj, filePath) => {
184 [this.combineFilePath([filePath])]: "",
188 return this.replaceFiles({ uuid: collectionUuid }, fileMap, showErrors);
194 destinationCollection: CollectionPartialUpdateOrCreate,
195 destinationPath: string,
198 const fileMap = files.reduce((obj, sourceFile) => {
199 const fileBasename = sourceFile.split("/").filter(Boolean).slice(-1).join("");
202 [this.combineFilePath([destinationPath, fileBasename])]: `${sourcePdh}${this.combineFilePath([sourceFile])}`,
206 return this.replaceFiles(destinationCollection, fileMap, showErrors);
213 destinationCollection: CollectionPartialUpdateOrCreate,
214 destinationPath: string,
217 if (sourceUuid === destinationCollection.uuid) {
218 let errors: CommonResourceServiceError[] = [];
219 const fileMap = files.reduce((obj, sourceFile) => {
220 const fileBasename = sourceFile.split("/").filter(Boolean).slice(-1).join("");
221 const fileDestinationPath = this.combineFilePath([destinationPath, fileBasename]);
222 const fileSourcePath = this.combineFilePath([sourceFile]);
223 const fileSourceUri = `${sourcePdh}${fileSourcePath}`;
225 if (fileDestinationPath !== fileSourcePath) {
228 [fileDestinationPath]: fileSourceUri,
229 [fileSourcePath]: "",
232 errors.push(CommonResourceServiceError.SOURCE_DESTINATION_CANNOT_BE_SAME);
237 if (errors.length === 0) {
238 return this.replaceFiles({ uuid: sourceUuid }, fileMap, showErrors);
240 return Promise.reject({ errors });
243 return this.copyFiles(sourcePdh, files, destinationCollection, destinationPath, showErrors).then(() => {
244 return this.deleteFiles(sourceUuid, files, showErrors);
249 createDirectory(collectionUuid: string, path: string, showErrors?: boolean) {
250 const fileMap = { [this.combineFilePath([path])]: emptyCollectionPdh };
252 return this.replaceFiles({ uuid: collectionUuid }, fileMap, showErrors);