TD-322: [BACK]. [реализовать сервис для подписания документов в контейнере signer]

This commit is contained in:
cherednik 2024-10-11 18:19:53 +04:00
parent 9896071173
commit c0e5d795b8
4 changed files with 59 additions and 37 deletions

View File

@ -11,35 +11,34 @@ use GuzzleHttp\RequestOptions;
class Api extends AbstractApi
{
private const string TYPE_OTHER = 'other';
private const string API_DOCUMENT = '/api/v1/document/upload/batch/';
public ApiParams $apiParams;
public function send(string $url, string $token, $urlDocument, int $batch): array
public function send(string $token, string $path, int $batch): array
{
$headers = [
'Accept' => 'application/json',
$params = [
RequestOptions::HEADERS => [
'Authorization' => $token,
];
$options = [
'multipart' => [
[
'name' => 'type',
'contents' => self::TYPE_OTHER
'Accept' => 'application/json',
],
RequestOptions::MULTIPART => [
[
'name' => 'file',
'contents' => fopen($urlDocument, 'r'),
'filename' => 'sign_attorney.pdf',
'contents' => fopen($path, 'r'),
'filename' => $path,
'headers' => [
'Content-Type' => '<Content-type header>'
]
]
]];
$request = new Request('POST', sprintf('%s%s%s', $url, self::API_DOCUMENT, $batch), $headers);
$res = $this->client->sendAsync($request, $options)->wait();
'Content-Type' => '<Content-type header>',
],
],
[
'name' => 'type',
'contents' => 'other'
],
],
];
return $this->responseHandler->setResponse($res)->getContentJsonToArray();
$response = $this->client->post(sprintf('%s%s%s', $this->apiParams->endPointUrl, '/api/v1/document/upload/batch/', $batch), $params);
return $this->responseHandler->setResponse($response)->getContentJsonToArray();
}
public function download(string $url, string $token): BinaryStringFileResult

View File

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace App;
class DevSignService
{
public function sign(string $documentUrl): void
{
exec(sprintf('cp %s %s_sign.pdf', $documentUrl, $documentUrl));
}
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace App;
class ProdSignService
{
public function sign(string $documentUrl): void
{
exec(sprintf('cp %s %s.pdf', $documentUrl, $documentUrl));
exec(sprintf('pdfcpro sign /mnt/t/%s.pdf -out /mnt/t/%s_sign.pdf -cert ${SHA} -text "\n\t\tПодписано ЭП\n\t\t{subject/cn}\n\t\tСертификат {sha1}\n\t\tДействителен от {since} до {until}\n\t\tДата {date}\n\t\t{subject/t}\n\t\t{subject/fullname}\n\t\t" -fontfile /usr/local/share/fonts/Inter-Bold.ttf -fontsize 8 -x 2 -y 2 -w 96 -h 9', $documentUrl, $documentUrl));
}
}

View File

@ -12,10 +12,15 @@ use RuntimeException;
class SignService
{
private DevSignService $devSignService;
private ProdSignService $prodSignService;
public function __construct(
private Api $api,
private ApiParams $apiParams
){
$this->devSignService = new DevSignService();
$this->prodSignService = new ProdSignService();
}
public function signDocument(string $url, string $token, int $batch): array
{
@ -26,7 +31,7 @@ class SignService
$this->sign($document->tempFileName);
$response = $this->api->send($this->apiParams->endPointUrl, $token, $document->tempFileName . '_sign.pdf', $batch);
$response = $this->api->send($token, $document->tempFileName . '_sign.pdf', $batch);
$this->removeExistingDocument($document);
@ -39,22 +44,11 @@ class SignService
private function sign (string $documentUrl): void
{
match ($_ENV['APP_ENV']) {
'dev' => $this->signDev($documentUrl),
'prod' => $this->signProd($documentUrl),
'dev' => $this->devSignService->sign($documentUrl),
'prod' => $this->prodSignService->sign($documentUrl),
};
}
private function signDev(string $documentUrl): void
{
exec(sprintf('cp %s %s_sign.pdf', $documentUrl, $documentUrl));
}
private function signProd(string $documentUrl): void
{
exec(sprintf('cp %s %s.pdf', $documentUrl, $documentUrl));
exec(sprintf('pdfcpro sign /mnt/t/%s.pdf -out /mnt/t/%s_sign.pdf -cert ${SHA} -text "\n\t\tПодписано ЭП\n\t\t{subject/cn}\n\t\tСертификат {sha1}\n\t\tДействителен от {since} до {until}\n\t\tДата {date}\n\t\t{subject/t}\n\t\t{subject/fullname}\n\t\t" -fontfile /usr/local/share/fonts/Inter-Bold.ttf -fontsize 8 -x 2 -y 2 -w 96 -h 9', $documentUrl, $documentUrl));
}
private function removeExistingDocument(BinaryStringFileResult $document): void
{
if (file_exists($document->tempFileName)) {