From b5f1f3153f75260699012d51d694ce7757af155d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 15 Nov 2024 14:17:57 +0100 Subject: [PATCH] Convert non-PDF documents before applying a signature Enhanced the `addConvertedVersion` method in `StoredObjectToPdfConverter` to optionally include converted content in the response. Updated `SignatureRequestController` to handle non-PDF documents by converting them to PDF before processing the signature request. --- .../Controller/SignatureRequestController.php | 16 +++++++++++++--- .../Service/StoredObjectToPdfConverter.php | 11 ++++++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Bundle/ChillDocStoreBundle/Controller/SignatureRequestController.php b/src/Bundle/ChillDocStoreBundle/Controller/SignatureRequestController.php index b443779bb..913dde2d5 100644 --- a/src/Bundle/ChillDocStoreBundle/Controller/SignatureRequestController.php +++ b/src/Bundle/ChillDocStoreBundle/Controller/SignatureRequestController.php @@ -15,12 +15,14 @@ use Chill\DocStoreBundle\Service\Signature\Driver\BaseSigner\RequestPdfSignMessa use Chill\DocStoreBundle\Service\Signature\PDFPage; use Chill\DocStoreBundle\Service\Signature\PDFSignatureZone; use Chill\DocStoreBundle\Service\StoredObjectManagerInterface; +use Chill\DocStoreBundle\Service\StoredObjectToPdfConverter; use Chill\MainBundle\Entity\Workflow\EntityWorkflowSignatureStateEnum; use Chill\MainBundle\Entity\Workflow\EntityWorkflowStepSignature; use Chill\MainBundle\Templating\Entity\ChillEntityRenderManagerInterface; use Chill\MainBundle\Security\Authorization\EntityWorkflowStepSignatureVoter; use Chill\MainBundle\Templating\Entity\UserRender; use Chill\MainBundle\Workflow\EntityWorkflowManager; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -39,6 +41,8 @@ class SignatureRequestController private readonly ChillEntityRenderManagerInterface $entityRender, private readonly NormalizerInterface $normalizer, private readonly Security $security, + private readonly StoredObjectToPdfConverter $converter, + private readonly EntityManagerInterface $entityManager, ) {} #[Route('/api/1.0/document/workflow/{id}/signature-request', name: 'chill_docstore_signature_request')] @@ -53,11 +57,17 @@ class SignatureRequestController if (EntityWorkflowSignatureStateEnum::PENDING !== $signature->getState()) { return new JsonResponse([], status: Response::HTTP_CONFLICT); } - $storedObject = $this->entityWorkflowManager->getAssociatedStoredObject($entityWorkflow); - $content = $this->storedObjectManager->read($storedObject); - $data = \json_decode((string) $request->getContent(), true, 512, JSON_THROW_ON_ERROR); // TODO parse payload: json_decode ou, mieux, dataTransfertObject + if ('application/pdf' !== $storedObject->getType()) { + [$storedObject, $storedObjectVersion, $content] = $this->converter->addConvertedVersion($storedObject, $request->getLocale(), includeConvertedContent: true); + $this->entityManager->persist($storedObjectVersion); + $this->entityManager->flush(); + } else { + $content = $this->storedObjectManager->read($storedObject); + } + + $data = \json_decode((string) $request->getContent(), true, 512, JSON_THROW_ON_ERROR); $zone = new PDFSignatureZone( $data['zone']['index'], $data['zone']['x'], diff --git a/src/Bundle/ChillDocStoreBundle/Service/StoredObjectToPdfConverter.php b/src/Bundle/ChillDocStoreBundle/Service/StoredObjectToPdfConverter.php index abfcca4cc..ac570c513 100644 --- a/src/Bundle/ChillDocStoreBundle/Service/StoredObjectToPdfConverter.php +++ b/src/Bundle/ChillDocStoreBundle/Service/StoredObjectToPdfConverter.php @@ -39,13 +39,13 @@ class StoredObjectToPdfConverter * @param string $lang the language for the conversion context * @param string $convertTo The target format for the conversion. Default is 'pdf'. * - * @return array{0: StoredObjectPointInTime, 1: StoredObjectVersion} contains the point in time before conversion and the new version of the stored object + * @return array{0: StoredObjectPointInTime, 1: StoredObjectVersion, 2?: string} contains the point in time before conversion and the new version of the stored object. The converted content is included in the response if $includeConvertedContent is true * * @throws \UnexpectedValueException if the preferred mime type for the conversion is not found * @throws \RuntimeException if the conversion or storage of the new version fails * @throws StoredObjectManagerException */ - public function addConvertedVersion(StoredObject $storedObject, string $lang, $convertTo = 'pdf'): array + public function addConvertedVersion(StoredObject $storedObject, string $lang, $convertTo = 'pdf', bool $includeConvertedContent = false): array { $newMimeType = $this->mimeTypes->getMimeTypes($convertTo)[0] ?? null; @@ -70,6 +70,11 @@ class StoredObjectToPdfConverter $pointInTime = new StoredObjectPointInTime($currentVersion, StoredObjectPointInTimeReasonEnum::KEEP_BEFORE_CONVERSION); $version = $this->storedObjectManager->write($storedObject, $converted, $newMimeType); - return [$pointInTime, $version]; + if (!$includeConvertedContent) { + return [$pointInTime, $version]; + } + + return [$pointInTime, $version, $converted]; + } }