mise en place de minio
This commit is contained in:
101
src/Controller/MinioController.php
Normal file
101
src/Controller/MinioController.php
Normal file
@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use App\Service\MinioService;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
|
||||
class MinioController extends AbstractController
|
||||
{
|
||||
private $minio;
|
||||
|
||||
public function __construct(KernelInterface $appKernel, MinioService $minio)
|
||||
{
|
||||
$this->appKernel = $appKernel;
|
||||
$this->minio = $minio;
|
||||
}
|
||||
|
||||
public function ckupload($access,Request $request): Response
|
||||
{
|
||||
// Fichier temporaire uploadé
|
||||
$tmpfile = $request->files->get('upload');
|
||||
$extention = $tmpfile->getClientOriginalExtension();
|
||||
|
||||
// Répertoire de Destination
|
||||
$fs = new Filesystem();
|
||||
$rootdir = $this->getParameter('kernel.project_dir') . '/public';
|
||||
$fs->mkdir($rootdir."/uploads/ckeditor");
|
||||
|
||||
// Fichier cible
|
||||
$targetName = uniqid().".".$extention;
|
||||
$targetFile = "uploads/ckeditor/".$targetName;
|
||||
$targetUrl = $this->getParameter('appAlias')."uploads/ckeditor/".$targetName;
|
||||
$targetUrl = $this->generateUrl('app_minio_document',["file"=>"uploads/ckeditor/".$targetName]);
|
||||
$message = "";
|
||||
|
||||
//move_uploaded_file($tmpfile,$targetFile);
|
||||
$this->minio->upload($tmpfile,$targetFile,true);
|
||||
|
||||
$output["uploaded"]=1;
|
||||
$output["fileName"]=$targetName;
|
||||
$output["url"]=$targetUrl;
|
||||
|
||||
return new Response(json_encode($output));
|
||||
}
|
||||
|
||||
public function image(Request $request): Response
|
||||
{
|
||||
$file=$request->query->get("file");
|
||||
switch($file) {
|
||||
case "uploads/avatar/admin.jpg":
|
||||
case "uploads/avatar/noavatar.png":
|
||||
case "uploads/avatar/system.jpg":
|
||||
case "uploads/header/header.jpg":
|
||||
case "uploads/logo/logo.png":
|
||||
$filePath = $file;
|
||||
$content = file_get_contents($file);
|
||||
break;
|
||||
|
||||
default:
|
||||
// C'est une url = on affiche l'url
|
||||
if(stripos($file,"http")===0) {
|
||||
$filePath = $file;
|
||||
$content = file_get_contents($file);
|
||||
}
|
||||
// C'est du contenu dynamique on download depuis minio
|
||||
elseif(stripos($file,"uploads")===0) {
|
||||
$filePath = $this->minio->download($file, $file, true);
|
||||
$content = file_get_contents($filePath);
|
||||
}
|
||||
// C'est du contenu statique on download depuis minio
|
||||
else {
|
||||
$filePath = $file;
|
||||
$content = file_get_contents($file);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return new Response($content, 200, [
|
||||
'Content-Type' => mime_content_type($filePath),
|
||||
'Cache-Control' => 'max-age='.(60 * 60 * 24 * 7),
|
||||
'Expires' => gmdate(DATE_RFC1123, time() + 60 * 60 * 24 * 365),
|
||||
]);
|
||||
}
|
||||
|
||||
public function document(Request $request)
|
||||
{
|
||||
$file=$request->query->get("file");
|
||||
$filePath = $this->minio->download($file, $file, true);
|
||||
$content = file_get_contents($filePath);
|
||||
|
||||
return new Response($content, 200, [
|
||||
'Content-Type' => mime_content_type($filePath),
|
||||
'Cache-Control' => 'max-age='.(60 * 60 * 24 * 7),
|
||||
'Expires' => gmdate(DATE_RFC1123, time() + 60 * 60 * 24 * 365),
|
||||
]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user