3a2094066e4107022eb7a1a27f06eb976b8c5cba
[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 { CommonResourceService } from "~/services/common-service/common-resource-service";
9 import * as _ from "lodash";
10
11 export class CollectionFilesService {
12
13     constructor(private collectionService: CollectionService) { }
14
15     getFiles(collectionUuid: string) {
16         return this.collectionService
17             .get(collectionUuid)
18             .then(collection =>
19                 mapManifestToCollectionFilesTree(
20                     parseKeepManifestText(
21                         collection.manifestText
22                     )
23                 )
24             );
25     }
26
27     async renameFile(collectionUuid: string, file: { name: string, path: string }, newName: string) {
28         const collection = await this.collectionService.get(collectionUuid);
29         const manifest = parseKeepManifestText(collection.manifestText);
30         const updatedManifest = manifest.map(
31             stream => stream.name === file.path
32                 ? {
33                     ...stream,
34                     files: stream.files.map(
35                         f => f.name === file.name
36                             ? { ...f, name: newName }
37                             : f
38                     )
39                 }
40                 : stream
41         );
42         const manifestText = stringifyKeepManifest(updatedManifest);
43         return this.collectionService.update(collectionUuid, { manifestText });
44     }
45
46     async deleteFile(collectionUuid: string, file: { name: string, path: string }) {
47         const collection = await this.collectionService.get(collectionUuid);
48         const manifest = parseKeepManifestText(collection.manifestText);
49         const updatedManifest = manifest.map(stream =>
50             stream.name === file.path
51                 ? {
52                     ...stream,
53                     files: stream.files.filter(f => f.name !== file.name)
54                 }
55                 : stream
56         );
57         const manifestText = stringifyKeepManifest(updatedManifest);
58         return this.collectionService.update(collectionUuid, { manifestText });
59     }
60 }