2022-08-24 14:40:10 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
use App\Service\MinioService;
|
2023-02-01 09:03:27 +01:00
|
|
|
use Doctrine\Persistence\ManagerRegistry;
|
2022-08-24 14:40:10 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2023-01-10 11:13:28 +01:00
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
2022-08-24 14:40:10 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\HttpKernel\KernelInterface;
|
|
|
|
|
|
|
|
class MinioController extends AbstractController
|
|
|
|
{
|
2022-09-23 14:53:47 +02:00
|
|
|
private $appKernel;
|
2022-08-24 14:40:10 +02:00
|
|
|
private $minio;
|
|
|
|
|
|
|
|
public function __construct(KernelInterface $appKernel, MinioService $minio)
|
|
|
|
{
|
|
|
|
$this->appKernel = $appKernel;
|
|
|
|
$this->minio = $minio;
|
|
|
|
}
|
|
|
|
|
2023-02-01 09:03:27 +01:00
|
|
|
public function ckupload($access, $category, $id, $usage, Request $request, ManagerRegistry $em): Response
|
2022-08-24 14:40:10 +02:00
|
|
|
{
|
|
|
|
// Fichier temporaire uploadé
|
2022-09-23 16:14:15 +02:00
|
|
|
$tmpfile = $request->files->get('upload');
|
|
|
|
$extention = $tmpfile->getClientOriginalExtension();
|
2022-08-24 14:40:10 +02:00
|
|
|
|
|
|
|
// Fichier cible
|
2022-09-23 16:14:15 +02:00
|
|
|
$targetName = uniqid().'.'.$extention;
|
2023-02-01 09:03:27 +01:00
|
|
|
if ('all' == $category) {
|
|
|
|
$targetFile = 'ckeditor/'.$targetName;
|
|
|
|
} else {
|
|
|
|
if ('pagewidget' == $category) {
|
|
|
|
$pagewidget = $em->getRepository("App\Entity\Pagewidget")->find($id);
|
|
|
|
if (!$em->getRepository("App\Entity\Pagewidget")->getCanadd($this->getUser(), $pagewidget, $usage)) {
|
|
|
|
$this->createAccessDeniedException('Permission denied');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$targetFile = 'file/'.$category.'/'.$id.'/'.$targetName;
|
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
|
2023-02-01 09:03:27 +01:00
|
|
|
$targetUrl = $this->generateUrl('app_minio_document', ['file' => $targetFile]);
|
2022-09-23 16:14:15 +02:00
|
|
|
$this->minio->upload($tmpfile, $targetFile, true);
|
2022-08-24 14:40:10 +02:00
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
$output['uploaded'] = 1;
|
|
|
|
$output['fileName'] = $targetName;
|
|
|
|
$output['url'] = $targetUrl;
|
2022-08-24 14:40:10 +02:00
|
|
|
|
|
|
|
return new Response(json_encode($output));
|
|
|
|
}
|
|
|
|
|
2022-09-23 16:14:15 +02:00
|
|
|
public function logo(Request $request): Response
|
|
|
|
{
|
|
|
|
return $this->redirectToRoute('app_minio_image', ['file' => 'logo/'.$request->getSession()->get('logolight')]);
|
2022-08-27 18:24:56 +02:00
|
|
|
}
|
|
|
|
|
2023-02-01 09:03:27 +01:00
|
|
|
public function image(Request $request, ManagerRegistry $em): Response
|
|
|
|
{
|
|
|
|
$file = $request->query->get('file');
|
|
|
|
|
|
|
|
return $this->returnminio($file, $em);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function document(Request $request, ManagerRegistry $em)
|
2022-08-24 14:40:10 +02:00
|
|
|
{
|
2022-09-23 16:14:15 +02:00
|
|
|
$file = $request->query->get('file');
|
2023-02-01 09:03:27 +01:00
|
|
|
|
|
|
|
return $this->returnminio($file, $em);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function returnminio($file, $em)
|
|
|
|
{
|
2022-09-23 16:14:15 +02:00
|
|
|
switch ($file) {
|
|
|
|
case 'avatar/admin.jpg':
|
|
|
|
case 'avatar/noavatar.png':
|
|
|
|
case 'avatar/system.jpg':
|
|
|
|
case 'header/header.jpg':
|
|
|
|
case 'logo/logo.png':
|
|
|
|
$file = 'medias/'.$file;
|
2022-08-24 14:40:10 +02:00
|
|
|
$filePath = $file;
|
2023-01-10 11:13:28 +01:00
|
|
|
$response = new BinaryFileResponse($filePath);
|
|
|
|
$response->headers->set('Content-Type', mime_content_type($filePath));
|
|
|
|
|
|
|
|
return $response;
|
2022-09-23 16:14:15 +02:00
|
|
|
break;
|
2022-08-24 14:40:10 +02:00
|
|
|
|
|
|
|
default:
|
2023-01-10 11:13:28 +01:00
|
|
|
// Icons ?
|
|
|
|
if (0 === stripos($file, 'icon/icon_')) {
|
|
|
|
$file = 'medias/'.$file;
|
|
|
|
$filePath = $file;
|
|
|
|
$response = new BinaryFileResponse($filePath);
|
|
|
|
$response->headers->set('Content-Type', mime_content_type($filePath));
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2023-02-01 09:03:27 +01:00
|
|
|
// Slide = on s'assure que le user puisse voir
|
|
|
|
elseif (0 === stripos($file, 'slide/')) {
|
|
|
|
$idwidget = explode('/', $file)[1];
|
|
|
|
$pagewidget = $em->getRepository("App\Entity\Pagewidget")->find($idwidget);
|
|
|
|
if (!$em->getRepository("App\Entity\Pagewidget")->getCansee($this->getUser(), $pagewidget)) {
|
|
|
|
$this->createAccessDeniedException('Permission denied');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Response($this->minio->image($file, $file));
|
|
|
|
}
|
|
|
|
// Pagewidgtet = on s'assure que le user puisse voir
|
|
|
|
elseif (0 === stripos($file, 'file/pagewidget/')) {
|
|
|
|
$idwidget = explode('/', $file)[2];
|
|
|
|
$pagewidget = $em->getRepository("App\Entity\Pagewidget")->find($idwidget);
|
|
|
|
if (!$em->getRepository("App\Entity\Pagewidget")->getCansee($this->getUser(), $pagewidget)) {
|
|
|
|
$this->createAccessDeniedException('Permission denied');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Response($this->minio->image($file, $file));
|
|
|
|
}
|
2022-08-24 14:40:10 +02:00
|
|
|
// C'est une url = on affiche l'url
|
2023-01-10 11:13:28 +01:00
|
|
|
elseif (0 === stripos($file, 'http')) {
|
2022-08-24 14:40:10 +02:00
|
|
|
$filePath = $file;
|
|
|
|
$content = file_get_contents($file);
|
|
|
|
}
|
|
|
|
// C'est du contenu dynamique on download depuis minio
|
2022-08-31 11:20:22 +02:00
|
|
|
else {
|
2022-10-24 13:47:43 +02:00
|
|
|
return new Response($this->minio->image($file, $file));
|
2022-08-24 14:40:10 +02:00
|
|
|
}
|
2022-09-23 16:14:15 +02:00
|
|
|
break;
|
2022-08-24 14:40:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|