feat: pdf converter command

This commit is contained in:
Björn Fromme
2025-03-19 10:57:45 +01:00
parent 9aaf996e07
commit aae385d165
2 changed files with 72 additions and 2 deletions
+60
View File
@@ -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;
}
}