X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/50031c8c6aae1f325f81d71a6f5a8c1cb293aa46..54e859c204c3952a8eaf96d2145dfa96c199b934:/src/services/collection-files-service/collection-files-service.ts diff --git a/src/services/collection-files-service/collection-files-service.ts b/src/services/collection-files-service/collection-files-service.ts index fd17dc4f..6f88a729 100644 --- a/src/services/collection-files-service/collection-files-service.ts +++ b/src/services/collection-files-service/collection-files-service.ts @@ -3,7 +3,66 @@ // SPDX-License-Identifier: AGPL-3.0 import { CollectionService } from "../collection-service/collection-service"; +import { parseKeepManifestText, stringifyKeepManifest } from "./collection-manifest-parser"; +import { mapManifestToCollectionFilesTree } from "./collection-manifest-mapper"; +import { CommonResourceService } from "~/services/common-service/common-resource-service"; +import * as _ from "lodash"; export class CollectionFilesService { - constructor(private collectionService: CollectionService){} -} \ No newline at end of file + + constructor(private collectionService: CollectionService) { } + + getFiles(collectionUuid: string) { + return this.collectionService + .get(collectionUuid) + .then(collection => + mapManifestToCollectionFilesTree( + parseKeepManifestText( + collection.manifestText + ) + ) + ); + } + + async renameFile(collectionUuid: string, file: { name: string, path: string }, newName: string) { + const collection = await this.collectionService.get(collectionUuid); + const manifest = parseKeepManifestText(collection.manifestText); + const updatedManifest = manifest.map( + stream => stream.name === file.path + ? { + ...stream, + files: stream.files.map( + f => f.name === file.name + ? { ...f, name: newName } + : f + ) + } + : stream + ); + const manifestText = stringifyKeepManifest(updatedManifest); + const data = { ...collection, manifestText }; + return this.collectionService.update(collectionUuid, CommonResourceService.mapKeys(_.snakeCase)(data)); + } + + async deleteFile(collectionUuid: string, file: { name: string, path: string }) { + const collection = await this.collectionService.get(collectionUuid); + const manifest = parseKeepManifestText(collection.manifestText); + const updatedManifest = manifest.map(stream => + stream.name === file.path + ? { + ...stream, + files: stream.files.filter(f => f.name !== file.name) + } + : stream + ); + const manifestText = stringifyKeepManifest(updatedManifest); + return this.collectionService.update(collectionUuid, { manifestText }); + } + + renameTest() { + const u = this.renameFile('qr1hi-4zz18-n0sx074erl4p0ph', { + name: 'extracted2.txt.png', + path: '' + }, 'extracted-new.txt.png'); + } +}