diff --git a/lib/Controller/MessagesController.php b/lib/Controller/MessagesController.php index 022964f703..f588a6e41d 100755 --- a/lib/Controller/MessagesController.php +++ b/lib/Controller/MessagesController.php @@ -841,6 +841,37 @@ public function downloadAttachments(int $id): Response { return $zip; } + private function moveAttachmentToFiles(Attachment $attachment, string $targetPath, ?string $fileName): void { + if ($this->userFolder === null) { + return; + } + + if ($fileName === null) { + $fileName = ''; + } else { + $fileName = trim($fileName); + } + + if ($fileName === '') { + $fileName = $attachment->getName() ?? $this->l10n->t('Embedded message %s', [ + $attachment->getId(), + ]) . '.eml'; + } + + $fileParts = pathinfo($fileName); + $fileName = $fileParts['filename']; + $fileExtension = $fileParts['extension'] ?? ''; + $fullPath = "$targetPath/$fileName.$fileExtension"; + $counter = 2; + while ($this->userFolder->nodeExists($fullPath)) { + $fullPath = "$targetPath/$fileName ($counter).$fileExtension"; + $counter++; + } + + $newFile = $this->userFolder->newFile($fullPath); + $newFile->putContent($attachment->getContent()); + } + /** * @NoAdminRequired * @@ -858,7 +889,8 @@ public function downloadAttachments(int $id): Response { #[TrapError] public function saveAttachment(int $id, string $attachmentId, - string $targetPath) { + string $targetPath, + ?string $fileName) { if ($this->userId === null) { return new JSONResponse([], Http::STATUS_UNAUTHORIZED); } @@ -880,40 +912,27 @@ public function saveAttachment(int $id, return new JSONResponse([], Http::STATUS_FORBIDDEN); } - /** @var Attachment[] $attachments */ - $attachments = []; if ($attachmentId === '0') { $attachments = $this->mailManager->getMailAttachments( $account, $mailbox, $message, ); + + foreach ($attachments as $attachment) { + $this->moveAttachmentToFiles($attachment, $targetPath, null); + } } else { - $attachments[] = $this->mailManager->getMailAttachment( + $attachment = $this->mailManager->getMailAttachment( $account, $mailbox, $message, $attachmentId, ); - } - foreach ($attachments as $attachment) { - $fileName = $attachment->getName() ?? $this->l10n->t('Embedded message %s', [ - $attachment->getId(), - ]) . '.eml'; - $fileParts = pathinfo($fileName); - $fileName = $fileParts['filename']; - $fileExtension = $fileParts['extension'] ?? ''; - $fullPath = "$targetPath/$fileName.$fileExtension"; - $counter = 2; - while ($this->userFolder->nodeExists($fullPath)) { - $fullPath = "$targetPath/$fileName ($counter).$fileExtension"; - $counter++; - } - - $newFile = $this->userFolder->newFile($fullPath); - $newFile->putContent($attachment->getContent()); + $this->moveAttachmentToFiles($attachment, $targetPath, $fileName); } + return new JSONResponse(); } diff --git a/src/components/MessageAttachment.vue b/src/components/MessageAttachment.vue index e0679e0337..ec3fafdd82 100644 --- a/src/components/MessageAttachment.vue +++ b/src/components/MessageAttachment.vue @@ -19,6 +19,7 @@ {{ humanReadable(size) }} + + + + + +