<?php
declare(strict_types=1);
namespace App\Controller\Front\Download\Manual;
use App\Application\Service\Storage\DownloadService;
use App\Service\Download\ManualDownloadService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ManualController extends AbstractController
{
private DownloadService $downloadService;
private ManualDownloadService $manualDownloadService;
public function __construct(
DownloadService $downloadService,
ManualDownloadService $manualDownloadService
) {
$this->downloadService = $downloadService;
$this->manualDownloadService = $manualDownloadService;
}
/**
* @Route("/manual/course/{downloadId}", name="download_course_manual", methods={"GET"})
*/
public function downloadCourseManual(int $downloadId): ?Response
{
if (!$this->manualDownloadService->validateCourseProduct($downloadId)) {
return new Response('', Response::HTTP_CONFLICT, ['Content-Disposition' => 'attachment']);
}
try {
$productManual = $this->downloadService->downloadProductManual($downloadId);
} catch (\Throwable $t) {
$productManual = null;
}
if (!$productManual || empty($productManual->content)) {
return new Response('', Response::HTTP_CONFLICT, ['Content-Disposition' => 'attachment']);
}
$response = new Response();
$response->setContent($productManual->content);
$response->headers->set('Content-Type', $productManual->mimeType);
$response->headers->set('Content-Disposition', 'attachment; filename="'.$productManual->fileName.'"');
return $response;
}
}