15027: Cleans up unused imports.
[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
9 export class CollectionFilesService {
10
11     constructor(private collectionService: CollectionService) { }
12
13     getFiles(collectionUuid: string) {
14         return this.collectionService
15             .get(collectionUuid)
16             .then(collection =>
17                 mapManifestToCollectionFilesTree(
18                     parseKeepManifestText(
19                         collection.manifestText
20                     )
21                 )
22             );
23     }
24
25     async renameFile(collectionUuid: string, file: { name: string, path: string }, newName: string) {
26         const collection = await this.collectionService.get(collectionUuid);
27         const manifest = parseKeepManifestText(collection.manifestText);
28         const updatedManifest = manifest.map(
29             stream => stream.name === file.path
30                 ? {
31                     ...stream,
32                     files: stream.files.map(
33                         f => f.name === file.name
34                             ? { ...f, name: newName }
35                             : f
36                     )
37                 }
38                 : stream
39         );
40         const manifestText = stringifyKeepManifest(updatedManifest);
41         return this.collectionService.update(collectionUuid, { 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 }