Avatar.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace Component\Avatar;
  19. use App\Core\Cache;
  20. use App\Core\DB\DB;
  21. use App\Core\Event;
  22. use App\Core\GSFile;
  23. use App\Core\Modules\Component;
  24. use App\Util\Common;
  25. use Component\Avatar\Exception\NoAvatarException;
  26. use Exception;
  27. use Symfony\Component\Asset\Package;
  28. use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
  29. class Avatar extends Component
  30. {
  31. public function onAddRoute($r)
  32. {
  33. $r->connect('avatar', '/{gsactor_id<\d+>}/avatar/{size<full|big|medium|small>?full}', [Controller\Avatar::class, 'avatar_view']);
  34. $r->connect('settings_avatar', '/settings/avatar', [Controller\Avatar::class, 'settings_avatar']);
  35. return Event::next;
  36. }
  37. public function onEndTwigPopulateVars(array &$vars)
  38. {
  39. if (Common::user() != null) {
  40. $vars['user_avatar'] = self::getAvatarUrl();
  41. }
  42. return Event::next;
  43. }
  44. public function onGetAvatarUrl(int $gsactor_id, ?string &$url)
  45. {
  46. $url = self::getAvatarUrl($gsactor_id);
  47. return Event::next;
  48. }
  49. public function onDeleteCachedAvatar(int $gsactor_id)
  50. {
  51. Cache::delete('avatar-' . $gsactor_id);
  52. Cache::delete('avatar-url-' . $gsactor_id);
  53. Cache::delete('avatar-file-info-' . $gsactor_id);
  54. }
  55. // UTILS ----------------------------------
  56. /**
  57. * Get the avatar associated with the given nickname
  58. */
  59. public static function getAvatar(?int $gsactor_id = null): \App\Entity\Avatar
  60. {
  61. $gsactor_id = $gsactor_id ?: Common::userNickname();
  62. return GSFile::error(NoAvatarException::class,
  63. $gsactor_id,
  64. Cache::get("avatar-{$gsactor_id}",
  65. function () use ($gsactor_id) {
  66. return DB::dql('select a from App\Entity\Avatar a ' .
  67. 'where a.gsactor_id = :gsactor_id',
  68. ['gsactor_id' => $gsactor_id]);
  69. }));
  70. }
  71. /**
  72. * Get the cached avatar associated with the given nickname, or the current user if not given
  73. */
  74. public static function getAvatarUrl(?int $gsactor_id = null): string
  75. {
  76. $gsactor_id = $gsactor_id ?: Common::userId();
  77. return Cache::get("avatar-url-{$gsactor_id}", function () use ($gsactor_id) {
  78. try {
  79. return self::getAvatar($gsactor_id)->getUrl();
  80. } catch (NoAvatarException $e) {
  81. }
  82. $package = new Package(new EmptyVersionStrategy());
  83. return $package->getUrl(Common::config('avatar', 'default'));
  84. });
  85. }
  86. /**
  87. * Get the cached avatar file info associated with the given GSActor id
  88. *
  89. * Returns the avatar file's hash, mimetype, title and path.
  90. * Ensures exactly one cached value exists
  91. */
  92. public static function getAvatarFileInfo(int $gsactor_id): array
  93. {
  94. try {
  95. $res = GSFile::error(NoAvatarException::class,
  96. $gsactor_id,
  97. Cache::get("avatar-file-info-{$gsactor_id}",
  98. function () use ($gsactor_id) {
  99. return DB::dql('select f.file_hash, f.mimetype, f.title ' .
  100. 'from App\Entity\Attachment f ' .
  101. 'join App\Entity\Avatar a with f.id = a.attachment_id ' .
  102. 'where a.gsactor_id = :gsactor_id',
  103. ['gsactor_id' => $gsactor_id]);
  104. }));
  105. $res['file_path'] = \App\Entity\Avatar::getFilePathStatic($res['file_hash']);
  106. return $res;
  107. } catch (Exception $e) {
  108. $filepath = INSTALLDIR . '/public/assets/default-avatar.svg';
  109. return ['file_path' => $filepath, 'mimetype' => 'image/svg+xml', 'title' => null];
  110. }
  111. }
  112. }