From fe50f71d6b5492be83634276c977fcb299e98b0b Mon Sep 17 00:00:00 2001 From: Jonathan Treffler Date: Tue, 5 Nov 2024 13:45:14 +0100 Subject: [PATCH] implemented proper resource deletion: remove folder resources in filesystem; remove child resources first --- lib/Service/ResourceService.php | 45 ++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/Service/ResourceService.php b/lib/Service/ResourceService.php index 4e77650..d3779b3 100644 --- a/lib/Service/ResourceService.php +++ b/lib/Service/ResourceService.php @@ -271,14 +271,51 @@ class ResourceService { public function getFolderResourceFilesystemNode(FolderResource $resource) { return $this->pathManager->getOrganizationFolderByIdSubfolder($resource->getOrganizationFolderId(), $this->getResourcePath($resource)); } + /** + * get all direct sub-resources + */ + public function getSubResources(Resource $resource) { + return $this->findAll($resource->getOrganizationFolderId(), $resource->getId()); + } - public function delete(int $id): Resource { + /** + * get all sub-resources recursively + */ + public function getAllSubResources(Resource $resource) { + $subResources = $this->getSubResources($resource); + + foreach($subResources as $subResource) { + $subResources = array_merge($subResources, $this->getAllSubResources($subResource)); + } + + return $subResources; + } + + public function deleteById(int $id): Resource { try { $resource = $this->mapper->find($id); - $this->mapper->delete($resource); - return $resource; + return $this->delete($resource); } catch (Exception $e) { - $this->handleException($e, $id); + $this->handleException($e, $resource->getId()); } } + + public function delete(Resource $resource): Resource { + // first delete all subresources recursively + $subResources = $this->getSubResources($resource); + + foreach($subResources as $subResource) { + $this->delete($subResource); + } + + // delete in filesystem if type folder + if($resource->getType() === "folder") { + $this->getFolderResourceFilesystemNode($resource)->delete(); + } + + // delete in database + $this->mapper->delete($resource); + return $resource; + + } }