Merge branch '15027-object-update-fix'
[arvados-workbench2.git] / src / services / collection-files-service / collection-files-service.ts
index 96c9e9905820e927efed0e856d8a27cee3e66703..f8e7de9857c333d78fd08de5dc8425cf75789552 100644 (file)
@@ -3,11 +3,11 @@
 // 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";
 
 export class CollectionFilesService {
-    
+
     constructor(private collectionService: CollectionService) { }
 
     getFiles(collectionUuid: string) {
@@ -22,4 +22,37 @@ export class CollectionFilesService {
             );
     }
 
-}
\ No newline at end of file
+    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, { 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 });
+    }
+}