[ATTACHMENTS] In sendFile, check that file exists or show a custom exception

This commit is contained in:
Hugo Sales 2021-05-01 13:02:14 +00:00
parent 38cf8f8efe
commit 6d31945401
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
1 changed files with 22 additions and 17 deletions

View File

@ -27,6 +27,7 @@ use App\Entity\Attachment;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\ClientException; use App\Util\Exception\ClientException;
use App\Util\Exception\NoSuchFileException; use App\Util\Exception\NoSuchFileException;
use App\Util\Exception\ServerException;
use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\File as SymfonyFile; use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
use Symfony\Component\HttpFoundation\HeaderUtils; use Symfony\Component\HttpFoundation\HeaderUtils;
@ -95,24 +96,28 @@ class GSFile
*/ */
public static function sendFile(string $filepath, string $mimetype, ?string $output_filename, string $disposition = 'inline'): Response public static function sendFile(string $filepath, string $mimetype, ?string $output_filename, string $disposition = 'inline'): Response
{ {
$response = new BinaryFileResponse( if (is_file($filepath)) {
$filepath, $response = new BinaryFileResponse(
Response::HTTP_OK, $filepath,
[ Response::HTTP_OK,
'Content-Description' => 'File Transfer', [
'Content-Type' => $mimetype, 'Content-Description' => 'File Transfer',
'Content-Disposition' => HeaderUtils::makeDisposition($disposition, $output_filename ?: _m('Untitled attachment'), _m('Untitled attachment')), 'Content-Type' => $mimetype,
'Cache-Control' => 'public', 'Content-Disposition' => HeaderUtils::makeDisposition($disposition, $output_filename ?: _m('Untitled attachment'), _m('Untitled attachment')),
], 'Cache-Control' => 'public',
$public = true, ],
$disposition = null, $public = true,
$add_etag = true, $disposition = null,
$add_last_modified = true $add_etag = true,
); $add_last_modified = true
if (Common::config('site', 'x_static_delivery')) { );
$response->trustXSendfileTypeHeader(); if (Common::config('site', 'x_static_delivery')) {
$response->trustXSendfileTypeHeader();
}
return $response;
} else {
throw new ServerException(_m('This attachment is not stored locally'));
} }
return $response;
} }
/** /**