47 lines
1.6 KiB
PHP
47 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\Common;
|
|
|
|
use App\Entity\Upload;
|
|
use App\Service\Upload\UploadHandler;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
|
|
|
class DownloadController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
private readonly UploadHandler $uploadHandler,
|
|
private readonly LoggerInterface $logger
|
|
) {
|
|
}
|
|
|
|
#[Route('/download/{uuid}', name: 'app_common_download')]
|
|
#[IsGranted('ROLE_USER')]
|
|
#[IsGranted('DOWNLOAD', subject: 'upload')]
|
|
public function index(Upload $upload, Request $request): Response
|
|
{
|
|
$path = $this->uploadHandler->getUploadFilepath($upload);
|
|
$originalFilename = $upload->getOriginalFilename();
|
|
$inline = (bool) $request->get('inline');
|
|
$disposition = $inline ? ResponseHeaderBag::DISPOSITION_INLINE : ResponseHeaderBag::DISPOSITION_ATTACHMENT;
|
|
|
|
$this->logger->info('Download file', [
|
|
'file_id' => $upload->getId(),
|
|
'file_filename' => $upload->getOriginalFilename(),
|
|
]);
|
|
|
|
try {
|
|
$response = $this->file($path, $originalFilename, $disposition);
|
|
} catch (FileNotFoundException $e) {
|
|
throw $this->createNotFoundException('Die Datei wurde nicht gefunden');
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
} |