<?php
namespace App\Controller\ApiController;
use App\Repository\CorpusRepository;
use App\Repository\ImageRepository;
use App\Repository\ProjectRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
class DownloadController extends AbstractController
{
private $imageRepository;
private $corpusRepository;
private $projectRepository;
public function __construct(ImageRepository $imageRepository, EntityManagerInterface $manager, CorpusRepository $corpusRepository, ProjectRepository $projectRepository)
{
$this->corpusRepository = $corpusRepository;
$this->imageRepository = $imageRepository;
$this->projectRepository = $projectRepository;
$this->em = $manager;
}
/**
* @Route("/api/image/{quality}/{id}", name="image_download", methods={"GET"})
*/
public function image_download($quality,$id): BinaryFileResponse
{
if($quality != "highres" && $quality != "thumbnails"){
throw new NotFoundHttpException('Error : quality format invalid');
}
$image = $this->imageRepository->find($id);
if($image == null){
throw new NotFoundHttpException('Error : invalid ID');
}
if($quality == "highres"){
return $this->file(
new File($this->getParameter('CorpusPath')."/highres/".$image->getHighres()),
$image->getHighres()
);
} else {
return $this->file(
new File($this->getParameter('CorpusPath')."/thumbnails/".$image->getThumbnails()),
$image->getThumbnails()
);
}
}
/**
* @Route("/api/corpus/download/{id}", name="zip_download", methods={"GET"})
*/
public function zip_download($id): BinaryFileResponse
{
if(!is_dir($this->getParameter('CorpusPath').'/export')){
mkdir($this->getParameter('CorpusPath').'/export');
}
$corpus = $this->corpusRepository->find($id);
if($corpus == null){
throw new NotFoundHttpException('Error : invalid ID');
}
$zipPath = $this->corpusRepository->createZip($corpus,$this->getParameter('CorpusPath'));
return $this->file(
new File($zipPath),
$corpus->getTitleFr().'.zip'
);
}
/**
* @Route("/api/project/download/{code}", name="project_download", methods={"GET"})
*/
public function project_download($code): BinaryFileResponse
{
$project = $this->projectRepository->findOneBy(['code' => strtoupper($code)]);
if($project == null){
throw new NotFoundHttpException('Error : invalid code');
}
return $this->file(
new File($this->getParameter('ProjectPath')."/".$project->getFileName()),
$project->getTitle().'.bdnf'
);
}
}