1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { CollectionService } from "../collection-service/collection-service";
6 import { parseKeepManifestText, stringifyKeepManifest } from "./collection-manifest-parser";
7 import { mapManifestToCollectionFilesTree } from "./collection-manifest-mapper";
8 import { CollectionFile } from "../../models/collection-file";
9 import { CommonResourceService } from "../../common/api/common-resource-service";
10 import * as _ from "lodash";
12 export class CollectionFilesService {
14 constructor(private collectionService: CollectionService) { }
16 getFiles(collectionUuid: string) {
17 return this.collectionService
20 mapManifestToCollectionFilesTree(
21 parseKeepManifestText(
22 collection.manifestText
28 async renameFile(collectionUuid: string, file: { name: string, path: string }, newName: string) {
29 const collection = await this.collectionService.get(collectionUuid);
30 const manifest = parseKeepManifestText(collection.manifestText);
31 const updatedManifest = manifest.map(
32 stream => stream.name === file.path
35 files: stream.files.map(
36 f => f.name === file.name
37 ? { ...f, name: newName }
43 const manifestText = stringifyKeepManifest(updatedManifest);
44 const data = { ...collection, manifestText };
45 return this.collectionService.update(collectionUuid, CommonResourceService.mapKeys(_.snakeCase)(data));
48 async deleteFile(collectionUuid: string, file: { name: string, path: string }) {
49 const collection = await this.collectionService.get(collectionUuid);
50 const manifest = parseKeepManifestText(collection.manifestText);
51 const updatedManifest = manifest.map(stream =>
52 stream.name === file.path
55 files: stream.files.filter(f => f.name !== file.name)
59 const manifestText = stringifyKeepManifest(updatedManifest);
60 return this.collectionService.update(collectionUuid, { manifestText });
64 const u = this.renameFile('qr1hi-4zz18-n0sx074erl4p0ph', {
65 name: 'extracted2.txt.png',
67 }, 'extracted-new.txt.png');