[AVATAR] Update way avatar is sent, to use proper symfony responses, make config('site', 's_static_delivery') into a boolean

This commit is contained in:
Hugo Sales 2020-08-08 21:28:51 +00:00 committed by Hugo Sales
parent a498134b13
commit d21d4f5cb1
3 changed files with 28 additions and 35 deletions

View File

@ -49,10 +49,7 @@ class Avatar extends Controller
} }
$res = $result[0]; $res = $result[0];
Media::sendFile(EAvatar::getFilePath($res['file_hash']), $res['mimetype'], $res['title']); return Media::sendFile(EAvatar::getFilePath($res['file_hash']), $res['mimetype'], $res['title']);
die();
// TODO FIX THIS
break;
default: default:
throw new Exception('Not implemented'); throw new Exception('Not implemented');
} }

View File

@ -19,11 +19,15 @@
namespace Component\Media; namespace Component\Media;
use App\Core\Log;
use App\Core\Module; use App\Core\Module;
use App\Entity\File; use App\Entity\File;
use App\Util\Common; use App\Util\Common;
use App\Util\Nickname; use App\Util\Nickname;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\File as SymfonyFile; use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
use Symfony\Component\HttpFoundation\Response;
class Media extends Module class Media extends Module
{ {
@ -50,40 +54,32 @@ class Media extends Module
* *
* @throws ServerException * @throws ServerException
*/ */
public static function sendFile(string $filepath, string $mimetype, string $output_filename, string $disposition = 'inline'): void public static function sendFile(string $filepath, string $mimetype, string $output_filename, string $disposition = 'inline'): Response
{ {
$x_delivery = Common::config('site', 'x_static_delivery'); if (file_exists($filepath)) {
if (is_string($x_delivery)) { try {
$tmp = explode(INSTALLDIR, $filepath); $response = new BinaryFileResponse(
$relative_path = end($tmp); $filepath,
Log::debug("Using Static Delivery with header for: {$relative_path}"); Response::HTTP_OK,
header("{$x_delivery}: {$relative_path}"); [
} else { 'Content-Description' => 'File Transfer',
if (file_exists($filepath)) { 'Content-Type' => $mimetype,
header('Content-Description: File Transfer'); ],
header("Content-Type: {$mimetype}"); $public = true,
header("Content-Disposition: {$disposition}; filename=\"{$output_filename}\""); $disposition,
header('Expires: 0'); $add_etag = true,
header('Content-Transfer-Encoding: binary'); $add_last_modified = true
);
$filesize = filesize($filepath); if (Common::config('site', 'x_static_delivery')) {
$response->trustXSendfileTypeHeader();
http_response_code(200);
header("Content-Length: {$filesize}");
// header('Cache-Control: private, no-transform, no-store, must-revalidate');
$ret = @readfile($filepath);
if ($ret === false) {
http_response_code(404);
Log::error("Couldn't read file at {$filepath}.");
} elseif ($ret !== $filesize) {
http_response_code(500);
Log::error('The lengths of the file as recorded on the DB (or on disk) for the file ' .
"{$filepath} differ from what was sent to the user ({$filesize} vs {$ret}).");
} }
return $response;
} catch (FileException $e) {
// continue bellow
} }
} }
Log::error("Couldn't read file at {$filepath}.");
throw new ClientException('No such file', Response::HTTP_NOT_FOUND);
} }
public function onAddRoute($r) public function onAddRoute($r)

View File

@ -63,7 +63,7 @@ abstract class DefaultSettings
'ssl_proxy' => false, // set to true to force GNU social to think it is HTTPS (i.e. using reverse proxy to enable it) 'ssl_proxy' => false, // set to true to force GNU social to think it is HTTPS (i.e. using reverse proxy to enable it)
'duplicate_time_limit' => 60, // default for same person saying the same thing 'duplicate_time_limit' => 60, // default for same person saying the same thing
'text_limit' => 1000, // in chars; 0 == no limit 'text_limit' => 1000, // in chars; 0 == no limit
'x_static_delivery' => null, 'x_static_delivery' => false,
'defaults_modified' => time(), 'defaults_modified' => time(),
], ],
'security' => [ 'security' => [