3b320eb6c9780f7bd407ee01ce9be2902d4d313f
[arvados-workbench2.git] / src / services / collection-files-service / collection-files-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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
10 export class CollectionFilesService {
11
12     constructor(private collectionService: CollectionService) { }
13
14     getFiles(collectionUuid: string) {
15         return this.collectionService
16             .get(collectionUuid)
17             .then(collection =>
18                 mapManifestToCollectionFilesTree(
19                     parseKeepManifestText(
20                         collection.manifestText
21                     )
22                 )
23             );
24     }
25
26     async renameFile(collectionUuid: string, file: { name: string, path: string }, newName: string) {
27         const collection = await this.collectionService.get(collectionUuid);
28         const manifest = parseKeepManifestText(collection.manifestText);
29         const updatedManifest = manifest.map(stream =>
30             stream.name === file.path
31                 ? {
32                     ...stream,
33                     files: stream.files.map(f =>
34                         f.name === file.name
35                             ? { ...f, name: newName }
36                             : f)
37                 }
38                 : stream
39         );
40         const manifestText = stringifyKeepManifest(updatedManifest);
41         return this.collectionService.update(collectionUuid, { ...collection, manifestText });
42     }
43
44     async deleteFile(collectionUuid: string, file: { name: string, path: string }) {
45         const collection = await this.collectionService.get(collectionUuid);
46         const manifest = parseKeepManifestText(collection.manifestText);
47         const updatedManifest = manifest.map(stream =>
48             stream.name === file.path
49                 ? {
50                     ...stream,
51                     files: stream.files.filter(f => f.name !== file.name)
52                 }
53                 : stream
54         );
55         const manifestText = stringifyKeepManifest(updatedManifest);
56         return this.collectionService.update(collectionUuid, { manifestText });
57     }
58
59 }