[AVATAR] Fixed avatar upload, added avatar inline download and updated template and base controller

This commit is contained in:
Hugo Sales
2020-08-08 16:10:25 +00:00
committed by Hugo Sales
parent 2bf914f96f
commit bd8f4bd277
13 changed files with 303 additions and 97 deletions

View File

@@ -0,0 +1,60 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace Component\Media\Controller;
use App\Core\Controller;
use App\Core\DB\DB;
use function App\Core\I18n\_m;
use App\Core\Log;
use App\Entity\Avatar as EAvatar;
use Component\Media\Media;
use Exception;
use Symfony\Component\HttpFoundation\Request;
class Avatar extends Controller
{
public function send(Request $request, string $nickname, string $size)
{
switch ($size) {
case 'full':
$result = DB::createQuery('select f.file_hash, f.mimetype, f.title from ' .
'App\\Entity\\File f join App\\Entity\\Avatar a with f.id = a.file_id ' .
'join App\\Entity\\Profile p with p.id = a.profile_id ' .
'where p.nickname = :nickname')
->setParameter('nickname', $nickname)
->getResult();
if (count($result) != 1) {
Log::error('Avatar query returned more than one result for nickname ' . $nickname);
throw new Exception(_m('Internal server error'));
}
$res = $result[0];
Media::sendFile(EAvatar::getFilePath($res['file_hash']), $res['mimetype'], $res['title']);
die();
// TODO FIX THIS
break;
default:
throw new Exception('Not implemented');
}
}
}

View File

@@ -21,6 +21,8 @@ namespace Component\Media;
use App\Core\Module;
use App\Entity\File;
use App\Util\Common;
use App\Util\Nickname;
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
class Media extends Module
@@ -42,4 +44,50 @@ class Media extends Module
// TODO Normalize file types
return $file;
}
/**
* Include $filepath in the response, for viewing and downloading.
*
* @throws ServerException
*/
public static function sendFile(string $filepath, string $mimetype, string $output_filename, string $disposition = 'inline'): void
{
$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}).");
}
}
}
}
public function onAddRoute($r)
{
$r->connect('avatar', '/{nickname<' . Nickname::DISPLAY_FMT . '>}/avatar/{size<full|big|medium|small>?full}', [Controller\Avatar::class, 'send']);
}
}