diff --git a/config/services.yaml b/config/services.yaml index 98aa87f..04fa4da 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -31,6 +31,7 @@ services: autowire: true autoconfigure: true bind: + $tempDir: '%kernel.project_dir%/temp' $logger: '@monolog.logger.myep' $destinations: '%destinations%' @@ -138,6 +139,11 @@ services: $orphanageManager: '@oneup_uploader.orphanage_manager' $projectDir: '%kernel.project_dir%' + App\Service\Pdf\Sanitizer: + arguments: + $binGs: '/usr/bin/gs' + $binConvert: '/usr/bin/convert' + App\EventListener\UploadSessionListener: tags: - name: kernel.event_listener diff --git a/src/Controller/Administrative/Document/CheckController.php b/src/Controller/Administrative/Document/CheckController.php index a5da8fa..a17eda4 100644 --- a/src/Controller/Administrative/Document/CheckController.php +++ b/src/Controller/Administrative/Document/CheckController.php @@ -126,7 +126,11 @@ class CheckController extends AbstractController ; if (Upload::TYPE_INVOICE === $document->getType()) { - $this->invoiceApprover->render($document->getDisposition()); + try { + $this->invoiceApprover->render($document->getDisposition()); + } catch (\InvalidArgumentException $e) { + $this->addFlash('error', 'Die Honorarnote ist nicht als PDF hochgeladen worden'); + } } $this->addFlash('success', 'Das Dokument wurde bestÃĪtigt'); diff --git a/src/Controller/Teamer/Disposition/DetailController.php b/src/Controller/Teamer/Disposition/DetailController.php index bca74eb..862e99e 100644 --- a/src/Controller/Teamer/Disposition/DetailController.php +++ b/src/Controller/Teamer/Disposition/DetailController.php @@ -86,10 +86,11 @@ class DetailController extends AbstractController $disposition->addDocument($upload); $this->workflow->apply($disposition, 'upload_contract'); - $this->entityManager->flush(); - $this->uploadHandler->moveUploadSessionFilesFromOrphanage(Upload::TYPE_CONTRACT, $uploadSession); $this->uploadHandler->destroyUploadSession(); + $this->uploadHandler->ensurePdf($upload); + + $this->entityManager->flush(); $this->addFlash('success', 'Der Honorarvertrag wurde hochgeladen'); @@ -115,10 +116,11 @@ class DetailController extends AbstractController $disposition->addDocument($upload); $this->workflow->apply($disposition, 'upload_invoice'); - $this->entityManager->flush(); - $this->uploadHandler->moveUploadSessionFilesFromOrphanage(Upload::TYPE_INVOICE, $uploadSession); $this->uploadHandler->destroyUploadSession(); + $this->uploadHandler->ensurePdf($upload); + + $this->entityManager->flush(); $this->addFlash('success', 'Die Honornote wurde hochgeladen'); @@ -128,4 +130,4 @@ class DetailController extends AbstractController $this->eventDispatcher->dispatch(new DocumentUploadedEvent($upload), DocumentUploadedEvent::NAME); } -} \ No newline at end of file +} diff --git a/src/Service/Pdf/InvoiceApprover.php b/src/Service/Pdf/InvoiceApprover.php index 204c1ae..15625af 100644 --- a/src/Service/Pdf/InvoiceApprover.php +++ b/src/Service/Pdf/InvoiceApprover.php @@ -8,12 +8,19 @@ use Symfony\Component\Filesystem\Filesystem; class InvoiceApprover extends AbstractPdfRenderer { + /** + * @throws \InvalidArgumentException + */ public function render(Disposition $disposition): Pdf { $teamer = $disposition->getTeamer(); $assignment = $disposition->getAssignment(); $invoice = $disposition->getDocumentByType(Upload::TYPE_INVOICE); - $originalPdfFilename = $invoice->getFilename(); + $originalFilename = $invoice->getFilename(); + + if ('application/pdf' !== $invoice->getMimeType()) { + throw new \InvalidArgumentException('Expected PDF but got '.$invoice->getMimeType()); + } $pdf = new Pdf(); @@ -21,7 +28,7 @@ class InvoiceApprover extends AbstractPdfRenderer $pdf->SetCreator($teamer->getFullName(), true); // Load template - $filepath = sprintf('%s/uploads/invoice/%s', $this->config['project_dir'], $originalPdfFilename); + $filepath = sprintf('%s/uploads/invoice/%s', $this->config['project_dir'], $originalFilename); $pdf->appendPdf($filepath); $pdf->setPage(1); diff --git a/src/Service/Pdf/Sanitizer.php b/src/Service/Pdf/Sanitizer.php new file mode 100644 index 0000000..fd29ee0 --- /dev/null +++ b/src/Service/Pdf/Sanitizer.php @@ -0,0 +1,82 @@ +copy($inputFilepath, $inputFilepath.'.bak'); + } + + $tempFilename = sha1($inputFilepath); + $outputFilepath = $this->tempDir.'/'.$tempFilename; + $pdfFilepath = null; + + if (null === $targetFilepath) { + $targetFilepath = $inputFilepath.'.pdf'; + } + + if ('pdf' !== pathinfo($inputFilepath, PATHINFO_EXTENSION)) { + $pdfFilepath = $outputFilepath.'.pdf'; + + $command = [ + $this->binConvert, + $inputFilepath, + $pdfFilepath, + ]; + + $process = new Process($command); + $process->run(); + + if (false === $process->isSuccessful()) { + throw new \InvalidArgumentException('File could not be sanitized: '.$process->getErrorOutput()); + } + + $inputFilepath = $pdfFilepath; + } + + $command = [ + $this->binGs, + '-dNOPAUSE', + '-dQUIET', + '-dBATCH', + '-sDEVICE=pdfwrite', + '-dCompatibilityLevel=1.4', + '-sOutputFile='.$outputFilepath, + '-f', + $inputFilepath, + ]; + + $process = new Process($command); + $process->run(); + + if (false === $process->isSuccessful()) { + throw new \InvalidArgumentException('File could not be sanitized: '.$process->getErrorOutput()); + } + + $filesystem->rename($outputFilepath, $targetFilepath, true); + + if (null !== $pdfFilepath) { + $filesystem->remove($pdfFilepath); + } + + return $targetFilepath; + } +} diff --git a/src/Service/Upload/UploadHandler.php b/src/Service/Upload/UploadHandler.php index 2ffa176..0a8f22b 100644 --- a/src/Service/Upload/UploadHandler.php +++ b/src/Service/Upload/UploadHandler.php @@ -5,6 +5,7 @@ namespace App\Service\Upload; use App\Entity\Upload; use App\Model\UploadDto; use App\Model\UploadSessionDto; +use App\Service\Pdf\Sanitizer; use Oneup\UploaderBundle\Uploader\Orphanage\OrphanageManager; use Symfony\Component\Filesystem\Exception\IOException; use Symfony\Component\Filesystem\Filesystem; @@ -17,6 +18,7 @@ class UploadHandler public function __construct( private readonly RequestStack $requestStack, private readonly OrphanageManager $orphanageManager, + private readonly Sanitizer $sanitizer, private readonly string $projectDir ) { } @@ -131,4 +133,17 @@ class UploadHandler { return sprintf('%s/uploads/temp/%s', $this->projectDir, $uploadSessionId); } + + public function ensurePdf(Upload $upload): void + { + try { + $filepath = $this->getUploadFilepath($upload); + $sanitizedFilepath = $this->sanitizer->sanitizeFile($filepath); + + if ($filepath !== $sanitizedFilepath) { + $upload->setFilename(basename($sanitizedFilepath)); + } + } catch (\InvalidArgumentException $e) { + } + } } diff --git a/tests/Resources/upload.jpg b/tests/Resources/upload.jpg new file mode 100644 index 0000000..fefcb20 Binary files /dev/null and b/tests/Resources/upload.jpg differ diff --git a/tests/Resources/upload.pdf b/tests/Resources/upload.pdf new file mode 100644 index 0000000..09ac530 Binary files /dev/null and b/tests/Resources/upload.pdf differ diff --git a/tests/Service/Pdf/SanitizerTest.php b/tests/Service/Pdf/SanitizerTest.php new file mode 100644 index 0000000..b9b18fb --- /dev/null +++ b/tests/Service/Pdf/SanitizerTest.php @@ -0,0 +1,40 @@ +sanitizeFile($sourceFilepath, $targetFilepath); + + $this->assertFileExists($targetFilepath); + + (new Filesystem())->remove($targetFilepath); + } + + public function testAcceptsPdfs(): void + { + $tempDir = realpath(__DIR__.'/../../../temp'); + $sourceFilepath = realpath(__DIR__.'/../../Resources/upload.pdf'); + $targetFilepath = $tempDir.'/test.pdf'; + + $sanitizer = new Sanitizer('/usr/bin/gs', '/usr/bin/convert', $tempDir); + + $sanitizer->sanitizeFile($sourceFilepath, $targetFilepath); + + $this->assertFileExists($targetFilepath); + + (new Filesystem())->remove($targetFilepath); + } +}