<?php
namespace App\Controller;
use App\Entity\File\Biblios;
use App\Entity\File\OSFile;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use OlaSoft\Common;
class OSFileController extends AbstractController
{
public function __construct(private ManagerRegistry $registry){}
/**
* @Route("/doc/{id}/read", name="file-read", defaults={"isRead":1})
* @Route("/doc/{id}/download", name="file-download")
*/
public function download(OSFile $doc = null, $isRead = 0){
$em = $this->registry->getManager();
if(!$doc) throw $this->createNotFoundException('Le document que vous demandez n\'est pas disponible.');
if($isRead) {
$doc->setReading($doc->getReading() + 1);
}
else {
$doc->setDownloads($doc->getDownloads() + 1);
}
$em->persist($doc);
$em->flush();
$extension = strtolower(pathinfo($doc->getFile(), PATHINFO_EXTENSION));
$name= strtolower(pathinfo($doc->getFile(), PATHINFO_FILENAME));
$filename = $doc->getFile();
$response = new Response();
$response->headers->set('Cache-Control', 'private');
$response->headers->set('Content-type', mime_content_type($filename));
$response->headers->set('Content-Disposition', 'attachment; filename="' . Common::slug($name) .'.'.$extension.'";');
$response->headers->set('Content-length', filesize($filename));
$response->setContent(file_get_contents($filename));
return $response;
}
/**
* @Route("/biblio/{id}/", name="biblio")
* @Route("/biblio/{id}/read", name="biblio-read")
*/
public function biblio(Biblios $o)
{
$list = [];
foreach ($o->getFiles() as $p) {
$list[] = $p->getFile();
}
return new Response(implode(',',$list));
}
/**
* @Route("/biblio/{id}/post", name="doc-post")
*/
public function docPost(Biblios $o)
{
$list = [];
foreach ($o->getFiles() as $p) {
$list[] = $p->getDocument()->getFile();
}
return new Response(implode(',',$list));
}
}