Skip to content

Commit

Permalink
fixup! feat(ocs): notify of new messages and provide API endpoint to …
Browse files Browse the repository at this point in the history
…retrieve its contents
  • Loading branch information
miaulalala committed Jul 1, 2024
1 parent 40ff823 commit d798904
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
7 changes: 6 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,14 @@
'verb' => 'GET',
],
[
'name' => 'messageApi#raw',
'name' => 'messageApi#getRaw',
'url' => '/message/{id}/raw',
'verb' => 'GET',
],
[
'name' => 'messageApi#getAttachment',
'url' => '/message/{id}/attachment/{attachmentId}',
'verb' => 'GET',
],
],
];
61 changes: 33 additions & 28 deletions lib/Controller/MessageApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use OCA\Mail\Contracts\IDkimService;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Http\AttachmentDownloadResponse;
use OCA\Mail\Http\TrapError;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCA\Mail\Model\SmimeData;
Expand All @@ -28,8 +27,6 @@
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\OCSController;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IMimeTypeDetector;
Expand Down Expand Up @@ -129,7 +126,7 @@ public function get(int $id): DataResponse {
#[BruteForceProtection('mailGetRawMessage')]
#[NoAdminRequired]
#[NoCSRFRequired]
public function raw(int $id): DataResponse {
public function getRaw(int $id): DataResponse {
try {
$message = $this->mailManager->getMessage($this->userId, $id);
$mailbox = $this->mailManager->getMailbox($this->userId, $message->getMailboxId());
Expand Down Expand Up @@ -164,50 +161,58 @@ public function raw(int $id): DataResponse {
* @return array
*/
private function enrichDownloadUrl(int $id, array $attachment) {
$downloadUrl = $this->urlGenerator->linkToRoute('mail.messageApi.downloadAttachment',
$downloadUrl = $this->urlGenerator->linkToOCSRouteAbsolute('mail.messageApi.downloadAttachment',
[
'id' => $id,
'attachmentId' => $attachment['id'],
]);
$downloadUrl = $this->urlGenerator->getAbsoluteURL($downloadUrl);
$attachment['downloadUrl'] = $downloadUrl;
return $attachment;
}

#[NoCSRFRequired]
#[NoAdminRequired]
#[TrapError]
public function downloadAttachment(int $id,
string $attachmentId): Response {
public function getAttachment(int $id,
string $attachmentId): DataResponse {
try {
$message = $this->mailManager->getMessage($this->userId, $id);
$mailbox = $this->mailManager->getMailbox($this->userId, $message->getMailboxId());
$account = $this->accountService->find($this->userId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse($e, Http::STATUS_FORBIDDEN);
} catch (DoesNotExistException | ClientException $e) {
return new DataResponse($e, Http::STATUS_FORBIDDEN);
}

$attachment = $this->mailManager->getMailAttachment(
$account,
$mailbox,
$message,
$attachmentId,
);
try {
$attachment = $this->mailManager->getMailAttachment(
$account,
$mailbox,
$message,
$attachmentId,
);
} catch (\Horde_Imap_Client_Exception_NoSupportExtension | \Horde_Imap_Client_Exception | \Horde_Mime_Exception $e) {
$this->logger->error('Error when trying to process the attachment', ['exception' => $e]);
return new DataResponse($e, Http::STATUS_INTERNAL_SERVER_ERROR);
} catch (ServiceException | DoesNotExistException $e) {
$this->logger->error('Could not find attachment', ['exception' => $e]);
return new DataResponse($e, Http::STATUS_NOT_FOUND);
}

// Body party and embedded messages do not have a name
if ($attachment->getName() === null) {
return new AttachmentDownloadResponse(
$attachment->getContent(),
$this->l10n->t('Embedded message %s', [
$attachmentId,
]) . '.eml',
$attachment->getType()
);
return new DataResponse([
'name' => $attachmentId . '.eml',
'mime' => $attachment->getType(),
'size' => $attachment->getSize(),
'content' => $attachment->getContent()
]);
}
return new AttachmentDownloadResponse(
$attachment->getContent(),
$attachment->getName(),
$attachment->getType()
);

return new DataResponse([
'name' => $attachment->getName(),
'mime' => $attachment->getType(),
'size' => $attachment->getSize(),
'content' => $attachment->getContent()
]);
}
}

0 comments on commit d798904

Please sign in to comment.