Avatar.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\Exception\NotFoundException;
  31. use App\Util\TemporaryFile;
  32. use Component\Avatar\Entity\Avatar as AvatarEntity;
  33. use Exception;
  34. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  35. use Symfony\Component\Form\Extension\Core\Type\FileType;
  36. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  37. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  38. use Symfony\Component\Form\FormError;
  39. use Symfony\Component\HttpFoundation\Request;
  40. use Symfony\Component\HttpFoundation\Response;
  41. class Avatar extends Controller
  42. {
  43. public function default_avatar_view(Request $request, string $size): Response
  44. {
  45. return $this->avatar_view($request, 0, $size);
  46. }
  47. /**
  48. * @throws Exception
  49. */
  50. public function avatar_view(Request $request, int $actor_id, string $size): Response
  51. {
  52. switch ($size) {
  53. case 'full':
  54. $res = \Component\Avatar\Avatar::getAvatarFileInfo($actor_id);
  55. return M::sendFile($res['filepath'], $res['mimetype'], $res['title']);
  56. default:
  57. throw new Exception('Not implemented');
  58. }
  59. }
  60. /**
  61. * Local user avatar panel
  62. */
  63. public static function settings_avatar(Request $request): array
  64. {
  65. $form = Form::create([
  66. ['avatar', FileType::class, ['label' => _m('Avatar'), 'help' => _m('You can upload your personal avatar. The maximum file size is 2MB.'), 'multiple' => false, 'required' => false]],
  67. ['remove', CheckboxType::class, ['label' => _m('Remove avatar'), 'help' => _m('Remove your avatar and use the default one'), 'required' => false, 'value' => false]],
  68. ['hidden', HiddenType::class, []],
  69. ['save_avatar', SubmitType::class, ['label' => _m('Submit')]],
  70. ]);
  71. $form->handleRequest($request);
  72. if ($form->isSubmitted() && $form->isValid()) {
  73. $data = $form->getData();
  74. $user = Common::user();
  75. $actor_id = $user->getId();
  76. if ($data['remove'] == true) {
  77. try {
  78. $avatar = DB::findOneBy('avatar', ['actor_id' => $actor_id]);
  79. $avatar->delete();
  80. Event::handle('AvatarUpdate', [$user->getId()]);
  81. } catch (NotFoundException) {
  82. $form->addError(new FormError(_m('No avatar set, so cannot delete')));
  83. }
  84. } else {
  85. $attachment = null;
  86. if (isset($data['hidden'])) {
  87. // Cropped client side
  88. $matches = [];
  89. if (!empty(preg_match('/data:([^;]*)(;(base64))?,(.*)/', $data['hidden'], $matches))) {
  90. [, , , $encoding_user, $data_user] = $matches;
  91. if ($encoding_user === 'base64') {
  92. $data_user = base64_decode($data_user);
  93. $tempfile = new TemporaryFile(['prefix' => 'gs-avatar']);
  94. $tempfile->write($data_user);
  95. $attachment = GSFile::storeFileAsAttachment($tempfile);
  96. } else {
  97. Log::info('Avatar upload got an invalid encoding, something\'s fishy and/or wrong');
  98. }
  99. }
  100. } elseif (isset($data['avatar'])) {
  101. // Cropping failed (e.g. disabled js), use file as uploaded
  102. $file = $data['avatar'];
  103. $attachment = GSFile::storeFileAsAttachment($file);
  104. } else {
  105. throw new ClientException('Invalid form');
  106. }
  107. // Delete current avatar if there's one
  108. $avatar = DB::find('avatar', ['actor_id' => $actor_id]);
  109. $avatar?->delete();
  110. DB::persist($attachment);
  111. // Can only get new id after inserting
  112. DB::flush();
  113. DB::persist(AvatarEntity::create(['actor_id' => $actor_id, 'attachment_id' => $attachment->getId()]));
  114. DB::flush();
  115. Event::handle('AvatarUpdate', [$user->getId()]);
  116. }
  117. }
  118. return ['_template' => 'avatar/settings.html.twig', 'avatar' => $form->createView()];
  119. }
  120. }