Avatar.php 5.5 KB

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