17016: Code optimisation
[arvados.git] / src / services / collection-service / collection-service.ts
index 0aa0aa84de4f476866c60cccb68b5dc2acc9bcb9..5ae03b89eb0f816c7f595d322dadd1000d94b8cd 100644 (file)
@@ -36,12 +36,23 @@ export class CollectionService extends TrashableResourceService<CollectionResour
     }
 
     async deleteFiles(collectionUuid: string, filePaths: string[]) {
-        for (const path of filePaths) {
-            const splittedPath = path.split('/');
-            if (collectionUuid) {
-                await this.webdavClient.delete(`c=${collectionUuid}/${splittedPath[1]}`);
-            } else {
+        const sortedUniquePaths = Array.from(new Set(filePaths))
+            .sort((a, b) => a.length - b.length)
+            .reduce((acc, currentPath) => {
+                const parentPathFound = acc.find((parentPath) => currentPath.indexOf(`${parentPath}/`) > -1);
+
+                if (!parentPathFound) {
+                    return [...acc, currentPath];
+                }
+
+                return acc;
+            }, []);
+
+        for (const path of sortedUniquePaths) {
+            if (path.indexOf(collectionUuid) === -1) {
                 await this.webdavClient.delete(`c=${collectionUuid}${path}`);
+            } else {
+                await this.webdavClient.delete(`c=${path}`);
             }
         }
     }