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
{

View File

@ -8,6 +8,6 @@ Monolog\ErrorHandler::register($app->getContainer()->get(Psr\Log\LoggerInterface
try {
$app->run();
} catch (Error | Exception $e) {
$app->getContainer()->get(\Psr\Log\LoggerInterface::class)->error($e);
$app->getContainer()->get(Psr\Log\LoggerInterface::class)->error($e);
throw $e;
}

View File

@ -5,4 +5,7 @@ $app->group('/attachments', function($app) {
$app->put('/grab', [Attachments::class, 'grab']);
$app->post('/decrypt', [Attachments::class, 'decrypt']);
$app->get('[/]', Attachments::class);
});
$app->group('/attachment/{attachment_id}', function($app) {
$app->get('[/]', [Attachments::class, 'get']);
});

View File

@ -6,4 +6,7 @@ $app->group('/messages', function($app) {
$app->put('/grab', [Messages::class, 'grab']);
$app->put('/schedule', [Jobs::class, 'schedule']);
$app->get('/pending', [Jobs::class, 'pending']);
});
$app->group('/message/{message_id}', function($app) {
$app->get('[/]', [Messages::class, 'get']);
});

View File

@ -2,21 +2,21 @@
use Psr\Container\ContainerInterface;
return [
\Monolog\Handler\DeduplicationHandler::class => function(ContainerInterface $container) {
return new \Monolog\Handler\DeduplicationHandler($container->get(\Monolog\Handler\RotatingFileHandler::class));
Monolog\Handler\DeduplicationHandler::class => function(ContainerInterface $container) {
return new Monolog\Handler\DeduplicationHandler($container->get(Monolog\Handler\RotatingFileHandler::class));
},
\Monolog\Handler\RotatingFileHandler::class => function(ContainerInterface $container) {
$handler = new \Monolog\Handler\RotatingFileHandler($container->get('log_file'));
$handler->setFormatter($container->get(\Monolog\Formatter\SyslogFormatter::class));
Monolog\Handler\RotatingFileHandler::class => function(ContainerInterface $container) {
$handler = new Monolog\Handler\RotatingFileHandler($container->get('log_file'));
$handler->setFormatter($container->get(Monolog\Formatter\SyslogFormatter::class));
return $handler;
},
\Psr\Log\LoggerInterface::class => function(ContainerInterface $container) {
$logger = new \Monolog\Logger('file_logger');
$logger->pushHandler($container->get(\Monolog\Handler\DeduplicationHandler::class));
//$logger->pushHandler($container->get(\Monolog\Handler\RotatingFileHandler::class));
$logger->pushProcessor($container->get(\Monolog\Processor\PsrLogMessageProcessor::class));
$logger->pushProcessor($container->get(\Monolog\Processor\IntrospectionProcessor::class));
$logger->pushProcessor($container->get(\Monolog\Processor\MemoryUsageProcessor::class));
Psr\Log\LoggerInterface::class => function(ContainerInterface $container) {
$logger = new Monolog\Logger('file_logger');
$logger->pushHandler($container->get(Monolog\Handler\DeduplicationHandler::class));
//$logger->pushHandler($container->get(Monolog\Handler\RotatingFileHandler::class));
$logger->pushProcessor($container->get(Monolog\Processor\PsrLogMessageProcessor::class));
$logger->pushProcessor($container->get(Monolog\Processor\IntrospectionProcessor::class));
$logger->pushProcessor($container->get(Monolog\Processor\MemoryUsageProcessor::class));
return $logger;
}
];

View File

@ -163,6 +163,7 @@ class Attachment implements Model
'date_time' => $this->getMessage()->getDateTime()->format('Y-m-d H:i:s')
],
'filename' => $this->getFilename(),
'fullname' => $this->getFullFilename(),
'downloaded' => $this->isDownloaded(),
'encrypted' => $this->isEncrypted(),
'decrypted' => $this->isDecrypted()