Files
myep-team/src/Controller/Administrative/Document/BatchDownloadController.php
T

93 lines
3.2 KiB
PHP

<?php
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;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use ZipStream\Exception\FileNotFoundException;
use ZipStream\Exception\FileNotReadableException;
use ZipStream\ZipStream;
class BatchDownloadController extends AbstractController
{
public function __construct(
private readonly UploadRepository $uploadRepository,
private readonly UploadHandler $uploadHandler,
private readonly EntityManagerInterface $entityManager,
private readonly LoggerInterface $logger,
) {
}
#[Route(
path: '/administrative/document/batch-download/{type}',
name: 'app_administrative_document_batch_download',
defaults: ['type' => Upload::TYPE_INVOICE]
)]
#[IsGranted('ROLE_ADMINISTRATIVE')]
public function index(string $type): Response
{
$status = Upload::TYPE_INVOICE === $type ? Upload::STATUS_PAID : Upload::STATUS_CHECKED;
$uploads = $this
->uploadRepository
->getBatchDownloadList($type, $status)
;
if (0 === count($uploads)) {
$this->addFlash('error', 'Keine Dokumente vorhanden');
return $this->redirectToRoute('app_administrative_document_index');
}
$zipFilename = sprintf('%ss_%s.zip', $type, date('YmdHi'));
$response = new StreamedResponse(function () use ($uploads, $zipFilename) {
$zip = new ZipStream(
defaultEnableZeroHeader: true,
outputName: $zipFilename,
contentType: 'application/octet-stream'
);
$namer = new DownloadNamer();
foreach ($uploads as $upload) {
/** @var Upload $upload */
$teamer = $upload->getOwner()->getTeamer();
$filename = $namer->nameForTeamer($upload, $teamer);
$filepath = $this->uploadHandler->getResolvedUploadFilepath($upload);
try {
$zip->addFileFromPath(fileName: $filename, path: $filepath);
$upload
->setDownloaded()
->setDownloadedBy($this->getUser()->getUserIdentifier())
;
} catch (FileNotFoundException|FileNotReadableException $e) {
$this->logger->error('Unable to add file to ZIP', [
'upload' => $upload->getUuid(),
'error' => $e->getMessage(),
]);
}
}
$zip->finish();
$this->entityManager->flush();
});
$disposition = HeaderUtils::makeDisposition('attachment', $zipFilename, md5($zipFilename));
$response->headers->set('Content-Disposition', $disposition);
return $response;
}
}