[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 699f25a397
commit 6438092d86
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 28 additions and 35 deletions

View File

@ -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');
}

View File

@ -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)

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)
'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' => [