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

Merged
akurilenko merged 22 commits from TD-322 into main 2024-10-28 14:44:12 +03:00
4 changed files with 59 additions and 37 deletions
Showing only changes of commit c0e5d795b8 - Show all commits

View File

@ -11,35 +11,34 @@ use GuzzleHttp\RequestOptions;
class Api extends AbstractApi class Api extends AbstractApi
{ {
private const string TYPE_OTHER = 'other';
private const string API_DOCUMENT = '/api/v1/document/upload/batch/';
public ApiParams $apiParams; 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 = [ $params = [
'Accept' => 'application/json', RequestOptions::HEADERS => [
'Authorization' => $token, 'Authorization' => $token,
]; 'Accept' => 'application/json',
$options = [
'multipart' => [
[
'name' => 'type',
'contents' => self::TYPE_OTHER
], ],
RequestOptions::MULTIPART => [
[ [
'name' => 'file', 'name' => 'file',
'contents' => fopen($urlDocument, 'r'), 'contents' => fopen($path, 'r'),
'filename' => 'sign_attorney.pdf', 'filename' => $path,
'headers' => [ 'headers' => [
'Content-Type' => '<Content-type header>' 'Content-Type' => '<Content-type header>',
] ],
] ],
]]; [
$request = new Request('POST', sprintf('%s%s%s', $url, self::API_DOCUMENT, $batch), $headers); 'name' => 'type',
$res = $this->client->sendAsync($request, $options)->wait(); '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 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 class SignService
{ {
private DevSignService $devSignService;
private ProdSignService $prodSignService;
public function __construct( public function __construct(
private Api $api, private Api $api,
private ApiParams $apiParams private ApiParams $apiParams
){ ){
$this->devSignService = new DevSignService();
$this->prodSignService = new ProdSignService();
} }
public function signDocument(string $url, string $token, int $batch): array public function signDocument(string $url, string $token, int $batch): array
{ {
@ -26,7 +31,7 @@ class SignService
$this->sign($document->tempFileName); $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); $this->removeExistingDocument($document);
@ -39,22 +44,11 @@ class SignService
private function sign (string $documentUrl): void private function sign (string $documentUrl): void
{ {
match ($_ENV['APP_ENV']) { match ($_ENV['APP_ENV']) {
'dev' => $this->signDev($documentUrl), 'dev' => $this->devSignService->sign($documentUrl),
'prod' => $this->signProd($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 private function removeExistingDocument(BinaryStringFileResult $document): void
{ {
if (file_exists($document->tempFileName)) { if (file_exists($document->tempFileName)) {