[MEDIA][EXCEPTIONS] Fix errors and deprecations

This commit is contained in:
Hugo Sales 2020-08-09 12:44:47 +00:00 committed by Hugo Sales
parent 08e5b313ce
commit 4128a5403d
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
11 changed files with 26 additions and 12 deletions

View File

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

View File

@ -23,6 +23,7 @@ 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\Exception\ClientException;
use App\Util\Nickname; use App\Util\Nickname;
use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Exception\FileException; use Symfony\Component\HttpFoundation\File\Exception\FileException;

View File

@ -62,7 +62,7 @@ class Controller extends AbstractController implements EventSubscriberInterface
$controller = $event->getController(); $controller = $event->getController();
$request = $event->getRequest(); $request = $event->getRequest();
if (($avatar = DB::find('avatar', ['profile_id' => Common::profile()->getId()])) != null) { if (($user = Common::user()) !== null && ($avatar = DB::find('avatar', ['profile_id' => $user->getProfile()->getId()])) != null) {
$avatar_filename = $avatar->getUrl(); $avatar_filename = $avatar->getUrl();
} else { } else {
$avatar_filename = '/public/assets/default_avatar.svg'; $avatar_filename = '/public/assets/default_avatar.svg';

View File

@ -1,6 +1,7 @@
<?php <?php
// {{{ License // {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social // 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 // GNU social is free software: you can redistribute it and/or modify
@ -15,6 +16,7 @@
// //
// You should have received a copy of the GNU Affero General Public License // 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/>. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}} // }}}
namespace App\Entity; namespace App\Entity;
@ -107,9 +109,14 @@ class Avatar extends Entity
return $this->file; return $this->file;
} }
public function getFilePath(?string $filename = null): string public static function getFilePathStatic(string $filename): string
{ {
return Common::config('avatar', 'dir') . '/' . $filename ?: $this->getFile()->getFileName(); return Common::config('avatar', 'dir') . '/' . $filename;
}
public function getFilePath(): string
{
return Common::config('avatar', 'dir') . '/' . $this->getFile()->getFileName();
} }
/** /**

View File

@ -202,12 +202,12 @@ class Profile extends Entity
// }}} Autocode // }}} Autocode
public function getFromId(int $id): ?self public static function getFromId(int $id): ?self
{ {
return DB::find('profile', ['id' => $id]); return DB::find('profile', ['id' => $id]);
} }
public function getFromNickname(string $nickname): ?self public static function getFromNickname(string $nickname): ?self
{ {
return DB::findOneBy('profile', ['nickname' => $nickname]); return DB::findOneBy('profile', ['nickname' => $nickname]);
} }

View File

@ -70,12 +70,12 @@ abstract class Common
DB::flush(); DB::flush();
} }
public static function user(): LocalUser public static function user(): ?LocalUser
{ {
return Security::getUser(); return Security::getUser();
} }
public static function profile(): Profile public static function profile(): ?Profile
{ {
return self::user()->getProfile(); return self::user()->getProfile();
} }

View File

@ -19,6 +19,8 @@
namespace App\Util\Exception; namespace App\Util\Exception;
use Exception;
/** /**
* Client exception. Indicates a client request contains some sort of * Client exception. Indicates a client request contains some sort of
* error. HTTP code 400 * error. HTTP code 400

View File

@ -40,7 +40,7 @@ namespace App\Util\Exception;
*/ */
class NicknameInvalidException extends NicknameException class NicknameInvalidException extends NicknameException
{ {
protected function defaultMessage() protected function defaultMessage(): string
{ {
// TRANS: Validation error in form for registration, profile and group settings, etc. // TRANS: Validation error in form for registration, profile and group settings, etc.
return _m('Nickname must have only lowercase letters and numbers and no spaces.'); return _m('Nickname must have only lowercase letters and numbers and no spaces.');

View File

@ -40,7 +40,7 @@ namespace App\Util\Exception;
*/ */
class NicknameReservedException extends NicknameException class NicknameReservedException extends NicknameException
{ {
protected function defaultMessage() protected function defaultMessage(): string
{ {
// TRANS: Validation error in form for registration, profile and group settings, etc. // TRANS: Validation error in form for registration, profile and group settings, etc.
return _m('Nickname is reserved.'); return _m('Nickname is reserved.');

View File

@ -1,6 +1,7 @@
<?php <?php
// {{{ License // {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social // 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 // GNU social is free software: you can redistribute it and/or modify
@ -15,6 +16,7 @@
// //
// You should have received a copy of the GNU Affero General Public License // 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/>. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}} // }}}
namespace App\Util\Exception; namespace App\Util\Exception;
@ -48,7 +50,7 @@ class NicknameTakenException extends NicknameException
parent::__construct($msg, $code); parent::__construct($msg, $code);
} }
protected function defaultMessage() protected function defaultMessage(): string
{ {
// TRANS: Validation error in form for registration, profile and group settings, etc. // TRANS: Validation error in form for registration, profile and group settings, etc.
return _m('Nickname is already in use on this server.'); return _m('Nickname is already in use on this server.');

View File

@ -1,6 +1,7 @@
<?php <?php
// {{{ License // {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social // 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 // GNU social is free software: you can redistribute it and/or modify
@ -15,6 +16,7 @@
// //
// You should have received a copy of the GNU Affero General Public License // 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/>. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}} // }}}
namespace App\Util\Exception; namespace App\Util\Exception;
@ -42,7 +44,7 @@ use App\Util\Nickname;
*/ */
class NicknameTooLongException extends NicknameInvalidException class NicknameTooLongException extends NicknameInvalidException
{ {
protected function defaultMessage() protected function defaultMessage(): string
{ {
// TRANS: Validation error in form for registration, profile and group settings, etc. // TRANS: Validation error in form for registration, profile and group settings, etc.
return _m('Nickname cannot be more than # character long.', ['count' => Nickname::MAX_LEN]); return _m('Nickname cannot be more than # character long.', ['count' => Nickname::MAX_LEN]);