From: Michael Date: Mon, 9 May 2022 06:31:09 +0000 (+0000) Subject: Standards and renamed class X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=681d19a3bca8e0108071d86f9fbc405525479ad5;p=friendica.git Standards and renamed class --- diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php new file mode 100644 index 0000000000..f4e5b71afe --- /dev/null +++ b/src/Contact/Avatar.php @@ -0,0 +1,194 @@ +. + * + */ + +namespace Friendica\Contact; + +use Friendica\Core\Logger; +use Friendica\DI; +use Friendica\Model\Item; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; +use Friendica\Network\HTTPClient\Client\HttpClientOptions; +use Friendica\Object\Image; +use Friendica\Util\DateTimeFormat; +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 + */ +class Avatar +{ + /** + * Returns a field array with locally cached avatar pictures + * + * @param array $contact + * @param string $avatar + * @return array + */ + public static function fetchAvatarContact(array $contact, string $avatar): array + { + $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(), 'photo' => '', 'thumb' => '', 'micro' => '']; + + if (!DI::config()->get('system', 'avatar_cache')) { + self::deleteCache($contact); + return $fields; + } + + if (Network::isLocalLink($avatar)) { + return $fields; + } + + if ($avatar != $contact['avatar']) { + self::deleteCache($contact); + Logger::debug('Avatar file name changed', ['new' => $avatar, 'old' => $contact['avatar']]); + } elseif (self::isCacheFile($contact['photo']) && self::isCacheFile($contact['thumb']) && self::isCacheFile($contact['micro'])) { + $fields['photo'] = $contact['photo']; + $fields['thumb'] = $contact['thumb']; + $fields['micro'] = $contact['micro']; + Logger::debug('Using existing cache files', ['uri-id' => $contact['uri-id'], 'fields' => $fields]); + return $fields; + } + + $guid = Item::guidFromUri($contact['url'], parse_url($contact['url'], PHP_URL_HOST)); + + $filename = substr($guid, 0, 2) . '/' . substr($guid, 3, 2) . '/' . substr($guid, 5, 3) . '/' . + substr($guid, 9, 2) .'/' . substr($guid, 11, 2) . '/' . substr($guid, 13, 4). '/' . substr($guid, 18) . '-'; + + $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]); + + $img_str = $fetchResult->getBody(); + if (empty($img_str)) { + Logger::debug('Avatar is invalid', ['avatar' => $avatar]); + return $fields; + } + + $image = new Image($img_str, Images::getMimeTypeByData($img_str)); + if (!$image->isValid()) { + Logger::debug('Avatar picture is invalid', ['avatar' => $avatar]); + return $fields; + } + + $fields['photo'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_SMALL); + $fields['thumb'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_THUMB); + $fields['micro'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_MICRO); + + Logger::debug('Storing new avatar cache', ['uri-id' => $contact['uri-id'], 'fields' => $fields]); + + return $fields; + } + + private static function storeAvatarCache(Image $image, string $filename, int $size): string + { + $image->scaleDown($size); + if (is_null($image) || !$image->isValid()) { + return ''; + } + + $path = '/avatar/' . $filename . $size . '.' . $image->getExt(); + + $filepath = DI::basePath() . $path; + + $dirpath = dirname($filepath); + + DI::profiler()->startRecording('file'); + + if (!file_exists($dirpath)) { + mkdir($dirpath, 0777, true); + } + + file_put_contents($filepath, $image->asString()); + DI::profiler()->stopRecording(); + + return DI::baseUrl() . $path; + } + + /** + * Check if the avatar cache file is locally stored + * + * @param string $avatar + * @return boolean + */ + public static function isCacheFile(string $avatar): bool + { + return !empty(self::getCacheFile($avatar)); + } + + /** + * Fetch the name of locally cached avatar pictures + * + * @param string $avatar + * @return string + */ + private static function getCacheFile(string $avatar): string + { + if (empty($avatar) || !Network::isLocalLink($avatar)) { + return ''; + } + + $path = Strings::normaliseLink(DI::baseUrl() . '/avatar'); + + if (Network::getUrlMatch($path, $avatar) != $path) { + return ''; + } + + $filename = str_replace($path, DI::basePath(). '/avatar/', Strings::normaliseLink($avatar)); + + DI::profiler()->startRecording('file'); + $exists = file_exists($filename); + DI::profiler()->stopRecording(); + + if (!$exists) { + return ''; + } + return $filename; + } + + /** + * Delete locally cached avatar pictures of a contact + * + * @param string $avatar + * @return void + */ + public static function deleteCache(array $contact) + { + self::deleteCacheFile($contact['photo']); + self::deleteCacheFile($contact['thumb']); + self::deleteCacheFile($contact['micro']); + } + + /** + * Delete a locally cached avatar picture + * + * @param string $avatar + * @return void + */ + private static function deleteCacheFile(string $avatar) + { + $localFile = self::getCacheFile($avatar); + if (!empty($localFile)) { + unlink($localFile); + Logger::debug('Unlink avatar', ['avatar' => $avatar]); + } + } +} diff --git a/src/Contact/Contact.php b/src/Contact/Contact.php deleted file mode 100644 index d452096e40..0000000000 --- a/src/Contact/Contact.php +++ /dev/null @@ -1,193 +0,0 @@ -. - * - */ - -namespace Friendica\Contact; - -use Friendica\Core\Logger; -use Friendica\DI; -use Friendica\Model\Item; -use Friendica\Network\HTTPClient\Client\HttpClientAccept; -use Friendica\Network\HTTPClient\Client\HttpClientOptions; -use Friendica\Object\Image; -use Friendica\Util\DateTimeFormat; -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 - */ -class Avatar -{ - /** - * Returns a field array with locally cached avatar pictures - * - * @param array $contact - * @param string $avatar - * @return array - */ - public static function fetchAvatarContact(array $contact, string $avatar): array - { - $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(), 'photo' => '', 'thumb' => '', 'micro' => '']; - - if (!DI::config()->get('system', 'avatar_cache')) { - self::deleteCache($contact); - return $fields; - } - - if (Network::isLocalLink($avatar)) { - return $fields; - } - - if ($avatar != $contact['avatar']) { - self::deleteCache($contact); - Logger::debug('Avatar file name changed', ['new' => $avatar, 'old' => $contact['avatar']]); - } elseif (self::isCacheFile($contact['photo']) && self::isCacheFile($contact['thumb']) && self::isCacheFile($contact['micro'])) { - $fields['photo'] = $contact['photo']; - $fields['thumb'] = $contact['thumb']; - $fields['micro'] = $contact['micro']; - Logger::debug('Using existing cache files', ['uri-id' => $contact['uri-id'], 'fields' => $fields]); - return $fields; - } - - $guid = Item::guidFromUri($contact['url'], parse_url($contact['url'], PHP_URL_HOST)); - - $filename = substr($guid, 0, 2) . '/' . substr($guid, 3, 2) . '/' . substr($guid, 5, 3) . '/' . - substr($guid, 9, 2) .'/' . substr($guid, 11, 2) . '/' . substr($guid, 13, 4). '/' . substr($guid, 18) . '-'; - - $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]); - $img_str = $fetchResult->getBody(); - if (empty($img_str)) { - Logger::debug('Avatar is invalid', ['avatar' => $avatar]); - return $fields; - } - - $image = new Image($img_str, Images::getMimeTypeByData($img_str)); - if (!$image->isValid()) { - Logger::debug('Avatar picture is invalid', ['avatar' => $avatar]); - return $fields; - } - - $fields['photo'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_SMALL); - $fields['thumb'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_THUMB); - $fields['micro'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_MICRO); - - Logger::debug('Storing new avatar cache', ['uri-id' => $contact['uri-id'], 'fields' => $fields]); - - return $fields; - } - - private static function storeAvatarCache(Image $image, string $filename, int $size): string - { - $image->scaleDown($size); - if (is_null($image) || !$image->isValid()) { - return ''; - } - - $path = '/avatar/' . $filename . $size . '.' . $image->getExt(); - - $filepath = DI::basePath() . $path; - - $dirpath = dirname($filepath); - - DI::profiler()->startRecording('file'); - - if (!file_exists($dirpath)) { - mkdir($dirpath, 0777, true); - } - - file_put_contents($filepath, $image->asString()); - DI::profiler()->stopRecording(); - - return DI::baseUrl() . $path; - } - - /** - * Check if the avatar cache file is locally stored - * - * @param string $avatar - * @return boolean - */ - public static function isCacheFile(string $avatar): bool - { - return !empty(self::getCacheFile($avatar)); - } - - /** - * Fetch the name of locally cached avatar pictures - * - * @param string $avatar - * @return string - */ - private static function getCacheFile(string $avatar): string - { - if (empty($avatar) || !Network::isLocalLink($avatar)) { - return ''; - } - - $path = Strings::normaliseLink(DI::baseUrl() . '/avatar'); - - if (Network::getUrlMatch($path, $avatar) != $path) { - return ''; - } - - $filename = str_replace($path, DI::basePath(). '/avatar/', Strings::normaliseLink($avatar)); - - DI::profiler()->startRecording('file'); - $exists = file_exists($filename); - DI::profiler()->stopRecording(); - - if (!$exists) { - return ''; - } - return $filename; - } - - /** - * Delete locally cached avatar pictures of a contact - * - * @param string $avatar - * @return void - */ - public static function deleteCache(array $contact) - { - self::deleteCacheFile($contact['photo']); - self::deleteCacheFile($contact['thumb']); - self::deleteCacheFile($contact['micro']); - } - - /** - * Delete a locally cached avatar picture - * - * @param string $avatar - * @return void - */ - private static function deleteCacheFile(string $avatar) - { - $localFile = self::getCacheFile($avatar); - if (!empty($localFile)) { - unlink($localFile); - Logger::debug('Unlink avatar', ['avatar' => $avatar]); - } - } -}