nineskeletor/src/Controller/MinioController.php

158 lines
6.0 KiB
PHP
Executable File

<?php
namespace App\Controller;
use App\Service\MinioService;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpKernel\KernelInterface;
class MinioController extends AbstractController
{
private $appKernel;
private $minio;
public function __construct(KernelInterface $appKernel, MinioService $minio)
{
$this->appKernel = $appKernel;
$this->minio = $minio;
}
public function ckupload($access, $category, $id, $usage, Request $request, ManagerRegistry $em): Response
{
// Fichier temporaire uploadé
$tmpfile = $request->files->get('upload');
$extention = $tmpfile->getClientOriginalExtension();
// Fichier cible
$targetName = uniqid().'.'.$extention;
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;
}
$targetUrl = $this->generateUrl('app_minio_document', ['file' => $targetFile]);
$this->minio->upload($tmpfile, $targetFile, true);
$output['uploaded'] = 1;
$output['fileName'] = $targetName;
$output['url'] = $targetUrl;
return new Response(json_encode($output));
}
public function logo(Request $request): Response
{
return $this->redirectToRoute('app_minio_image', ['file' => 'logo/'.$request->getSession()->get('logolight')]);
}
public function image(Request $request, ManagerRegistry $em): Response
{
$file = $request->query->get('file');
return $this->returnminio($file, $em);
}
public function show(Request $request, ManagerRegistry $em): Response
{
$file = $request->query->get('file');
$tmpfile = $this->minio->download($file, basename($file));
if (str_starts_with(mime_content_type($tmpfile), 'image/') || 'application/pdf' == mime_content_type($tmpfile) || 'text/plain' == mime_content_type($tmpfile)) {
$response = new BinaryFileResponse($tmpfile);
$response->headers->set('Content-Type', mime_content_type($tmpfile));
} else {
$response = new BinaryFileResponse($tmpfile);
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, basename($file));
}
return $response;
}
public function download(Request $request, ManagerRegistry $em): Response
{
$file = $request->query->get('file');
// C'est une url = on affiche l'url
if (0 === stripos($file, 'http')) {
$tmpfile = $file;
} else {
$tmpfile = $this->minio->download($file, basename($file));
}
$response = new BinaryFileResponse($tmpfile);
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, basename($file));
return $response;
}
private function returnminio($file, $em)
{
switch ($file) {
case 'avatar/admin.jpg':
case 'avatar/noavatar.png':
case 'avatar/system.jpg':
case 'header/header.jpg':
case 'header/body.jpg':
case 'logo/logo.png':
$file = 'medias/'.$file;
$filePath = $file;
$response = new BinaryFileResponse($filePath);
$response->headers->set('Content-Type', mime_content_type($filePath));
return $response;
break;
default:
// 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;
}
// 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));
}
// C'est une url = on affiche l'url
elseif (0 === stripos($file, 'http')) {
$filePath = $file;
$content = file_get_contents($file);
}
// C'est du contenu dynamique on download depuis minio
else {
return new Response($this->minio->image($file, $file));
}
break;
}
}
}