60 lines
2.3 KiB
PHP
60 lines
2.3 KiB
PHP
|
<?php
|
||
|
namespace ProVM\Common\Controller;
|
||
|
|
||
|
use Psr\Http\Message\ResponseInterface;
|
||
|
use Psr\Http\Message\ServerRequestInterface;
|
||
|
use function Safe\json_decode;
|
||
|
use ProVM\Common\Implement\Controller\Json;
|
||
|
use ProVM\Common\Service\Attachments as Service;
|
||
|
|
||
|
class Attachments
|
||
|
{
|
||
|
use Json;
|
||
|
|
||
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||
|
{
|
||
|
$attachments = array_map(function($attachment) {
|
||
|
$attachment['original_attachment'] = $attachment['original_attachment']->getRealPath();
|
||
|
if (isset($attachment['decrypted_attachment']) and $attachment['decrypted_attachment'] !== false) {
|
||
|
$attachment['decrypted_attachment'] = $attachment['decrypted_attachment']->getRealPath();
|
||
|
}
|
||
|
return $attachment;
|
||
|
}, $service->fetchFullAttachments());
|
||
|
return $this->withJson($response, compact('attachments'));
|
||
|
}
|
||
|
protected function fileToArray(\SplFileInfo $info): array
|
||
|
{
|
||
|
return [
|
||
|
'filename' => $info->getFilename(),
|
||
|
'path' => $info->getPath(),
|
||
|
'full_name' => $info->getRealPath(),
|
||
|
'extension' => $info->getExtension()
|
||
|
];
|
||
|
}
|
||
|
public function get(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||
|
{
|
||
|
$body = $request->getBody()->getContents();
|
||
|
$json = json_decode($body);
|
||
|
if (!is_array($json)) {
|
||
|
$json = [$json];
|
||
|
}
|
||
|
$output = ['input' => $json, 'attachments' => []];
|
||
|
foreach ($json as $attachment) {
|
||
|
$output['attachments'] []= $this->fileToArray($service->getAttachment($json->attachment));
|
||
|
}
|
||
|
return $this->withJson($response, $output);
|
||
|
}
|
||
|
public function decrypt(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||
|
{
|
||
|
$body = $request->getBody()->getContents();
|
||
|
$json = json_decode($body);
|
||
|
if (!is_array($json)) {
|
||
|
$json = [$json];
|
||
|
}
|
||
|
$output = ['input' => $json, 'attachments' => []];
|
||
|
foreach ($json as $attachment) {
|
||
|
$output['attachments'] []= $this->fileToArray($service->removeEncryption($attachment));
|
||
|
}
|
||
|
return $this->withJson($response, $output);
|
||
|
}
|
||
|
}
|