src/Controller/OSFileController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\File\Biblios;
  4. use App\Entity\File\OSFile;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use OlaSoft\Common;
  10. class OSFileController extends AbstractController
  11. {
  12.     public function __construct(private ManagerRegistry $registry){}
  13.     /**
  14.      * @Route("/doc/{id}/read", name="file-read", defaults={"isRead":1})
  15.      * @Route("/doc/{id}/download", name="file-download")
  16.      */
  17.     public function download(OSFile $doc null$isRead 0){
  18.         $em $this->registry->getManager();
  19.         if(!$doc) throw $this->createNotFoundException('Le document que vous demandez n\'est pas disponible.');
  20.         if($isRead) {
  21.             $doc->setReading($doc->getReading() + 1);
  22.         }
  23.         else {
  24.             $doc->setDownloads($doc->getDownloads() + 1);
  25.         }
  26.         $em->persist($doc);
  27.         $em->flush();
  28.         $extension strtolower(pathinfo($doc->getFile(), PATHINFO_EXTENSION));
  29.         $namestrtolower(pathinfo($doc->getFile(), PATHINFO_FILENAME));
  30.         $filename $doc->getFile();
  31.         $response = new Response();
  32.         $response->headers->set('Cache-Control''private');
  33.         $response->headers->set('Content-type'mime_content_type($filename));
  34.         $response->headers->set('Content-Disposition''attachment; filename="' Common::slug($name) .'.'.$extension.'";');
  35.         $response->headers->set('Content-length'filesize($filename));
  36.         $response->setContent(file_get_contents($filename));
  37.         return $response;
  38.     }
  39.     /**
  40.      * @Route("/biblio/{id}/", name="biblio")
  41.      * @Route("/biblio/{id}/read", name="biblio-read")
  42.      */
  43.     public function biblio(Biblios $o)
  44.     {
  45.         $list = [];
  46.         foreach ($o->getFiles() as $p) {
  47.             $list[] = $p->getFile();
  48.         }
  49.         return new Response(implode(',',$list));
  50.     }
  51.     /**
  52.      * @Route("/biblio/{id}/post", name="doc-post")
  53.      */
  54.     public function docPost(Biblios $o)
  55.     {
  56.         $list = [];
  57.         foreach ($o->getFiles() as $p) {
  58.             $list[] = $p->getDocument()->getFile();
  59.         }
  60.         return new Response(implode(',',$list));
  61.     }
  62. }