From 7f6dbfe8fe8912c0668d9f87901e0a5c6420b406 Mon Sep 17 00:00:00 2001 From: hamza221 Date: Tue, 21 Nov 2023 19:12:13 +0100 Subject: [PATCH] View emf files in mail Signed-off-by: hamza221 --- appinfo/routes.php | 5 +++ lib/Controller/MessagesController.php | 57 ++++++++++++++++++++++++--- src/components/MessageAttachments.vue | 2 +- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 688279f9bf..a7f37e4643 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -200,6 +200,11 @@ 'url' => '/api/messages/{id}/attachment/{attachmentId}', 'verb' => 'GET' ], + [ + 'name' => 'messages#getPdf', + 'url' => '/api/messages/{id}/attachment/{attachmentId}/convert', + 'verb' => 'GET' + ], [ 'name' => 'messages#downloadAttachments', 'url' => '/api/messages/{id}/attachments', diff --git a/lib/Controller/MessagesController.php b/lib/Controller/MessagesController.php index ef82a9db62..c93749aed0 100755 --- a/lib/Controller/MessagesController.php +++ b/lib/Controller/MessagesController.php @@ -33,6 +33,7 @@ use Exception; use OC\Security\CSP\ContentSecurityPolicyNonceManager; +use OCA\EmailViewer\Service\ConversionService; use OCA\Mail\Attachment; use OCA\Mail\Contracts\IDkimService; use OCA\Mail\Contracts\IMailManager; @@ -90,6 +91,7 @@ class MessagesController extends Controller { private IDkimService $dkimService; private IUserPreferences $preferences; private SnoozeService $snoozeService; + private ConversionService $conversionService; public function __construct(string $appName, IRequest $request, @@ -110,7 +112,8 @@ public function __construct(string $appName, IMAPClientFactory $clientFactory, IDkimService $dkimService, IUserPreferences $preferences, - SnoozeService $snoozeService) { + SnoozeService $snoozeService, + ConversionService $conversionService) { parent::__construct($appName, $request); $this->accountService = $accountService; $this->mailManager = $mailManager; @@ -130,6 +133,7 @@ public function __construct(string $appName, $this->dkimService = $dkimService; $this->preferences = $preferences; $this->snoozeService = $snoozeService; + $this->conversionService = $conversionService; } /** @@ -293,7 +297,40 @@ public function getItineraries(int $id): JSONResponse { $response->cacheFor(24 * 60 * 60, false, true); return $response; } + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function getPdf(int $id, string $attachmentId): Response { + try { + $message = $this->mailManager->getMessage($this->currentUserId, $id); + $mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId()); + $account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId()); + } catch (DoesNotExistException $e) { + return new JSONResponse([], Http::STATUS_FORBIDDEN); + } + $attachment = $this->mailManager->getMailAttachment( + $account, + $mailbox, + $message, + $attachmentId, + ); + $uid = uniqid('', true); + $tmpPath = sys_get_temp_dir() . "/eml2pdfSource_$uid.pdf"; // Create a temp file with .pdf extension + + $handle = fopen($tmpPath, 'w'); // Open the file for writing + fwrite($handle, $attachment->getContent()); // Write some content + fclose($handle); // Close the file handle + + $path = $this->conversionService->convert($tmpPath); + $content = file_get_contents($path); + return new AttachmentDownloadResponse( + $content, + 'attachment.pdf', + 'application/pdf' + ); + } /** * @NoAdminRequired * @param int $id @@ -895,11 +932,19 @@ public function destroy(int $id): JSONResponse { */ private function enrichDownloadUrl(int $id, array $attachment) { - $downloadUrl = $this->urlGenerator->linkToRoute('mail.messages.downloadAttachment', - [ - 'id' => $id, - 'attachmentId' => $attachment['id'], - ]); + if($attachment['mime'] === "message/rfc822") { + $downloadUrl = $this->urlGenerator->linkToRoute('mail.messages.getPdf', + [ + 'id' => $id, + 'attachmentId' => $attachment['id'], + ]); + } else { + $downloadUrl = $this->urlGenerator->linkToRoute('mail.messages.downloadAttachment', + [ + 'id' => $id, + 'attachmentId' => $attachment['id'], + ]); + } $downloadUrl = $this->urlGenerator->getAbsoluteURL($downloadUrl); $attachment['downloadUrl'] = $downloadUrl; $attachment['mimeUrl'] = $this->mimeTypeDetector->mimeTypeIcon($attachment['mime']); diff --git a/src/components/MessageAttachments.vue b/src/components/MessageAttachments.vue index 988982e385..97b33a6247 100644 --- a/src/components/MessageAttachments.vue +++ b/src/components/MessageAttachments.vue @@ -119,7 +119,7 @@ export default { filename: attachment.downloadUrl, source: attachment.downloadUrl, basename: basename(attachment.downloadUrl), - mime: attachment.mime, + mime: attachment.mime === 'message/rfc822' ? 'application/pdf' : attachment.mime, etag: 'fixme', hasPreview: false, fileid: parseInt(attachment.id, 10),