nineskeletor/src/Controller/MinioController.php

102 lines
3.3 KiB
PHP

<?php
namespace App\Controller;
use App\Service\MinioService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
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, 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').'/var/tmp';
$fs->mkdir($rootdir.'/ckeditor');
// Fichier cible
$targetName = uniqid().'.'.$extention;
$targetFile = 'ckeditor/'.$targetName;
$targetUrl = $this->generateUrl('app_minio_document', ['file' => 'ckeditor/'.$targetName]);
// 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 logo(Request $request): Response
{
return $this->redirectToRoute('app_minio_image', ['file' => 'logo/'.$request->getSession()->get('logolight')]);
}
public function image(Request $request): Response
{
$file = $request->query->get('file');
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;
$filePath = $file;
$content = file_get_contents($file);
break;
default:
// C'est une url = on affiche l'url
if (0 === stripos($file, 'http')) {
$filePath = $file;
$content = file_get_contents($file);
}
// C'est du contenu dynamique on download depuis minio
else {
$filePath = $this->minio->download($file, $file, true);
$content = file_get_contents($filePath);
}
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),
]);
}
}