diff --git a/components/Media/Controller/Avatar.php b/components/Media/Controller/Avatar.php index 734526d457..cf6561c197 100644 --- a/components/Media/Controller/Avatar.php +++ b/components/Media/Controller/Avatar.php @@ -49,10 +49,7 @@ class Avatar extends Controller } $res = $result[0]; - Media::sendFile(EAvatar::getFilePath($res['file_hash']), $res['mimetype'], $res['title']); - die(); - // TODO FIX THIS - break; + return Media::sendFile(EAvatar::getFilePath($res['file_hash']), $res['mimetype'], $res['title']); default: throw new Exception('Not implemented'); } diff --git a/components/Media/Media.php b/components/Media/Media.php index bb5f78dbe4..103b1e89c7 100644 --- a/components/Media/Media.php +++ b/components/Media/Media.php @@ -19,11 +19,15 @@ namespace Component\Media; +use App\Core\Log; use App\Core\Module; use App\Entity\File; use App\Util\Common; 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\Response; class Media extends Module { @@ -50,40 +54,32 @@ class Media extends Module * * @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 (is_string($x_delivery)) { - $tmp = explode(INSTALLDIR, $filepath); - $relative_path = end($tmp); - Log::debug("Using Static Delivery with header for: {$relative_path}"); - header("{$x_delivery}: {$relative_path}"); - } else { - if (file_exists($filepath)) { - header('Content-Description: File Transfer'); - header("Content-Type: {$mimetype}"); - header("Content-Disposition: {$disposition}; filename=\"{$output_filename}\""); - header('Expires: 0'); - header('Content-Transfer-Encoding: binary'); - - $filesize = filesize($filepath); - - 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})."); + if (file_exists($filepath)) { + try { + $response = new BinaryFileResponse( + $filepath, + Response::HTTP_OK, + [ + 'Content-Description' => 'File Transfer', + 'Content-Type' => $mimetype, + ], + $public = true, + $disposition, + $add_etag = true, + $add_last_modified = true + ); + if (Common::config('site', 'x_static_delivery')) { + $response->trustXSendfileTypeHeader(); } + 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) diff --git a/src/Core/DB/DefaultSettings.php b/src/Core/DB/DefaultSettings.php index 1c46a4513c..5bac1a6856 100644 --- a/src/Core/DB/DefaultSettings.php +++ b/src/Core/DB/DefaultSettings.php @@ -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) 'duplicate_time_limit' => 60, // default for same person saying the same thing 'text_limit' => 1000, // in chars; 0 == no limit - 'x_static_delivery' => null, + 'x_static_delivery' => false, 'defaults_modified' => time(), ], 'security' => [