Media.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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\Media;
  19. use App\Core\Cache;
  20. use App\Core\Event;
  21. use App\Core\Module;
  22. use App\Util\Common;
  23. use App\Util\Nickname;
  24. class Media extends Module
  25. {
  26. public static function __callStatic(string $name, array $args)
  27. {
  28. return Utils::{$name}(...$args);
  29. }
  30. public function onAddRoute($r)
  31. {
  32. $r->connect('avatar', '/{nickname<' . Nickname::DISPLAY_FMT . '>}/avatar/{size<full|big|medium|small>?full}', [Controller\Media::class, 'avatar']);
  33. $r->connect('attachment_inline', '/attachment/{id<\d+>}', [Controller\Media::class, 'attachment_inline']);
  34. return Event::next;
  35. }
  36. public function onEndTwigPopulateVars(array &$vars)
  37. {
  38. if (Common::user() != null) {
  39. $vars['user_avatar'] = self::getAvatarUrl();
  40. }
  41. return Event::next;
  42. }
  43. public function onGetAvatarUrl(string $nickname, ?string &$url)
  44. {
  45. $url = self::getAvatarUrl($nickname);
  46. return Event::next;
  47. }
  48. public function onDeleteCachedAvatar(string $nickname)
  49. {
  50. Cache::delete('avatar-' . $nickname);
  51. Cache::delete('avatar-url-' . $nickname);
  52. Cache::delete('avatar-file-info-' . $nickname);
  53. }
  54. }