1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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 { 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";
15 export type UploadProgress = (fileId: number, loaded: number, total: number, currentTime: number) => void;
17 export class CollectionService extends TrashableResourceService<CollectionResource> {
18 constructor(serverApi: AxiosInstance, private webdavClient: WebDAV, private authService: AuthService, actions: ApiActions) {
19 super(serverApi, "collections", actions, [
22 'replicationConfirmed',
23 'replicationConfirmedAt',
24 'storageClassesConfirmed',
25 'storageClassesConfirmedAt',
26 'unsignedManifestText',
31 create(data?: Partial<CollectionResource>) {
32 return super.create({ ...data, preserveVersion: true });
35 update(uuid: string, data: Partial<CollectionResource>) {
36 return super.update(uuid, { ...data, preserveVersion: true });
39 async files(uuid: string) {
40 const request = await this.webdavClient.propfind(`c=${uuid}`);
41 if (request.responseXML != null) {
42 return extractFilesData(request.responseXML);
44 return Promise.reject();
47 async deleteFiles(collectionUuid: string, filePaths: string[]) {
48 const sortedUniquePaths = Array.from(new Set(filePaths))
49 .sort((a, b) => a.length - b.length)
50 .reduce((acc, currentPath) => {
51 const parentPathFound = acc.find((parentPath) => currentPath.indexOf(`${parentPath}/`) > -1);
53 if (!parentPathFound) {
54 return [...acc, currentPath];
60 for (const path of sortedUniquePaths) {
61 if (path.indexOf(collectionUuid) === -1) {
62 await this.webdavClient.delete(`c=${collectionUuid}${path}`);
64 await this.webdavClient.delete(`c=${path}`);
67 await this.update(collectionUuid, { preserveVersion: true });
70 async uploadFiles(collectionUuid: string, files: File[], onProgress?: UploadProgress) {
71 if (collectionUuid === "" || files.length === 0) { return; }
72 // files have to be uploaded sequentially
73 for (let idx = 0; idx < files.length; idx++) {
74 await this.uploadFile(collectionUuid, files[idx], idx, onProgress);
76 await this.update(collectionUuid, { preserveVersion: true });
79 async moveFile(collectionUuid: string, oldPath: string, newPath: string) {
80 await this.webdavClient.move(
81 `c=${collectionUuid}${oldPath}`,
82 `c=${collectionUuid}/${customEncodeURI(newPath)}`
84 await this.update(collectionUuid, { preserveVersion: true });
87 extendFileURL = (file: CollectionDirectory | CollectionFile) => {
88 const baseUrl = this.webdavClient.defaults.baseURL.endsWith('/')
89 ? this.webdavClient.defaults.baseURL.slice(0, -1)
90 : this.webdavClient.defaults.baseURL;
91 const apiToken = this.authService.getApiToken();
92 const encodedApiToken = apiToken ? encodeURI(apiToken) : '';
93 const userApiToken = `/t=${encodedApiToken}/`;
94 const splittedPrevFileUrl = file.url.split('/');
95 const url = `${baseUrl}/${splittedPrevFileUrl[1]}${userApiToken}${splittedPrevFileUrl.slice(2).join('/')}`;
102 private async uploadFile(collectionUuid: string, file: File, fileId: number, onProgress: UploadProgress = () => { return; }) {
103 const fileURL = `c=${collectionUuid}/${file.name}`;
104 const requestConfig = {
106 'Content-Type': 'text/octet-stream'
108 onUploadProgress: (e: ProgressEvent) => {
109 onProgress(fileId, e.loaded, e.total, Date.now());
112 return this.webdavClient.upload(fileURL, [file], requestConfig);