feat: download ZIP archive of invoices with status 'paid'
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Administrative\Document;
|
||||
|
||||
use App\Entity\Upload;
|
||||
use App\Repository\UploadRepository;
|
||||
use App\Service\Upload\UploadHandler;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\HeaderUtils;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
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
|
||||
) {
|
||||
}
|
||||
|
||||
#[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, Request $request): Response
|
||||
{
|
||||
$status = $request->query->get('status', Upload::STATUS_PAID);
|
||||
|
||||
$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('Honorarnoten_%s.zip', date('YmdHi'));
|
||||
|
||||
$response = new StreamedResponse(function () use ($uploads, $zipFilename) {
|
||||
$zip = new ZipStream(
|
||||
defaultEnableZeroHeader: true,
|
||||
outputName: $zipFilename,
|
||||
contentType: 'application/octet-stream'
|
||||
);
|
||||
|
||||
foreach ($uploads as $upload) {
|
||||
/** @var Upload $upload */
|
||||
$filename = $upload->getOriginalFilename();
|
||||
$filepath = $this->uploadHandler->getUploadFilepath($upload);
|
||||
|
||||
try {
|
||||
$zip->addFileFromPath(fileName: $filename, path: $filepath);
|
||||
$upload
|
||||
->setDownloaded()
|
||||
->setDownloadedBy($this->getUser()->getUserIdentifier())
|
||||
;
|
||||
} catch (FileNotFoundException $e) {
|
||||
} catch (FileNotReadableException $e) {
|
||||
}
|
||||
}
|
||||
$zip->finish();
|
||||
$this->entityManager->flush();
|
||||
});
|
||||
|
||||
$disposition = HeaderUtils::makeDisposition('attachment', $zipFilename, md5($zipFilename));
|
||||
$response->headers->set('Content-Disposition', $disposition);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user