From 52d40c6249c7f2b6be352eac3d468555df54fde9 Mon Sep 17 00:00:00 2001 From: StSet Date: Fri, 21 Mar 2025 12:15:22 +0300 Subject: [PATCH] =?UTF-8?q?DDB-1753=20-=20=D0=B4=D0=BE=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=BA=D0=B0=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82?= =?UTF-8?q?=D0=B0=20=D0=BE=D1=82=D0=B2=D0=B5=D1=82=D0=B0=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D1=81=D0=B5=D1=80=D0=B2=D0=B8=D1=81=D0=B0=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B4=D0=BF=D0=B8=D1=81=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../External/Api/BinaryStringFileResult.php | 12 ++++++++ .../Api/Response/DigitalSignatureResponse.php | 18 ++++++++++++ .../Controller/DigitalSignatureController.php | 6 ++-- .../Services/DigitalSignatureService.php | 28 +++++++++++++------ 4 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 backend/src/SignDocument/Api/Response/DigitalSignatureResponse.php diff --git a/backend/src/Infrastructure/External/Api/BinaryStringFileResult.php b/backend/src/Infrastructure/External/Api/BinaryStringFileResult.php index 6331c2c..363ef74 100644 --- a/backend/src/Infrastructure/External/Api/BinaryStringFileResult.php +++ b/backend/src/Infrastructure/External/Api/BinaryStringFileResult.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace App\Infrastructure\External\Api; +use RuntimeException; + /** * Враппер бинарных фалов, при создании класса содает новый временный файл, * при необходимости сохраняет файл под новым именем @@ -23,4 +25,14 @@ class BinaryStringFileResult $this->tempFileName = sprintf('%s/%s_%s', sys_get_temp_dir(), 'Document', time()); file_put_contents($this->tempFileName, $content); } + + public function remove(): bool + { + if (file_exists($this->tempFileName)) { + unlink($this->tempFileName); + return true; + } + + throw new RuntimeException('Temp file not found'); + } } diff --git a/backend/src/SignDocument/Api/Response/DigitalSignatureResponse.php b/backend/src/SignDocument/Api/Response/DigitalSignatureResponse.php new file mode 100644 index 0000000..7392f76 --- /dev/null +++ b/backend/src/SignDocument/Api/Response/DigitalSignatureResponse.php @@ -0,0 +1,18 @@ +server->get('HTTP_AUTHORIZATION'); - return $this->createJsonResponse([ - 'hash' => $this->digitalSignatureService->getSignature($digitalSignatureRequest, $token), - ]); + return $this->createJsonResponseFromObject( + $this->digitalSignatureService->getSignature($digitalSignatureRequest, $token) + ); } } \ No newline at end of file diff --git a/backend/src/SignDocument/Services/DigitalSignatureService.php b/backend/src/SignDocument/Services/DigitalSignatureService.php index 6c9468b..7ff187e 100644 --- a/backend/src/SignDocument/Services/DigitalSignatureService.php +++ b/backend/src/SignDocument/Services/DigitalSignatureService.php @@ -4,22 +4,31 @@ declare(strict_types=1); namespace App\SignDocument\Services; +use App\Infrastructure\External\Api\BinaryStringFileResult; use App\SignDocument\Api\Api; use App\SignDocument\Api\ApiParams; use App\SignDocument\Api\Request\DigitalSignatureRequest; +use App\SignDocument\Api\Response\DigitalSignatureResponse; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Exception; use RuntimeException; class DigitalSignatureService { + private BinaryStringFileResult $document; + public function __construct( private readonly Api $api, private readonly ApiParams $apiParams, - private readonly RemoveExistingDocumentService $removeExistingDocumentService, ){ } - public function getSignature(DigitalSignatureRequest $request, string $token): string + + public function __destruct() + { + $this->document->remove(); + } + + public function getSignature(DigitalSignatureRequest $request, string $token): DigitalSignatureResponse { if ($_ENV['API_TOKEN'] !== $request->apiToken) { throw new AccessDeniedHttpException('Доступ запрещен'); @@ -28,16 +37,17 @@ class DigitalSignatureService $this->api->apiParams = $this->apiParams; try { - $document = $this->api->download($request->url, $token); + $this->document = $this->api->download($request->url, $token); - exec(sprintf('cp %s %s.pdf', $document->tempFileName, $document->tempFileName)); - exec(sprintf('cryptcp -sign -detached -der %s', $document->tempFileName . '.pdf')); + exec(sprintf('cp %s %s.pdf', $this->document->tempFileName, $this->document->tempFileName)); + exec(sprintf('cryptcp -sign -detached -der %s.pdf', $this->document->tempFileName)); - $response = base64_encode(file_get_contents($document->tempFileName . '.pdf.sgn')); + $response = base64_encode(file_get_contents($this->document->tempFileName . '.pdf.sgn')); - $this->removeExistingDocumentService->removeExistingDocument($document); - - return $response; + return new DigitalSignatureResponse( + hash: $response, + content: file_get_contents(sprintf('%s.pdf', $this->document->tempFileName)) + ); } catch (Exception $e) { throw new RuntimeException($e->getMessage()); }