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();
}
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
$fs = new Filesystem();
@ -38,42 +38,30 @@ class MinioService
$fs->mkdir($tmpdir."/".dirname($filename));
// Approche repassant par le serveur d'appel
try {
$result = $this->client->getObject([
'Bucket' => $this->minioBucket,
'Key' => $this->minioRoot.$file,
'SaveAs' => $tmpdir.'/'.$filename,
]);
} catch (S3Exception $e) {
switch ($e->getResponse()->getStatusCode()) {
case 404:
throw new NotFoundHttpException($this->translator->trans(self::ERR_FILE_NOT_FOUND, [], 'messages'));
break;
default:
\Sentry\captureException($e);
throw new Exception(self::ERR_UNAVAILABLE);
break;
if(!$usecache||!$fs->exists($tmpdir.'/'.$filename)) {
try {
$result = $this->client->getObject([
'Bucket' => $this->minioBucket,
'Key' => $this->minioRoot.$file,
'SaveAs' => $tmpdir.'/'.$filename,
]);
} catch (S3Exception $e) {
switch ($e->getResponse()->getStatusCode()) {
case 404:
throw new NotFoundHttpException($this->translator->trans(self::ERR_FILE_NOT_FOUND, [], 'messages'));
break;
default:
\Sentry\captureException($e);
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;
} 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;
}
return $tmpdir.'/'.$filename;
}
public function upload($file, $filename, $deleteSource = false)