feat: streamlined filenames of downloaded teamer documents
addresses #8698updx6
This commit is contained in:
@@ -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();
|
||||
});
|
||||
|
||||
$disposition = HeaderUtils::makeDisposition('attachment', $zipFilename, md5($zipFilename));
|
||||
$response->headers->set('Content-Disposition', $disposition);
|
||||
|
||||
@@ -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,14 +30,20 @@ class DownloadController extends AbstractController
|
||||
public function index(Upload $upload, Request $request): Response
|
||||
{
|
||||
$path = $this->uploadHandler->getUploadFilepath($upload);
|
||||
$extension = pathinfo($path, PATHINFO_EXTENSION);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
$inline = (bool) $request->get('inline');
|
||||
$disposition = $inline ? ResponseHeaderBag::DISPOSITION_INLINE : ResponseHeaderBag::DISPOSITION_ATTACHMENT;
|
||||
|
||||
@@ -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')
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user