From 928109fdf5cbe7a898a4ef8377033a9c36b0bda2 Mon Sep 17 00:00:00 2001 From: afornerot Date: Mon, 24 Oct 2022 13:47:43 +0200 Subject: [PATCH] fix(fixminio): stream image from minio (#6) fix load image from minio Co-authored-by: Arnaud Fornerot Reviewed-on: https://forge.cadoles.com/Cadoles/nineskeletor/pulls/6 --- src/Controller/MinioController.php | 3 +-- src/Service/MinioService.php | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Controller/MinioController.php b/src/Controller/MinioController.php index 8470394..e4c3d95 100644 --- a/src/Controller/MinioController.php +++ b/src/Controller/MinioController.php @@ -73,8 +73,7 @@ class MinioController extends AbstractController } // C'est du contenu dynamique on download depuis minio else { - $filePath = $this->minio->download($file, $file, true); - $content = file_get_contents($filePath); + return new Response($this->minio->image($file, $file)); } break; } diff --git a/src/Service/MinioService.php b/src/Service/MinioService.php index d963f83..92c7edf 100644 --- a/src/Service/MinioService.php +++ b/src/Service/MinioService.php @@ -30,6 +30,31 @@ class MinioService $this->initBucket(); } + public function image(string $file, string $filename) + { + try { + $result = $this->client->getObject([ + 'Bucket' => $this->minioBucket, + 'Key' => $this->minioRoot.$file, + ]); + } catch (S3Exception $e) { + switch ($e->getResponse()->getStatusCode()) { + case 404: + throw new NotFoundHttpException(self::ERR_FILE_NOT_FOUND); + break; + default: + \Sentry\captureException($e); + throw new Exception(self::ERR_UNAVAILABLE); + break; + } + } catch (Exception $e) { + \Sentry\captureException($e); + throw new Exception(self::ERR_UNAVAILABLE); + } + + return $result->get('Body'); + } + public function download(string $file, string $filename, bool $usecache = true) { // On s'assure que le repertoire temporaire de destination existe bien