<?php
namespace App\Controller;
use App\Entity\Vetoadom\Document;
use App\Service\CustomJsonResponse;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DownloadController extends AbstractController
{
private $urlGenerator;
private $vetoadomEntityManager;
private $customJsonResponse;
public function __construct(
UrlGeneratorInterface $urlGenerator,
EntityManagerInterface $vetoadomEntityManager,
CustomJsonResponse $customJsonResponse
)
{
$this->urlGenerator = $urlGenerator;
$this->vetoadomEntityManager = $vetoadomEntityManager;
$this->customJsonResponse = $customJsonResponse;
}
/**
* @Route("/download/{documentId}/{action}/{filename}", requirements={"action": "save|display"}, name="app_download", methods={"GET"})
* @param int $documentId
* @param string $action
* @param string $filename
* @return Response
*/
public function download(int $documentId = 0, string $action = "display", string $filename = ''): Response
{
$filter = [
'id' => $documentId,
'active' => 1
];
$document = $this->vetoadomEntityManager->getRepository(Document::class)->findOneBy($filter);
if (
!$document
){
throw $this->createNotFoundException();
}
if (($file = $this->documentParams($document)) === false){
throw $this->createNotFoundException();
}
// save
if ($action == "save") {
return $this->file($file, $document->getNom());
}
// display the file contents in the browser instead of downloading it
if ($filename === '') {
return new RedirectResponse($this->urlGenerator->generate('app_download', ['documentId' => $document->getId(), 'action' => 'display', 'filename' => $document->getNom()]));
}
return $this->file($file, $document->getNom(), ResponseHeaderBag::DISPOSITION_INLINE);
}
/**
* @Route("/download/{document_id}", name="app_download_delete", methods={"DELETE"})
* @param int $document_id
* @return JsonResponse
*/
public function downloadDelete(int $document_id = 0): JsonResponse
{
$document = $this->vetoadomEntityManager->getRepository(Document::class)->findOneBy(['id' => $document_id, 'active' => 1]);
if (
!$document
){
return $this->customJsonResponse->sendResponse();
}
$document->setActive(false);
$this->vetoadomEntityManager->flush();
return $this->customJsonResponse->sendResponse();
}
/**
* @Route("/downloads/{folder}/{file}", name="app_downloads", methods={"GET"})
* @param Request $request
* @param string $folder
* @param string $file
* @return Response
*/
public function downloads(Request $request, string $folder = '', string $file = ''): Response
{
$folder = preg_replace("/[^\p{L}0-9 _-]/u",'',rawurldecode($folder));
$path_parts = pathinfo(rawurldecode($file));
$filename = preg_replace("/[^\p{L}0-9 _-]/u",'',$path_parts['filename']);
$extension = preg_replace("/[^\p{L} _-]/u",'',$path_parts['extension']);
$document = $this->getParameter('downloads_path') . '/';
if ($folder != '') {
$document .= $folder . '/';
}
$document .= $filename . '.' . $extension;
if (!file_exists($document)){
throw $this->createNotFoundException();
}
// save
if ($request->get('action') == "save") {
return $this->file($document);
}
return $this->file($document, null, ResponseHeaderBag::DISPOSITION_INLINE);
}
public function documentParams(Document $document){
$path = sprintf("%s/%s",
$this->getParameter("documents_directory"),
$document->getId()
);
if (!file_exists($path)){
return false;
}
// load the file from the filesystem
return new File($path);
}
/**
* @param Document $document
* @param string $for
* @return Response|string
*/
public function downloadLogo(Document $document, string $for = 'html'){
$file = $this->documentParams($document);
$logo = sprintf("data:%s;base64,%s",
$file->getMimeType(),
base64_encode(file_get_contents($file->getPathname()))
);
if ($for === 'html')
return new Response($logo,Response::HTTP_OK);
return $logo;
}
}