From 710e08c109cf320334371083a93fda5f5b40a4ea Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 20 Feb 2024 00:11:10 +1300 Subject: [PATCH] Make sure to clear cache if file was downloaded from remote source --- src/Appwrite/Platform/Workers/Messaging.php | 31 +++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index 4f20139a69..8b3ef5e8bd 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -337,6 +337,37 @@ class Messaging extends Action $message->setAttribute('deliveredAt', DateTime::now()); $dbForProject->updateDocument('messages', $message->getId(), $message); + + // Delete any attachments that were downloaded to the local cache + if ($provider->getAttribute('type') === MESSAGE_TYPE_EMAIL) { + if ($deviceFiles->getType() === Storage::DEVICE_LOCAL) { + return; + } + + $data = $message->getAttribute('data'); + $attachments = $data['attachments'] ?? []; + + foreach ($attachments as $attachment) { + $bucketId = $attachment['bucketId']; + $fileId = $attachment['fileId']; + + $bucket = $dbForProject->getDocument('buckets', $bucketId); + if ($bucket->isEmpty()) { + throw new Exception(Exception::STORAGE_BUCKET_NOT_FOUND); + } + + $file = $dbForProject->getDocument('bucket_' . $bucket->getInternalId(), $fileId); + if ($file->isEmpty()) { + throw new Exception(Exception::STORAGE_FILE_NOT_FOUND); + } + + $path = $file->getAttribute('path', ''); + + if ($localCache->exists($path)) { + $localCache->delete($path); + } + } + } } private function processInternalSMSMessage(Document $message, Document $project, array $recipients, Usage $queueForUsage, Log $log): void