[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]; $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 (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)) { if (file_exists($filepath)) {
header('Content-Description: File Transfer'); try {
header("Content-Type: {$mimetype}"); $response = new BinaryFileResponse(
header("Content-Disposition: {$disposition}; filename=\"{$output_filename}\""); $filepath,
header('Expires: 0'); Response::HTTP_OK,
header('Content-Transfer-Encoding: binary'); [
'Content-Description' => 'File Transfer',
$filesize = filesize($filepath); 'Content-Type' => $mimetype,
],
http_response_code(200); $public = true,
header("Content-Length: {$filesize}"); $disposition,
// header('Cache-Control: private, no-transform, no-store, must-revalidate'); $add_etag = true,
$add_last_modified = true
$ret = @readfile($filepath); );
if (Common::config('site', 'x_static_delivery')) {
if ($ret === false) { $response->trustXSendfileTypeHeader();
http_response_code(404); }
return $response;
} catch (FileException $e) {
// continue bellow
}
}
Log::error("Couldn't read file at {$filepath}."); Log::error("Couldn't read file at {$filepath}.");
} elseif ($ret !== $filesize) { throw new ClientException('No such file', Response::HTTP_NOT_FOUND);
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}).");
}
}
}
} }
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' => [