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 { fileToArrayBuffer } from "~/common/file";
13 import { TrashableResourceService } from "~/services/common-service/trashable-resource-service";
14 import { ApiActions } from "~/services/api/api-actions";
15 import { snakeCase } from 'lodash';
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 async files(uuid: string) {
25 const request = await this.webdavClient.propfind(`c=${uuid}`);
26 if (request.responseXML != null) {
27 const filesTree = parseFilesResponse(request.responseXML);
28 return mapTreeValues(this.extendFileURL)(filesTree);
30 return Promise.reject();
33 async deleteFiles(collectionUuid: string, filePaths: string[]) {
34 for (const path of filePaths) {
35 await this.webdavClient.delete(`c=${collectionUuid}${path}`);
39 async uploadFiles(collectionUuid: string, files: File[], onProgress?: UploadProgress) {
40 // files have to be uploaded sequentially
41 for (let idx = 0; idx < files.length; idx++) {
42 await this.uploadFile(collectionUuid, files[idx], idx, onProgress);
46 moveFile(collectionUuid: string, oldPath: string, newPath: string) {
47 return this.webdavClient.move(
48 `c=${collectionUuid}${oldPath}`,
49 `c=${collectionUuid}${encodeURI(newPath)}`
53 private extendFileURL = (file: CollectionDirectory | CollectionFile) => {
54 const baseUrl = this.webdavClient.defaults.baseURL.endsWith('/')
55 ? this.webdavClient.defaults.baseURL.slice(0, -1)
56 : this.webdavClient.defaults.baseURL;
59 url: baseUrl + file.url + '?api_token=' + this.authService.getApiToken()
63 private async uploadFile(collectionUuid: string, file: File, fileId: number, onProgress: UploadProgress = () => { return; }) {
64 const fileURL = `c=${collectionUuid}/${file.name}`;
65 const requestConfig = {
67 'Content-Type': 'text/octet-stream'
69 onUploadProgress: (e: ProgressEvent) => {
70 onProgress(fileId, e.loaded, e.total, Date.now());
73 return this.webdavClient.upload(fileURL, '', [file], requestConfig);
76 update(uuid: string, data: Partial<CollectionResource>) {
77 if (uuid && data && data.properties) {
78 const { properties } = data;
80 ...TrashableResourceService.mapKeys(snakeCase)(data),
83 return TrashableResourceService
86 .put<CollectionResource>(this.resourceType + uuid, mappedData),
91 return TrashableResourceService
94 .put<CollectionResource>(this.resourceType + uuid, data && TrashableResourceService.mapKeys(snakeCase)(data)),