src/Controller/ImagesController.php line 200

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use DateTime;
  4. use App\Entity\Images;
  5. use App\Service\Securizer;
  6. use App\Library\GraphOneDrive;
  7. use App\Repository\ImagesRepository;
  8. use Microsoft\Graph\Model\DriveItem;
  9. use App\Repository\ClientsRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Serializer\SerializerInterface;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  17. class ImagesController extends AbstractController
  18. {
  19.     /**
  20.      * @IsGranted("ROLE_TECH")
  21.      */
  22.     public function index(int $idImagesRepository $repoImagesClientsRepository $repoClientEntityManagerInterface $manager): Response
  23.     {
  24.         // Synchronisation et récupération des images d'un client
  25.         $graph = new GraphOneDrive();
  26.         $client $repoClient->find($id);
  27.         $images $repoImages->findBy(['idClient' => $id]);
  28.         $idDrive "b!0B8l_7Yi_U2bP-TRWI8AGy-WjI76ekZLrXGgrRJbi4d9LH8_adGXT6XMhZMdfqIi";
  29.         $filesOneDrive $graph->listContentFolder($idDrive$client->getGraphFolderId());
  30.         if (isset($filesOneDrive)) {
  31.             // S'il y a de nouvelles images, ajoutez-les
  32.             $addedImages array_filter($filesOneDrive, function (DriveItem $file) use ($images) {
  33.                 if ($file->getFile() == null) return false;
  34.                 if ($file->getFile()->getMimeType() != "image/jpeg" &&  $file->getFile()->getMimeType() != "image/jpg" && $file->getFile()->getMimeType() != "image/png") return false;
  35.                 foreach ($images as $img) {
  36.                     if ($file->getId() === $img->getDriveId()) return false;
  37.                 }
  38.                 return true;
  39.             });
  40.             foreach ($addedImages as $add) {
  41.                 $entity = new Images();
  42.                 $today = new DateTime();
  43.                 $entity->setIdClient($client)
  44.                     ->setUploadedDate($today)
  45.                     ->setDriveId($add->getId());
  46.                 $manager->persist($entity);
  47.             }
  48.             // Les images supprimées dans Onedrive, supprimez-les dans l'extranet aussi
  49.             $deletedImages array_filter($images, function (Images $img) use ($filesOneDrive) {
  50.                 foreach ($filesOneDrive as $file) {
  51.                     if ($file->getId() === $img->getDriveId()) return false;
  52.                 }
  53.                 return true;
  54.             });
  55.             foreach ($deletedImages as $image) {
  56.                 $repoImages->remove($imagetrue);
  57.             }
  58.             $manager->flush();
  59.         }
  60.         $images $repoImages->findBy(['idClient' => $id]);
  61.         return $this->json($images200, [], ['groups' => 'affichageImage']);
  62.     }
  63.     public function upload(Request $requestEntityManagerInterface $managerClientsRepository $repoClientAccessDecisionManagerInterface $accessDecisionManager): Response
  64.     {
  65.         $securizer = new Securizer($accessDecisionManager);
  66.         if (!$securizer->isGranted($this->getUser(), "ROLE_TECH"))
  67.             return $this->json([
  68.                 'status' => 400,
  69.                 'message' => "Vous ne pouvez pas consulter cet élément"
  70.             ], 400);
  71.         try {
  72.             $file $request->files->get('myFile');
  73.             $graphOneDrive = new GraphOneDrive();
  74.             $client $repoClient->find($_POST["idClient"]);
  75.             $folderGraphId $client->getGraphFolderId();
  76.             if (!isset($folderGraphId)) {
  77.                 return $this->json([
  78.                     'status' => 400,
  79.                     'message' => "There isn't a graph folder id for the selected client"
  80.                 ], 400);
  81.             }
  82.             $path dirname(__DIR__) . '/../documents/images/';
  83.             if (!is_dir($path))
  84.                 mkdir($path0777true);
  85.             $fileName $_FILES["myFile"]["name"];
  86.             // ** Télécharger le fichier
  87.             if (!empty($file)) {
  88.                 $file->move(
  89.                     $path,
  90.                     $fileName
  91.                 );
  92.             }
  93.             $path $path $fileName;
  94.             $filesize filesize($path);
  95.             $fp fopen($path'rb');
  96.             $binary fread($fp$filesize);
  97.             fclose($fp);
  98.             $idDrive "b!0B8l_7Yi_U2bP-TRWI8AGy-WjI76ekZLrXGgrRJbi4d9LH8_adGXT6XMhZMdfqIi";
  99.             $fileAdded $graphOneDrive->uploadFile($idDrive$folderGraphId$fileName$binary);
  100.             $entity = new Images();
  101.             $today = new DateTime();
  102.             $entity->setIdClient($client)->setUploadedDate($today)->setDriveId($fileAdded->getId());
  103.             $manager->persist($entity);
  104.             $manager->flush();
  105.             unlink($path);
  106.         } catch (\throwable $e) {
  107.             return $this->json([
  108.                 'status' => 400,
  109.                 'message' => $e->getMessage()
  110.             ], 400);
  111.         }
  112.         return $this->json($entity201, [], ['groups' => 'affichageElement']);
  113.     }
  114.     /**
  115.      * @IsGranted("ROLE_TECH")
  116.      */
  117.     public function modif($idRequest $requestImagesRepository $repoImagesAccessDecisionManagerInterface $accessDecisionManagerEntityManagerInterface $managerSerializerInterface $serializer): Response
  118.     {
  119.         //permets de effacer une image d'un client, seul les roles TECH et supperieur peuvent acceder a cette url
  120.         $securizer = new Securizer($accessDecisionManager);
  121.         if ($securizer->isGranted($this->getUser(), "ROLE_TECH")) {
  122.             $image $repoImages->find($id);
  123.             $jsonRecu $request->getContent();
  124.             //transforme le json reçu en entity
  125.             $serializer->deserialize($jsonRecuImages::class, 'json', ['object_to_populate' => $image]);
  126.             $manager->persist($image);
  127.             $manager->flush();
  128.         } else {
  129.             return $this->json([
  130.                 'status' => 400,
  131.                 'message' => "Vous ne pouvez pas modifier cet élément"
  132.             ], 400);
  133.         }
  134.         return $this->json(true200, []);
  135.     }
  136.     /**
  137.      * @IsGranted("ROLE_TECH")
  138.      */
  139.     public function delete($idImagesRepository $repoImagesAccessDecisionManagerInterface $accessDecisionManager): Response
  140.     {
  141.         //permets de effacer une image d'un client, seul les roles TECH et supperieur peuvent acceder a cette url
  142.         $securizer = new Securizer($accessDecisionManager);
  143.         if ($securizer->isGranted($this->getUser(), "ROLE_TECH")) {
  144.             // recuperer id de sharepoint
  145.             $image $repoImages->find($id);
  146.             // faire delete image en sharepoint
  147.             $graphOneDrive = new GraphOneDrive();
  148.             $idDrive "b!0B8l_7Yi_U2bP-TRWI8AGy-WjI76ekZLrXGgrRJbi4d9LH8_adGXT6XMhZMdfqIi";
  149.             $graphOneDrive->deleteFile($idDrive$image->getDriveId());
  150.             // si ça marche bine supprimer l'entity
  151.             $repoImages->remove($imagetrue);
  152.         } else {
  153.             return $this->json([
  154.                 'status' => 400,
  155.                 'message' => "Vous ne pouvez pas consulter cet élément"
  156.             ], 400);
  157.         }
  158.         return $this->json(true200, [], ['groups' => 'affichageImage']);
  159.     }
  160.     /**
  161.      * @IsGranted("ROLE_TECH")
  162.      */
  163.     public function getBase64Image($id)
  164.     {
  165.         $idDrive "b!0B8l_7Yi_U2bP-TRWI8AGy-WjI76ekZLrXGgrRJbi4d9LH8_adGXT6XMhZMdfqIi";
  166.         $graph = new GraphOneDrive();
  167.         $item $graph->getFolderDrive($idDrive$id);
  168.         $path $item->getProperties()['@microsoft.graph.downloadUrl'];
  169.         $context stream_context_create(array('http' => array('header' => 'Connection: close\r\n')));
  170.         header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T'time() + (60 60 24))); 
  171.         readfile($pathfalse$context);
  172.     }
  173.     /**
  174.      * @IsGranted("ROLE_ADMIN")
  175.      */
  176.     public function listFoldersClients($idFolderAccessDecisionManagerInterface $accessDecisionManager): Response
  177.     {
  178.         $securizer = new Securizer($accessDecisionManager);
  179.         if (!$securizer->isGranted($this->getUser(), "ROLE_ADMIN"))
  180.             return $this->json([
  181.                 'status' => 400,
  182.                 'message' => "Vous ne pouvez pas consulter cet élément"
  183.             ], 400);
  184.         $graphOneDrive = new GraphOneDrive();
  185.         if ($idFolder == "null")
  186.             $data $graphOneDrive->listFolderDrive("b!0B8l_7Yi_U2bP-TRWI8AGy-WjI76ekZLrXGgrRJbi4d9LH8_adGXT6XMhZMdfqIi");
  187.         else
  188.             $data $graphOneDrive->listContentFolder("b!0B8l_7Yi_U2bP-TRWI8AGy-WjI76ekZLrXGgrRJbi4d9LH8_adGXT6XMhZMdfqIi"$idFolder);
  189.         return $this->json($data200, []);
  190.     }
  191. }