74 lines
2.7 KiB
PHP
74 lines
2.7 KiB
PHP
<?php
|
|
|
|
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;
|
|
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 EntityManagerInterface $entityManager,
|
|
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->getResolvedUploadFilepath($upload);
|
|
|
|
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;
|
|
|
|
// mark file downloaded if applicable
|
|
if (true === $this->isGranted('ROLE_ADMINISTRATIVE') && false === $inline) {
|
|
$upload
|
|
->setDownloaded()
|
|
->setDownloadedBy($this->getUser()->getUserIdentifier())
|
|
;
|
|
$this->entityManager->flush();
|
|
}
|
|
|
|
$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;
|
|
}
|
|
}
|