use cache for minio

This commit is contained in:
Arnaud Fornerot 2022-08-31 12:05:06 +02:00
parent d539a2a740
commit 50322de0ff
1 changed files with 22 additions and 34 deletions

View File

@ -30,7 +30,7 @@ class MinioService
$this->initBucket(); $this->initBucket();
} }
public function download(string $file, string $filename, bool $returnFile = false) public function download(string $file, string $filename, bool $usecache = true)
{ {
// On s'assure que le repertoire temporaire de destination existe bien // On s'assure que le repertoire temporaire de destination existe bien
$fs = new Filesystem(); $fs = new Filesystem();
@ -38,42 +38,30 @@ class MinioService
$fs->mkdir($tmpdir."/".dirname($filename)); $fs->mkdir($tmpdir."/".dirname($filename));
// Approche repassant par le serveur d'appel // Approche repassant par le serveur d'appel
try { if(!$usecache||!$fs->exists($tmpdir.'/'.$filename)) {
$result = $this->client->getObject([ try {
'Bucket' => $this->minioBucket, $result = $this->client->getObject([
'Key' => $this->minioRoot.$file, 'Bucket' => $this->minioBucket,
'SaveAs' => $tmpdir.'/'.$filename, 'Key' => $this->minioRoot.$file,
]); 'SaveAs' => $tmpdir.'/'.$filename,
} catch (S3Exception $e) { ]);
switch ($e->getResponse()->getStatusCode()) { } catch (S3Exception $e) {
case 404: switch ($e->getResponse()->getStatusCode()) {
throw new NotFoundHttpException($this->translator->trans(self::ERR_FILE_NOT_FOUND, [], 'messages')); case 404:
break; throw new NotFoundHttpException($this->translator->trans(self::ERR_FILE_NOT_FOUND, [], 'messages'));
default: break;
\Sentry\captureException($e); default:
throw new Exception(self::ERR_UNAVAILABLE); \Sentry\captureException($e);
break; throw new Exception(self::ERR_UNAVAILABLE);
break;
}
} catch (Exception $e) {
\Sentry\captureException($e);
throw new Exception(self::ERR_UNAVAILABLE);
} }
} catch (Exception $e) {
\Sentry\captureException($e);
throw new Exception(self::ERR_UNAVAILABLE);
} }
if ($returnFile) { return $tmpdir.'/'.$filename;
return $tmpdir.'/'.$filename;
} else {
//Suppression de la copie locale
unlink($tmpdir.'/'.$filename);
$res = new Response($result['Body'], 200);
$res->headers->set('Content-Type', 'application/pdf');
$res->headers->set('Content-Description', 'File Transfer');
$res->headers->set('Content-Disposition', 'attachment; filename='.$filename);
$res->headers->set('Expires', '0');
$res->headers->set('Cache-Control', 'must-revalidate');
$res->headers->set('Pragma', 'public');
return $res;
}
} }
public function upload($file, $filename, $deleteSource = false) public function upload($file, $filename, $deleteSource = false)