From 726c4dff7d7c9b7d3953a6819c09e4e18e4673ad Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 15 Jun 2022 03:59:26 +0000 Subject: [PATCH] You can now store the avatar in a separate folder and host --- src/Contact/Avatar.php | 71 ++++++++++++++++++++++++++++++-------- static/defaults.config.php | 10 ++++++ 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php index d63ebe5b6..60981485e 100644 --- a/src/Contact/Avatar.php +++ b/src/Contact/Avatar.php @@ -32,7 +32,6 @@ use Friendica\Util\HTTPSignature; use Friendica\Util\Images; use Friendica\Util\Network; use Friendica\Util\Proxy; -use Friendica\Util\Strings; /** * functions for handling contact avatar caching @@ -139,21 +138,19 @@ class Avatar return ''; } - $path = self::BASE_PATH . $filename . $size . '.' . $image->getExt(); + $path = $filename . $size . '.' . $image->getExt(); - $filepath = DI::basePath() . $path; + $basepath = self::basePath(); + if (empty($basepath)) { + return ''; + } - $dirpath = DI::basePath() . self::BASE_PATH; + $filepath = $basepath . $path; + + $dirpath = $basepath; DI::profiler()->startRecording('file'); - if (!file_exists($dirpath)) { - if (!mkdir($dirpath, 0775)) { - Logger::warning('Base directory could not be created', ['directory' => $dirpath]); - return ''; - } - } - // Fetch the permission and group ownership of the "avatar" path and apply to all files $dir_perm = fileperms($dirpath) & 0777; $file_perm = fileperms($dirpath) & 0666; @@ -198,7 +195,7 @@ class Avatar return ''; } - return DI::baseUrl() . $path . '?ts=' . $timestamp; + return self::baseUrl() . $path . '?ts=' . $timestamp; } /** @@ -221,16 +218,17 @@ class Avatar private static function getCacheFile(string $avatar): string { $parts = parse_url($avatar); - if (empty($parts['host']) || ($parts['host'] != DI::baseUrl()->getHostname())) { + if (empty($parts['host']) || ($parts['host'] != parse_url(self::baseUrl(), PHP_URL_HOST))) { return ''; } - $pos = strpos($parts['path'], DI::baseUrl()->getUrlPath() . self::BASE_PATH); + $avatarpath = parse_url(self::baseUrl(), PHP_URL_PATH); + $pos = strpos($parts['path'], $avatarpath); if ($pos !== 0) { return ''; } - $filename = DI::basePath() . $parts['path']; + $filename = self::basePath() . substr($parts['path'], strlen($avatarpath)); DI::profiler()->startRecording('file'); $exists = file_exists($filename); @@ -269,4 +267,47 @@ class Avatar Logger::debug('Unlink avatar', ['avatar' => $avatar]); } } + + /** + * Fetch the avatar base path + * + * @return string + */ + private static function basePath(): string + { + $basepath = DI::config()->get('system', 'avatar_cache_path'); + if (empty($basepath)) { + $basepath = DI::basePath() . self::BASE_PATH; + } + $basepath = rtrim($basepath, '/') . '/'; + + if (!file_exists($basepath)) { + // We only automatically create the folder when it is in the web root + if (strpos($basepath, DI::basePath()) !== 0) { + Logger::warning('Base directory does not exist', ['directory' => $basepath]); + return ''; + } + if (!mkdir($basepath, 0775)) { + Logger::warning('Base directory could not be created', ['directory' => $basepath]); + return ''; + } + } + + return $basepath; + } + + /** + * Fetch the avatar base url + * + * @return string + */ + private static function baseUrl(): string + { + $baseurl = DI::config()->get('system', 'avatar_cache_url'); + if (!empty($baseurl)) { + return rtrim($baseurl, '/') . '/'; + } + + return DI::baseUrl() . self::BASE_PATH; + } } diff --git a/static/defaults.config.php b/static/defaults.config.php index 6ea80515b..619ac3c2b 100644 --- a/static/defaults.config.php +++ b/static/defaults.config.php @@ -122,6 +122,16 @@ return [ // Cache avatar pictures as files (experimental) 'avatar_cache' => false, + // avatar_cache_path (String) + // File path to the avatar cache. Default is /(your basepath)/avatar/ + // The value has to be an absolute path and has to end with a "/" + 'avatar_cache_path' => '', + + // avatar_cache_url (String) + // Base URL of the avatar cache. Default is http(s)://(your hostname)/avatar/ + // The value has to start with the scheme and end with a "/" + 'avatar_cache_url' => '', + // big_emojis (Boolean) // Display "Emoji Only" posts in big. 'big_emojis' => false,