dfeed0b7032de2fec5e7b654345113292b4ce39d
[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 import { CommonResourceService } from "../../common/api/common-resource-service";
10 import * as _ from "lodash";
11
12 export class CollectionFilesService {
13
14     constructor(private collectionService: CollectionService) { }
15
16     getFiles(collectionUuid: string) {
17         return this.collectionService
18             .get(collectionUuid)
19             .then(collection =>
20                 mapManifestToCollectionFilesTree(
21                     parseKeepManifestText(
22                         collection.manifestText
23                     )
24                 )
25             );
26     }
27
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
33                 ? {
34                     ...stream,
35                     files: stream.files.map(
36                         f => f.name === file.name
37                             ? { ...f, name: newName }
38                             : f
39                     )
40                 }
41                 : stream
42         );
43         const manifestText = stringifyKeepManifest(updatedManifest);
44         const data = { ...collection, manifestText };
45         return this.collectionService.update(collectionUuid, CommonResourceService.mapKeys(_.snakeCase)(data));
46     }
47
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
53                 ? {
54                     ...stream,
55                     files: stream.files.filter(f => f.name !== file.name)
56                 }
57                 : stream
58         );
59         const manifestText = stringifyKeepManifest(updatedManifest);
60         return this.collectionService.update(collectionUuid, { manifestText });
61     }
62
63     renameTest() {
64         const u = this.renameFile('qr1hi-4zz18-n0sx074erl4p0ph', {
65             name: 'extracted2.txt.png',
66             path: ''
67         }, 'extracted-new.txt.png');
68     }
69 }