Download attachments

This commit is contained in:
2022-11-29 11:12:06 -03:00
parent c53eb4c7a6
commit f8500e061c
21 changed files with 242 additions and 27 deletions

View File

@ -7,6 +7,7 @@ use Psr\Http\Message\ServerRequestInterface;
use ProVM\Common\Implement\Controller\Json;
use ProVM\Common\Service\Attachments as Service;
use ProVM\Emails\Model\Attachment;
use Psr\Log\LoggerInterface;
class Attachments
{
@ -51,4 +52,13 @@ class Attachments
}
return $this->withJson($response, $output);
}
public function get(ServerRequestInterface $request, ResponseInterface $response, Service $service, LoggerInterface $logger, int $attachment_id): ResponseInterface
{
$attachment = $service->getRepository()->fetchById($attachment_id);
$response->withHeader('Content-Type', 'application/pdf');
$response->withHeader('Content-Disposition', "'attachment;filename='{$attachment->getFullFilename()}'");
$response->getBody()->write($service->getFile($attachment_id));
return $response;
}
}

View File

@ -3,9 +3,11 @@ namespace ProVM\Common\Service;
use Ddeboer\Imap\Message\AttachmentInterface;
use Ddeboer\Imap\MessageInterface;
use Nyholm\Psr7\Stream;
use ProVM\Common\Exception\Message\NoAttachments;
use ProVM\Emails\Model\Message;
use ProVM\Emails\Repository\Attachment;
use Psr\Http\Message\StreamInterface;
use Psr\Log\LoggerInterface;
use Safe\Exceptions\FilesystemException;
@ -84,6 +86,22 @@ class Attachments extends Base
$remote_message = $this->getMessages()->getRemoteMessage($message->getUID());
return $this->getRemoteService()->get($remote_message, $relative_filename);
}
public function getFile(int $attachment_id): string
{
$attachment = $this->getRepository()->fetchById($attachment_id);
$filename = implode(DIRECTORY_SEPARATOR, [
$this->getFolder(),
$attachment->getFullFilename()
]);
if ($attachment->isDecrypted()) {
$filename = implode(DIRECTORY_SEPARATOR, [
$this->getFolder(),
'decrypted',
$attachment->getFullFilename()
]);
}
return \Safe\file_get_contents($filename);
}
public function getAll(): array
{