Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View eml files in mail #9078

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
57 changes: 51 additions & 6 deletions lib/Controller/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,6 +91,7 @@
private IDkimService $dkimService;
private IUserPreferences $preferences;
private SnoozeService $snoozeService;
private ConversionService $conversionService;

Check failure on line 94 in lib/Controller/MessagesController.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

UndefinedClass

lib/Controller/MessagesController.php:94:10: UndefinedClass: Class, interface or enum named OCA\EmailViewer\Service\ConversionService does not exist (see https://psalm.dev/019)

Check failure on line 94 in lib/Controller/MessagesController.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable27

UndefinedClass

lib/Controller/MessagesController.php:94:10: UndefinedClass: Class, interface or enum named OCA\EmailViewer\Service\ConversionService does not exist (see https://psalm.dev/019)

Check failure on line 94 in lib/Controller/MessagesController.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable26

UndefinedClass

lib/Controller/MessagesController.php:94:10: UndefinedClass: Class, interface or enum named OCA\EmailViewer\Service\ConversionService does not exist (see https://psalm.dev/019)

public function __construct(string $appName,
IRequest $request,
Expand All @@ -110,7 +112,8 @@
IMAPClientFactory $clientFactory,
IDkimService $dkimService,
IUserPreferences $preferences,
SnoozeService $snoozeService) {
SnoozeService $snoozeService,
ConversionService $conversionService) {

Check failure on line 116 in lib/Controller/MessagesController.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

UndefinedClass

lib/Controller/MessagesController.php:116:3: UndefinedClass: Class, interface or enum named OCA\EmailViewer\Service\ConversionService does not exist (see https://psalm.dev/019)

Check failure on line 116 in lib/Controller/MessagesController.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable27

UndefinedClass

lib/Controller/MessagesController.php:116:3: UndefinedClass: Class, interface or enum named OCA\EmailViewer\Service\ConversionService does not exist (see https://psalm.dev/019)

Check failure on line 116 in lib/Controller/MessagesController.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable26

UndefinedClass

lib/Controller/MessagesController.php:116:3: UndefinedClass: Class, interface or enum named OCA\EmailViewer\Service\ConversionService does not exist (see https://psalm.dev/019)
parent::__construct($appName, $request);
$this->accountService = $accountService;
$this->mailManager = $mailManager;
Expand All @@ -130,6 +133,7 @@
$this->dkimService = $dkimService;
$this->preferences = $preferences;
$this->snoozeService = $snoozeService;
$this->conversionService = $conversionService;
}

/**
Expand Down Expand Up @@ -293,7 +297,40 @@
$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);

Check failure on line 326 in lib/Controller/MessagesController.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

UndefinedClass

lib/Controller/MessagesController.php:326:11: UndefinedClass: Class, interface or enum named OCA\EmailViewer\Service\ConversionService does not exist (see https://psalm.dev/019)

Check failure on line 326 in lib/Controller/MessagesController.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable27

UndefinedClass

lib/Controller/MessagesController.php:326:11: UndefinedClass: Class, interface or enum named OCA\EmailViewer\Service\ConversionService does not exist (see https://psalm.dev/019)

Check failure on line 326 in lib/Controller/MessagesController.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable26

UndefinedClass

lib/Controller/MessagesController.php:326:11: UndefinedClass: Class, interface or enum named OCA\EmailViewer\Service\ConversionService does not exist (see https://psalm.dev/019)
$content = file_get_contents($path);
return new AttachmentDownloadResponse(
$content,
'attachment.pdf',
'application/pdf'
);
}
/**
* @NoAdminRequired
* @param int $id
Expand Down Expand Up @@ -895,11 +932,19 @@
*/
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']);
Expand Down
2 changes: 1 addition & 1 deletion src/components/MessageAttachments.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading