Avatar.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Component\Avatar\Controller;
  20. use App\Core\Controller;
  21. use App\Core\DB\DB;
  22. use App\Core\Event;
  23. use App\Core\Form;
  24. use App\Core\GSFile;
  25. use App\Core\GSFile as M;
  26. use function App\Core\I18n\_m;
  27. use App\Core\Log;
  28. use App\Util\Common;
  29. use App\Util\Exception\ClientException;
  30. use App\Util\TemporaryFile;
  31. use Component\Avatar\Entity\Avatar as AvatarEntity;
  32. use Exception;
  33. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  34. use Symfony\Component\Form\Extension\Core\Type\FileType;
  35. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  36. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  37. use Symfony\Component\Form\FormError;
  38. use Symfony\Component\HttpFoundation\Request;
  39. use Symfony\Component\HttpFoundation\Response;
  40. class Avatar extends Controller
  41. {
  42. public function default_avatar_view(Request $request, string $size): Response
  43. {
  44. return $this->avatar_view($request, 0, $size);
  45. }
  46. /**
  47. * @throws Exception
  48. */
  49. public function avatar_view(Request $request, int $actor_id, string $size): Response
  50. {
  51. $res = \Component\Avatar\Avatar::getAvatarFileInfo($actor_id, $size);
  52. return M::sendFile($res['filepath'], $res['mimetype'], $res['title']);
  53. }
  54. /**
  55. * Local user avatar panel
  56. */
  57. public static function settings_avatar(Request $request): array
  58. {
  59. $form = Form::create([
  60. ['avatar', FileType::class, ['label' => _m('Avatar'), 'help' => _m('You can upload your personal avatar. The maximum file size is 2MB.'), 'multiple' => false, 'required' => false]],
  61. ['remove', CheckboxType::class, ['label' => _m('Remove avatar'), 'help' => _m('Remove your avatar and use the default one'), 'required' => false, 'value' => false]],
  62. ['hidden', HiddenType::class, []],
  63. ['save_avatar', SubmitType::class, ['label' => _m('Submit')]],
  64. ]);
  65. $form->handleRequest($request);
  66. if ($form->isSubmitted() && $form->isValid()) {
  67. $data = $form->getData();
  68. $user = Common::user();
  69. $actor_id = $user->getId();
  70. if ($data['remove'] == true) {
  71. if (\is_null($avatar = DB::findOneBy(AvatarEntity::class, ['actor_id' => $actor_id], return_null: true))) {
  72. $form->addError(new FormError(_m('No avatar set, so cannot delete.')));
  73. } else {
  74. $avatar->delete();
  75. }
  76. } else {
  77. $attachment = null;
  78. $title = $data['avatar']?->getClientOriginalName() ?? null;
  79. if (isset($data['hidden'])) {
  80. // Cropped client side
  81. $matches = [];
  82. if (!empty(preg_match('/data:([^;]*)(;(base64))?,(.*)/', $data['hidden'], $matches))) {
  83. [, , , $encoding_user, $data_user] = $matches;
  84. if ($encoding_user === 'base64') {
  85. $data_user = base64_decode($data_user);
  86. $tempfile = new TemporaryFile(['prefix' => 'gs-avatar']);
  87. $tempfile->write($data_user);
  88. $attachment = GSFile::storeFileAsAttachment($tempfile);
  89. } else {
  90. Log::info('Avatar upload got an invalid encoding, something\'s fishy and/or wrong');
  91. }
  92. }
  93. } elseif (isset($data['avatar'])) {
  94. // Cropping failed (e.g. disabled js), use file as uploaded
  95. $file = $data['avatar'];
  96. $attachment = GSFile::storeFileAsAttachment($file);
  97. } else {
  98. throw new ClientException(_m('Invalid form.'));
  99. }
  100. // Delete current avatar if there's one
  101. if (!\is_null($avatar = DB::findOneBy(AvatarEntity::class, ['actor_id' => $actor_id], return_null: true))) {
  102. $avatar->delete();
  103. }
  104. DB::persist($attachment);
  105. DB::persist(AvatarEntity::create([
  106. 'actor_id' => $actor_id,
  107. 'attachment_id' => $attachment->getId(),
  108. 'title' => $title,
  109. ]));
  110. DB::flush();
  111. Event::handle('AvatarUpdate', [$user->getId()]);
  112. }
  113. }
  114. return ['_template' => 'avatar/settings.html.twig', 'avatar' => $form->createView()];
  115. }
  116. }