From 478f90131a297e0da3a526269e6261c3815cd9e0 Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Thu, 8 Aug 2024 20:36:44 +0200 Subject: [PATCH] Fix deleting files when deleting an Attachment --- lib/Controller/AttachmentsController.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/Controller/AttachmentsController.php b/lib/Controller/AttachmentsController.php index aafb68a4..ca12d30d 100644 --- a/lib/Controller/AttachmentsController.php +++ b/lib/Controller/AttachmentsController.php @@ -365,14 +365,22 @@ public function update(string|int $id, ObjectService $objectService, ElasticSear public function destroy(string|int $id, ObjectService $objectService, ElasticSearchService $elasticSearchService): JSONResponse { $attachment = $this->show(id: $id, objectService: $objectService)->getData(); - if ($this->config->hasKey(app: $this->appName, key: 'mongoStorage') === false || $this->config->getValueString(app: $this->appName, key: 'mongoStorage') !== '1' ) { $attachment = $attachment->jsonSerialize(); + } - // Todo: are we sure this is the best way to do this (how do we save the full path to this file in nextCloud) - $this->fileService->deleteFile(filePath: 'Attachments/' . $attachment['title'] . '.' . $attachment['extension']); + // Todo: are we sure this is the best way to do this (how do we save the full path to this file in nextCloud) +// $this->fileService->deleteFile(filePath: 'Attachments/' . $attachment['title'] . '.' . $attachment['extension']); + $filePath = explode(separator: '/', string: $attachment['reference']); + array_shift(array: $filePath); + $filePath = implode(separator: '/', array: $filePath); + $this->fileService->deleteFile(filePath: $filePath); + + if ($this->config->hasKey(app: $this->appName, key: 'mongoStorage') === false + || $this->config->getValueString(app: $this->appName, key: 'mongoStorage') !== '1' + ) { $this->attachmentMapper->delete(entity: $this->attachmentMapper->find(id: (int) $id)); return new JSONResponse([]); @@ -382,9 +390,6 @@ public function destroy(string|int $id, ObjectService $objectService, ElasticSea $dbConfig['headers']['api-key'] = $this->config->getValueString(app: $this->appName, key: 'mongodbKey'); $dbConfig['mongodbCluster'] = $this->config->getValueString(app: $this->appName, key: 'mongodbCluster'); - // Todo: are we sure this is the best way to do this (how do we save the full path to this file in nextCloud) - $this->fileService->deleteFile(filePath: 'Attachments/' . $attachment['title'] . '.' . $attachment['extension']); - $filters['_id'] = (string) $id; $returnData = $objectService->deleteObject( filters: $filters,