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 { 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";
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 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);
28 return Promise.reject();
31 async deleteFiles(collectionUuid: string, filePaths: string[]) {
32 for (const path of filePaths) {
33 const splittedPath = path.split('/');
35 await this.webdavClient.delete(`c=${collectionUuid}/${splittedPath[1]}`);
37 await this.webdavClient.delete(`c=${collectionUuid}${path}`);
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);
49 moveFile(collectionUuid: string, oldPath: string, newPath: string) {
50 return this.webdavClient.move(
51 `c=${collectionUuid}${oldPath}`,
52 `c=${collectionUuid}${encodeURI(newPath)}`
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]}`;
71 private async uploadFile(collectionUuid: string, file: File, fileId: number, onProgress: UploadProgress = () => { return; }) {
72 const fileURL = `c=${collectionUuid}/${file.name}`;
73 const requestConfig = {
75 'Content-Type': 'text/octet-stream'
77 onUploadProgress: (e: ProgressEvent) => {
78 onProgress(fileId, e.loaded, e.total, Date.now());
81 return this.webdavClient.upload(fileURL, [file], requestConfig);