X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/9e4b7889a99ff2f76d8029aef3a85c4620178ba3..6653b7a7edbac351f5b3734114d6b5904dda1acc:/src/services/collection-service/collection-service.ts diff --git a/src/services/collection-service/collection-service.ts b/src/services/collection-service/collection-service.ts index 77ea7ea3..28de14f5 100644 --- a/src/services/collection-service/collection-service.ts +++ b/src/services/collection-service/collection-service.ts @@ -2,58 +2,70 @@ // // SPDX-License-Identifier: AGPL-3.0 -import { serverApi } from "../../common/api/server-api"; -import { Dispatch } from "redux"; -import actions from "../../store/collection/collection-action"; -import UrlBuilder from "../../common/api/url-builder"; -import FilterBuilder, { FilterField } from "../../common/api/filter-builder"; -import { ArvadosResource } from "../response"; -import { Collection } from "../../models/collection"; - -interface CollectionResource extends ArvadosResource { - name: string; - description: string; - properties: any; - portable_data_hash: string; - manifest_text: string; - replication_desired: number; - replication_confirmed: number; - replication_confirmed_at: string; - trash_at: string; - delete_at: string; - is_trashed: boolean; -} +import { CollectionResource } from "~/models/collection"; +import { AxiosInstance } from "axios"; +import { CollectionFile, CollectionDirectory } from "~/models/collection-file"; +import { WebDAV } from "~/common/webdav"; +import { AuthService } from "../auth-service/auth-service"; +import { mapTreeValues } from "~/models/tree"; +import { parseFilesResponse } from "./collection-service-files-response"; +import { fileToArrayBuffer } from "~/common/file"; +import { TrashableResourceService } from "~/services/common-service/trashable-resource-service"; +import { ApiActions } from "~/services/api/api-actions"; -interface CollectionsResponse { - offset: number; - limit: number; - items: CollectionResource[]; -} +export type UploadProgress = (fileId: number, loaded: number, total: number, currentTime: number) => void; + +export class CollectionService extends TrashableResourceService { + constructor(serverApi: AxiosInstance, private webdavClient: WebDAV, private authService: AuthService, actions: ApiActions) { + super(serverApi, "collections", actions); + } + + async files(uuid: string) { + const request = await this.webdavClient.propfind(`c=${uuid}`); + if (request.responseXML != null) { + const filesTree = parseFilesResponse(request.responseXML); + return mapTreeValues(this.extendFileURL)(filesTree); + } + return Promise.reject(); + } + + async deleteFiles(collectionUuid: string, filePaths: string[]) { + for (const path of filePaths) { + await this.webdavClient.delete(`c=${collectionUuid}${path}`); + } + } -export default class CollectionService { - public getCollectionList = (parentUuid?: string) => (dispatch: Dispatch): Promise => { - dispatch(actions.COLLECTIONS_REQUEST()); - if (parentUuid) { - const fb = new FilterBuilder(); - fb.addLike(FilterField.OWNER_UUID, parentUuid); - return serverApi.get('/collections', { params: { - filters: fb.get() - }}).then(resp => { - const collections = resp.data.items.map(g => ({ - name: g.name, - createdAt: g.created_at, - modifiedAt: g.modified_at, - href: g.href, - uuid: g.uuid, - ownerUuid: g.owner_uuid, - kind: g.kind - } as Collection)); - dispatch(actions.COLLECTIONS_SUCCESS({collections})); - return collections; - }); - } else { - dispatch(actions.COLLECTIONS_SUCCESS({collections: []})); - return Promise.resolve([]); + async uploadFiles(collectionUuid: string, files: File[], onProgress?: UploadProgress) { + // files have to be uploaded sequentially + for (let idx = 0; idx < files.length; idx++) { + await this.uploadFile(collectionUuid, files[idx], idx, onProgress); } } + + moveFile(collectionUuid: string, oldPath: string, newPath: string) { + return this.webdavClient.move( + `c=${collectionUuid}${oldPath}`, + `c=${collectionUuid}${encodeURI(newPath)}` + ); + } + + private extendFileURL = (file: CollectionDirectory | CollectionFile) => ({ + ...file, + url: this.webdavClient.defaults.baseURL + file.url + '?api_token=' + this.authService.getApiToken() + }) + + private async uploadFile(collectionUuid: string, file: File, fileId: number, onProgress: UploadProgress = () => { return; }) { + const fileURL = `c=${collectionUuid}/${file.name}`; + const fileContent = await fileToArrayBuffer(file); + const requestConfig = { + headers: { + 'Content-Type': 'text/octet-stream' + }, + onUploadProgress: (e: ProgressEvent) => { + onProgress(fileId, e.loaded, e.total, Date.now()); + } + }; + return this.webdavClient.put(fileURL, fileContent, requestConfig); + + } }