feat: streamlined filenames of downloaded teamer documents

addresses #8698updx6
This commit is contained in:
Björn Fromme
2025-04-24 10:03:29 +02:00
parent fb0f8054d5
commit 3646661fed
3 changed files with 52 additions and 14 deletions
@@ -4,6 +4,7 @@ namespace App\Controller\Administrative\Document;
use App\Entity\Upload; use App\Entity\Upload;
use App\Repository\UploadRepository; use App\Repository\UploadRepository;
use App\Service\Upload\DownloadNamer;
use App\Service\Upload\UploadHandler; use App\Service\Upload\UploadHandler;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@@ -57,16 +58,13 @@ class BatchDownloadController extends AbstractController
contentType: 'application/octet-stream' contentType: 'application/octet-stream'
); );
$namer = new DownloadNamer();
foreach ($uploads as $upload) { foreach ($uploads as $upload) {
/** @var Upload $upload */ /** @var Upload $upload */
$filename = $upload->getOriginalFilename(); $teamer = $upload->getOwner()->getTeamer();
$filename = $namer->nameForTeamer($upload, $teamer);
$filepath = $this->uploadHandler->getUploadFilepath($upload); $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 { try {
$zip->addFileFromPath(fileName: $filename, path: $filepath); $zip->addFileFromPath(fileName: $filename, path: $filepath);
@@ -82,9 +80,9 @@ class BatchDownloadController extends AbstractController
} }
} }
$zip->finish(); $zip->finish();
});
$this->entityManager->flush(); $this->entityManager->flush();
});
$disposition = HeaderUtils::makeDisposition('attachment', $zipFilename, md5($zipFilename)); $disposition = HeaderUtils::makeDisposition('attachment', $zipFilename, md5($zipFilename));
$response->headers->set('Content-Disposition', $disposition); $response->headers->set('Content-Disposition', $disposition);
+12 -5
View File
@@ -3,6 +3,7 @@
namespace App\Controller\Common; namespace App\Controller\Common;
use App\Entity\Upload; use App\Entity\Upload;
use App\Service\Upload\DownloadNamer;
use App\Service\Upload\UploadHandler; use App\Service\Upload\UploadHandler;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@@ -29,13 +30,19 @@ class DownloadController extends AbstractController
public function index(Upload $upload, Request $request): Response public function index(Upload $upload, Request $request): Response
{ {
$path = $this->uploadHandler->getUploadFilepath($upload); $path = $this->uploadHandler->getUploadFilepath($upload);
$extension = pathinfo($path, PATHINFO_EXTENSION);
// adopt potentially changed file extension if (true === in_array($upload->getType(), [Upload::TYPE_INVOICE, Upload::TYPE_CONTRACT])) {
$originalFilename = $upload->getOriginalFilename(); $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)) { if (false === str_ends_with($originalFilename, $extension)) {
$originalFilename = $originalFilename.'.'.$extension; $originalFilename = $originalFilename.'.'.$extension;
}
} }
$inline = (bool) $request->get('inline'); $inline = (bool) $request->get('inline');
+33
View File
@@ -0,0 +1,33 @@
<?php
namespace App\Service\Upload;
use App\Entity\Teamer;
use App\Entity\Upload;
use Symfony\Component\String\Slugger\AsciiSlugger;
class DownloadNamer
{
public function nameForTeamer(Upload $upload, Teamer $teamer): string
{
$pattern = match($upload->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')
);
}
}