feat: pdf converter command
This commit is contained in:
@@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Command;
|
||||||
|
|
||||||
|
use App\Entity\Upload;
|
||||||
|
use App\Service\Pdf\Sanitizer;
|
||||||
|
use App\Service\Upload\UploadHandler;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Helper\ProgressBar;
|
||||||
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
|
||||||
|
#[AsCommand(name: 'app:uploads:convert', description: 'Converts all uploads to sanitized PDFs')]
|
||||||
|
class ConvertUploadsCommand extends Command
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly EntityManagerInterface $entityManager,
|
||||||
|
private readonly UploadHandler $uploadHandler,
|
||||||
|
private readonly Sanitizer $sanitizer,
|
||||||
|
) {
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
|
{
|
||||||
|
$uploads = $this
|
||||||
|
->entityManager
|
||||||
|
->getRepository(Upload::class)
|
||||||
|
->findBy([
|
||||||
|
'type' => [Upload::TYPE_INVOICE, Upload::TYPE_CONTRACT],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$progressBar = new ProgressBar($output, count($uploads));
|
||||||
|
$progressBar->start();
|
||||||
|
|
||||||
|
foreach ($uploads as $upload) {
|
||||||
|
/** @var Upload $upload */
|
||||||
|
$filepath = $this->uploadHandler->getUploadFilepath($upload);
|
||||||
|
try {
|
||||||
|
$targetFilepath = $this->sanitizer->sanitizeFile($filepath);
|
||||||
|
$upload
|
||||||
|
->setFilename(basename($targetFilepath))
|
||||||
|
->setMimeType('application/pdf')
|
||||||
|
;
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
$progressBar->advance();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->entityManager->flush();
|
||||||
|
$progressBar->finish();
|
||||||
|
|
||||||
|
return Command::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Service\Pdf;
|
namespace App\Service\Pdf;
|
||||||
|
|
||||||
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
|
||||||
use Symfony\Component\Filesystem\Filesystem;
|
use Symfony\Component\Filesystem\Filesystem;
|
||||||
use Symfony\Component\Process\Process;
|
use Symfony\Component\Process\Process;
|
||||||
|
|
||||||
@@ -16,11 +17,16 @@ class Sanitizer
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
|
* @throws FileNotFoundException
|
||||||
*/
|
*/
|
||||||
public function sanitizeFile(string $inputFilepath, ?string $targetFilepath = null, bool $backup = true): string
|
public function sanitizeFile(string $inputFilepath, ?string $targetFilepath = null, bool $backup = true): string
|
||||||
{
|
{
|
||||||
$filesystem = new Filesystem();
|
$filesystem = new Filesystem();
|
||||||
|
|
||||||
|
if (false === $filesystem->exists($inputFilepath)) {
|
||||||
|
throw new FileNotFoundException();
|
||||||
|
}
|
||||||
|
|
||||||
if (true === $backup) {
|
if (true === $backup) {
|
||||||
$parts = pathinfo($inputFilepath);
|
$parts = pathinfo($inputFilepath);
|
||||||
$backupFilename = sprintf('%s/%s.orig.%s', $parts['dirname'], $parts['filename'], $parts['extension']);
|
$backupFilename = sprintf('%s/%s.orig.%s', $parts['dirname'], $parts['filename'], $parts['extension']);
|
||||||
@@ -32,7 +38,8 @@ class Sanitizer
|
|||||||
$pdfFilepath = null;
|
$pdfFilepath = null;
|
||||||
|
|
||||||
if (null === $targetFilepath) {
|
if (null === $targetFilepath) {
|
||||||
$targetFilepath = $inputFilepath.'.pdf';
|
$parts = pathinfo($inputFilepath);
|
||||||
|
$targetFilepath = sprintf('%s/%s.%s', $parts['dirname'], $parts['filename'], $parts['extension']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('pdf' !== pathinfo($inputFilepath, PATHINFO_EXTENSION)) {
|
if ('pdf' !== pathinfo($inputFilepath, PATHINFO_EXTENSION)) {
|
||||||
@@ -51,7 +58,10 @@ class Sanitizer
|
|||||||
throw new \InvalidArgumentException('File could not be sanitized: '.$process->getErrorOutput());
|
throw new \InvalidArgumentException('File could not be sanitized: '.$process->getErrorOutput());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$filesystem->remove($inputFilepath);
|
||||||
|
|
||||||
$inputFilepath = $pdfFilepath;
|
$inputFilepath = $pdfFilepath;
|
||||||
|
$targetFilepath.= '.pdf';
|
||||||
}
|
}
|
||||||
|
|
||||||
$command = [
|
$command = [
|
||||||
@@ -61,7 +71,7 @@ class Sanitizer
|
|||||||
'-dBATCH',
|
'-dBATCH',
|
||||||
'-dPreserveAnnots=false',
|
'-dPreserveAnnots=false',
|
||||||
'-sDEVICE=pdfwrite',
|
'-sDEVICE=pdfwrite',
|
||||||
'-dCompatibilityLevel=1.4',
|
'-dCompatibilityLevel=1.5',
|
||||||
'-sOutputFile='.$outputFilepath,
|
'-sOutputFile='.$outputFilepath,
|
||||||
'-f',
|
'-f',
|
||||||
$inputFilepath,
|
$inputFilepath,
|
||||||
|
|||||||
Reference in New Issue
Block a user