diff --git a/src/Controller/Administrative/Document/BatchDownloadController.php b/src/Controller/Administrative/Document/BatchDownloadController.php index 220e49a..1f94aa8 100644 --- a/src/Controller/Administrative/Document/BatchDownloadController.php +++ b/src/Controller/Administrative/Document/BatchDownloadController.php @@ -4,6 +4,7 @@ namespace App\Controller\Administrative\Document; use App\Entity\Upload; use App\Repository\UploadRepository; +use App\Service\Upload\DownloadNamer; use App\Service\Upload\UploadHandler; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; @@ -57,16 +58,13 @@ class BatchDownloadController extends AbstractController contentType: 'application/octet-stream' ); + $namer = new DownloadNamer(); + foreach ($uploads as $upload) { /** @var Upload $upload */ - $filename = $upload->getOriginalFilename(); + $teamer = $upload->getOwner()->getTeamer(); + $filename = $namer->nameForTeamer($upload, $teamer); $filepath = $this->uploadHandler->getUploadFilepath($upload); - $extension = pathinfo($filepath, PATHINFO_EXTENSION); - - // adopt potentially changed file extension - if (false === str_ends_with($filename, $extension)) { - $filename = $filename.'.'.$extension; - } try { $zip->addFileFromPath(fileName: $filename, path: $filepath); @@ -82,9 +80,9 @@ class BatchDownloadController extends AbstractController } } $zip->finish(); - }); - $this->entityManager->flush(); + $this->entityManager->flush(); + }); $disposition = HeaderUtils::makeDisposition('attachment', $zipFilename, md5($zipFilename)); $response->headers->set('Content-Disposition', $disposition); diff --git a/src/Controller/Common/DownloadController.php b/src/Controller/Common/DownloadController.php index f6bd29c..b4813b7 100644 --- a/src/Controller/Common/DownloadController.php +++ b/src/Controller/Common/DownloadController.php @@ -3,6 +3,7 @@ namespace App\Controller\Common; use App\Entity\Upload; +use App\Service\Upload\DownloadNamer; use App\Service\Upload\UploadHandler; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; @@ -29,13 +30,19 @@ class DownloadController extends AbstractController public function index(Upload $upload, Request $request): Response { $path = $this->uploadHandler->getUploadFilepath($upload); - $extension = pathinfo($path, PATHINFO_EXTENSION); - // adopt potentially changed file extension - $originalFilename = $upload->getOriginalFilename(); + if (true === in_array($upload->getType(), [Upload::TYPE_INVOICE, Upload::TYPE_CONTRACT])) { + $namer = new DownloadNamer(); + $teamer = $upload->getOwner()->getTeamer(); + $originalFilename = $namer->nameForTeamer($upload, $teamer); + } else { + // adopt potentially changed file extension + $extension = pathinfo($path, PATHINFO_EXTENSION); + $originalFilename = $upload->getOriginalFilename(); - if (false === str_ends_with($originalFilename, $extension)) { - $originalFilename = $originalFilename.'.'.$extension; + if (false === str_ends_with($originalFilename, $extension)) { + $originalFilename = $originalFilename.'.'.$extension; + } } $inline = (bool) $request->get('inline'); diff --git a/src/Service/Upload/DownloadNamer.php b/src/Service/Upload/DownloadNamer.php new file mode 100644 index 0000000..814c6e4 --- /dev/null +++ b/src/Service/Upload/DownloadNamer.php @@ -0,0 +1,33 @@ +getType()) { + Upload::TYPE_INVOICE => 'hn-%d-%s-%s.pdf', + Upload::TYPE_CONTRACT => 'hv-%d-%s-%s.pdf', + default => null, + }; + + if (null === $pattern) { + return $upload->getOriginalFilename(); + } + + $slugger = new AsciiSlugger('de'); + $teamerName = strtolower($slugger->slug($teamer->getFullName(true), '_')); + + return sprintf( + $pattern, + $upload->getId(), + $teamerName, + $upload->getCreatedAt()->format('Ymd') + ); + } +}