Implement renameFile and deleteFile [WIP]
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Mon, 6 Aug 2018 12:46:28 +0000 (14:46 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Mon, 6 Aug 2018 12:46:28 +0000 (14:46 +0200)
Feature #13967

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/services/collection-files-service/collection-files-service.ts

index 96c9e9905820e927efed0e856d8a27cee3e66703..3b320eb6c9780f7bd407ee01ce9be2902d4d313f 100644 (file)
@@ -3,11 +3,12 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import { CollectionService } from "../collection-service/collection-service";
-import { parseKeepManifestText } from "./collection-manifest-parser";
+import { parseKeepManifestText, stringifyKeepManifest } from "./collection-manifest-parser";
 import { mapManifestToCollectionFilesTree } from "./collection-manifest-mapper";
+import { CollectionFile } from "../../models/collection-file";
 
 export class CollectionFilesService {
-    
+
     constructor(private collectionService: CollectionService) { }
 
     getFiles(collectionUuid: string) {
@@ -22,4 +23,37 @@ export class CollectionFilesService {
             );
     }
 
+    async renameFile(collectionUuid: string, file: { name: string, path: string }, newName: string) {
+        const collection = await this.collectionService.get(collectionUuid);
+        const manifest = parseKeepManifestText(collection.manifestText);
+        const updatedManifest = manifest.map(stream =>
+            stream.name === file.path
+                ? {
+                    ...stream,
+                    files: stream.files.map(f =>
+                        f.name === file.name
+                            ? { ...f, name: newName }
+                            : f)
+                }
+                : stream
+        );
+        const manifestText = stringifyKeepManifest(updatedManifest);
+        return this.collectionService.update(collectionUuid, { ...collection, manifestText });
+    }
+
+    async deleteFile(collectionUuid: string, file: { name: string, path: string }) {
+        const collection = await this.collectionService.get(collectionUuid);
+        const manifest = parseKeepManifestText(collection.manifestText);
+        const updatedManifest = manifest.map(stream =>
+            stream.name === file.path
+                ? {
+                    ...stream,
+                    files: stream.files.filter(f => f.name !== file.name)
+                }
+                : stream
+        );
+        const manifestText = stringifyKeepManifest(updatedManifest);
+        return this.collectionService.update(collectionUuid, { manifestText });
+    }
+
 }
\ No newline at end of file