64 lines
2.4 KiB
PHP
64 lines
2.4 KiB
PHP
<?php
|
|
namespace ProVM\Common\Controller;
|
|
|
|
use ProVM\Common\Exception\Request\MissingArgument;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
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
|
|
{
|
|
use Json;
|
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
|
{
|
|
$attachments = array_map(function(Attachment $attachment) {
|
|
return $attachment->toArray();
|
|
},$service->getAll());
|
|
$output = [
|
|
'total' => count($attachments),
|
|
'attachments' => $attachments
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function grab(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Jobs $jobsService): ResponseInterface
|
|
{
|
|
$body = $request->getBody();
|
|
$json = \Safe\json_decode($body->getContents());
|
|
if (!isset($json->messages)) {
|
|
throw new MissingArgument('messages', 'array', 'message UIDs');
|
|
}
|
|
$output = [
|
|
'messages' => $json->messages,
|
|
'total' => count($json->messages),
|
|
'saved' => [
|
|
'attachments' => [],
|
|
'total' => 0
|
|
]
|
|
];
|
|
foreach ($json->messages as $message_id) {
|
|
if (!$jobsService->isPending($message_id)) {
|
|
continue;
|
|
}
|
|
if ($service->grab($message_id)) {
|
|
$job = $jobsService->find($message_id);
|
|
$jobsService->execute($job->getId());
|
|
$output['saved']['attachments'] []= $job->toArray();
|
|
$output['saved']['total'] ++;
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
} |