Skip to content

Commit

Permalink
Fix deleting files when deleting an Attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoLouwerse committed Aug 8, 2024
1 parent 8432d8c commit 478f901
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/Controller/AttachmentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
Expand All @@ -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,
Expand Down

0 comments on commit 478f901

Please sign in to comment.