<?php
namespace App\Controller;
use App\Entity\Downloads;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;
class DownloadsController extends AbstractController
{
/**
* @Route("/download/{download_url}", name="download_url")
*/
public function downloadFileAction(Request $request): Response
{
$url = $request->get('download_url');
$path = __DIR__ . '/../../public/documents/';
$pdf = $this->getDoctrine()->getRepository(Downloads::class)->findOneBy(['url' => $url]);
$response = new BinaryFileResponse($path . $pdf->getFile());
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$pdf->getFile()
);
return $response;
}
}