From b7ed15c865c14df7b35c9845eb630702f4fe8cdf Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Fri, 6 Mar 2020 17:34:49 +0000 Subject: [PATCH] [CORE] Make avatars be served with the same mechanism as attachments --- actions/avatar.php | 69 ++++++++++++++++++++++++++++++++++++++++++++ classes/Avatar.php | 2 +- lib/util/default.php | 4 +-- lib/util/router.php | 4 +++ 4 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 actions/avatar.php diff --git a/actions/avatar.php b/actions/avatar.php new file mode 100644 index 0000000000..3e9beff7d9 --- /dev/null +++ b/actions/avatar.php @@ -0,0 +1,69 @@ +. + +/** + * Retrieve user avatar by nickname action class. + * + * @category Action + * @package GNUsocial + * + * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 + */ +if (!defined('GNUSOCIAL')) { + exit(1); +} + +/** + * Retrieve user avatar by nickname action class. + * + * @category Action + * @package GNUsocial + * + * @author Evan Prodromou + * @author Robin Millette + * @author Mikael Nordfeldth + * @author Hugo Sales + * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 + * + * @see http://www.gnu.org/software/social/ + */ +class AvatarAction extends Action +{ + public $filename; + protected function prepare(array $args = []) + { + parent::prepare($args); + if (empty($this->filename = $this->trimmed('file'))) { + // TRANS: Client error displayed trying to get a non-existing avatar. + $this->clientError(_m('No such avatar.'), 404); + } + return true; + } + + protected function handle() + { + parent::handle(); + + if (is_string($srv = common_config('avatar', 'server')) && $srv != '') { + common_redirect(Avatar::url($this->filename), 302); + } else { + $attachment = new AttachmentAction(); // kludge... + $attachment->filepath = common_config('avatar', 'dir') . $this->filename; + $attachment->sendFile(); + } + return true; + } +} diff --git a/classes/Avatar.php b/classes/Avatar.php index cbb3fc1e5e..8c01d9d4d2 100644 --- a/classes/Avatar.php +++ b/classes/Avatar.php @@ -187,7 +187,7 @@ class Avatar extends Managed_DataObject public static function url($filename) { - $path = common_config('avatar', 'path'); + $path = common_config('avatar', 'url_base'); if ($path[strlen($path)-1] != '/') { $path .= '/'; diff --git a/lib/util/default.php b/lib/util/default.php index 1fc7fe52fe..d6f2639f48 100644 --- a/lib/util/default.php +++ b/lib/util/default.php @@ -147,8 +147,8 @@ $default = array('jpegquality' => 85), 'avatar' => array('server' => null, - 'dir' => PUBLICDIR . '/avatar/', - 'path' => $_path . '/avatar/', + 'dir' => INSTALLDIR . '/file/avatar/', + 'url_base' => $_path . '/avatar/', 'ssl' => null, 'maxsize' => 300), 'foaf' => diff --git a/lib/util/router.php b/lib/util/router.php index a069ec2a86..80097a70c3 100644 --- a/lib/util/router.php +++ b/lib/util/router.php @@ -1040,6 +1040,10 @@ class Router ['nickname' => Nickname::DISPLAY_FMT]); } + $m->connect('avatar/:file', + ['action' => 'avatar'], + ['file' => '.*']); + $m->connect(':nickname/avatar', ['action' => 'avatarbynickname'], ['nickname' => Nickname::DISPLAY_FMT]);