Cover.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 Plugin\Cover\Controller;
  19. use App\Core\DB\DB;
  20. use App\Core\Form;
  21. use function App\Core\I18n\_m;
  22. use App\Entity\Cover as CoverEntity;
  23. use App\Util\Common;
  24. use App\Util\Exception\ClientException;
  25. use App\Util\Exception\RedirectException;
  26. use App\Util\Exception\ServerException;
  27. use Component\Media\Attachment;
  28. use Component\Media\Attachment as M;
  29. use Symfony\Component\Form\Extension\Core\Type\FileType;
  30. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  31. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  32. use Symfony\Component\HttpFoundation\Request;
  33. use Symfony\Component\HttpFoundation\Response;
  34. use Symfony\Component\Validator\Constraints\File as F;
  35. /**
  36. * Cover controller
  37. *
  38. * @package GNUsocial
  39. * @category CoverPlugin
  40. *
  41. * @author Daniel Brandao <up201705812@fe.up.pt>
  42. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  43. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  44. */
  45. class Cover
  46. {
  47. /**
  48. * Display and handle the cover edit page, where a user can add or
  49. * edit their cover image
  50. *
  51. * @param Request $request
  52. *
  53. * @throws ClientException Invalid form
  54. * @throws ServerException Invalid file type
  55. *
  56. * @return array template
  57. */
  58. public function coverSettings(Request $request)
  59. {
  60. $user = Common::user();
  61. $actor_id = $user->getId();
  62. $form = Form::create([
  63. ['cover', FileType::class, ['label' => _m('Cover'), 'help' => _m('You can upload your personal cover. The maximum file size is 2MB.'),
  64. 'constraints' => [
  65. new F([
  66. 'maxSize' => '2048k',
  67. 'mimeTypes' => [
  68. 'image/gif',
  69. 'image/png',
  70. 'image/jpeg',
  71. 'image/bmp',
  72. 'image/webp',
  73. ],
  74. 'maxSizeMessage' => 'Image exceeded maximum size',
  75. 'mimeTypesMessage' => 'Please upload a valid image',
  76. ]), ], ]],
  77. ['hidden', HiddenType::class, []],
  78. ['save', SubmitType::class, ['label' => _m('Submit')]],
  79. ]);
  80. $form->handleRequest($request);
  81. if ($form->isSubmitted() && $form->isValid()) {
  82. $data = $form->getData();
  83. if (isset($data['cover'])) {
  84. $sfile = $data['cover'];
  85. } else {
  86. throw new ClientException('Invalid form');
  87. }
  88. if (explode('/',$sfile->getMimeType())[0] != 'image') {
  89. throw new ServerException('Invalid file type');
  90. }
  91. $file = Attachment::validateAndStoreFile($sfile, Common::config('cover', 'dir'), $title = null, $is_local = true, $use_unique = $actor_id);
  92. $old_file = null;
  93. $cover = DB::find('cover', ['gsactor_id' => $actor_id]);
  94. // Must get old id before inserting another one
  95. if ($cover != null) {
  96. $old_file = $cover->delete();
  97. DB::remove($cover);
  98. }
  99. DB::persist($file);
  100. // Can only get new id after inserting
  101. DB::flush();
  102. $cover = CoverEntity::create(['gsactor_id' => $actor_id, 'file_id' => $file->getId()]);
  103. DB::persist($cover);
  104. DB::flush();
  105. // Only delete files if the commit went through
  106. if ($old_file != null) {
  107. @unlink($old_file);
  108. }
  109. throw new RedirectException();
  110. }
  111. $removeForm = null;
  112. $cover = DB::find('cover', ['gsactor_id' => $actor_id]);
  113. if ($cover != null) {
  114. $form2 = Form::create([
  115. ['remove', SubmitType::class, ['label' => _m('Remove')]],
  116. ]);
  117. $form2->handleRequest($request);
  118. if ($form2->isSubmitted() && $form2->isValid()) {
  119. $old_file = $cover->delete();
  120. DB::remove($cover);
  121. DB::flush();
  122. @unlink($old_file);
  123. throw new RedirectException();
  124. }
  125. $removeForm = $form2->createView();
  126. }
  127. return ['_template' => 'cover/cover.html.twig', 'form' => $form->createView(), 'remove_form' => $removeForm];
  128. }
  129. /**
  130. * get user cover
  131. *
  132. * @return mixed cover file
  133. */
  134. public function cover()
  135. {
  136. $cover = DB::find('cover', ['gsactor_id' => Common::user()->getId()]);
  137. if ($cover == null) {
  138. return new Response('Cover not found',Response::HTTP_NOT_FOUND);
  139. }
  140. $file = $cover->getFile();
  141. if ($file == null) {
  142. return new Response('Cover File not found',Response::HTTP_NOT_FOUND);
  143. }
  144. return M::sendFile($cover->getFilePath(), $file->getMimetype(), $file->getTitle());
  145. }
  146. }