Advertisement
Laszlooo

Symfony VideosController

Jan 31st, 2022
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.48 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Controller\Pages;
  4.  
  5. use App\Entity\Db_Products;
  6. use App\Entity\Db_Timestamps;
  7. use App\Entity\Db_Transcripts;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Doctrine\ORM\PersistentCollection;
  13. use Symfony\Component\HttpFoundation\Request;
  14.  
  15. class VideosController extends AbstractController {
  16.    
  17.     /**
  18.      * @Route("/products/{id}", name="a_products")
  19.      */
  20.     public function index(int $id, UserInterface $userInterface = null, Request $request) {
  21.  
  22.         header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
  23.         header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  24.         header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
  25.         header("Cache-Control: post-check=0, pre-check=0", false);
  26.         header("Pragma: no-cache");
  27.         header("Connection: close");
  28.        
  29.         if ($product = $this->getDoctrine()->getRepository(Db_Products::class)->find($id)) {
  30.             if ($product->getPublic()) {
  31.                 $file = $this->getParameter("kernel.project_dir") . "/products/" . $product->getPath() . "/" . $product->getFile();
  32.                 session_write_close();
  33.                 header('Content-Type: '.mime_content_type($file));
  34.                 header('Content-Length:' . filesize($file));
  35.                 return (new VideoStream($file))->start();
  36.             }
  37.  
  38.             if ($userInterface) {
  39.                 if ($userInterface->getRoles()[0] == "ROLE_USER") {
  40.                     if (!$request->get("video")) {
  41.                         $file = $this->getParameter("kernel.project_dir") . "/products/" . $product->getPath() . "/" . $product->getFile();
  42.                         session_write_close();
  43.                         header('Content-Type: '.mime_content_type($file));
  44.                         header('Content-Length:' . filesize($file));
  45.                         return (new VideoStream($file))->start();
  46.                     }
  47.  
  48.                     /**
  49.                      * @var PersistentCollection
  50.                      */
  51.                     $users = $product->getUsers();
  52.  
  53.                      
  54.                     foreach ($users as $user) {
  55.                         if ($user == $userInterface) {
  56.                             $file = $this->getParameter("kernel.project_dir") . "/products/" . $product->getPath() . "/" . $product->getFile();
  57.  
  58.                             if ($videoId = $request->get("video")) {
  59.                                 /**
  60.                                  * @var Db_Timestamps
  61.                                  */
  62.                                 $video = $this->getDoctrine()->getRepository(Db_Timestamps::class)->find($videoId);
  63.                                 $file = $this->getParameter("kernel.project_dir") . "/products/" . $product
  64.                                             ->getPath() . "/" . $video->getVideoName();
  65.                             }                            
  66.                             session_write_close();
  67.                             header('Content-Type: '.mime_content_type($file));
  68.                             header('Content-Length:' . filesize($file));
  69.                             return (new VideoStream($file))->start();
  70.                         }
  71.                     }
  72.                 }
  73.             }
  74.         }
  75.         return new Response('Private video', 500);
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement