Cover.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 Plugin\Cover\Controller;
  20. use App\Core\DB\DB;
  21. use App\Core\Form;
  22. use App\Core\GSFile;
  23. use function App\Core\I18n\_m;
  24. use App\Util\Common;
  25. use App\Util\Exception\ClientException;
  26. use App\Util\Exception\RedirectException;
  27. use App\Util\Exception\ServerException;
  28. use Plugin\Cover\Entity\Cover as CoverEntity;
  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. * @throws ClientException Invalid form
  52. * @throws ServerException Invalid file type
  53. *
  54. * @return array template
  55. */
  56. public static function coverSettings(Request $request): array
  57. {
  58. $user = Common::user();
  59. $actor_id = $user->getId();
  60. $form = Form::create([
  61. ['cover', FileType::class, ['label' => _m('Cover'), 'help' => _m('You can upload your personal cover. The maximum file size is 2MB.'),
  62. 'constraints' => [
  63. new F([
  64. 'maxSize' => '2048k',
  65. 'mimeTypes' => [
  66. 'image/gif',
  67. 'image/png',
  68. 'image/jpeg',
  69. 'image/bmp',
  70. 'image/webp',
  71. ],
  72. 'maxSizeMessage' => 'Image exceeded maximum size',
  73. 'mimeTypesMessage' => 'Please upload a valid image',
  74. ]), ], ]],
  75. ['hidden', HiddenType::class, []],
  76. ['save_color', SubmitType::class, ['label' => _m('Submit')]],
  77. ]);
  78. $form->handleRequest($request);
  79. if ($form->isSubmitted() && $form->isValid()) {
  80. $data = $form->getData();
  81. if (isset($data['cover'])) {
  82. $sfile = $data['cover'];
  83. } else {
  84. throw new ClientException('Invalid form');
  85. }
  86. if (explode('/', $sfile->getMimeType())[0] != 'image') {
  87. throw new ServerException('Invalid file type');
  88. }
  89. $file = GSFile::storeFileAsAttachment($sfile);
  90. $old_file = null;
  91. $cover = DB::find('cover', ['gctor_id' => $actor_id]);
  92. // Must get old id before inserting another one
  93. if ($cover != null) {
  94. $old_file = $cover->delete();
  95. DB::remove($cover);
  96. }
  97. DB::persist($file);
  98. // Can only get new id after inserting
  99. DB::flush();
  100. $cover = CoverEntity::create(['actor_id' => $actor_id, 'file_id' => $file->getId()]);
  101. DB::persist($cover);
  102. DB::flush();
  103. // Only delete files if the commit went through
  104. if ($old_file != null) {
  105. @unlink($old_file);
  106. }
  107. throw new RedirectException();
  108. }
  109. $removeForm = null;
  110. $cover = DB::find('cover', ['actor_id' => $actor_id]);
  111. if ($cover != null) {
  112. $form2 = Form::create([
  113. ['remove', SubmitType::class, ['label' => _m('Remove')]],
  114. ]);
  115. $form2->handleRequest($request);
  116. if ($form2->isSubmitted() && $form2->isValid()) {
  117. $old_file = $cover->delete();
  118. DB::remove($cover);
  119. DB::flush();
  120. @unlink($old_file);
  121. throw new RedirectException();
  122. }
  123. $removeForm = $form2->createView();
  124. }
  125. return ['_template' => 'cover/cover.html.twig', 'cover' => $form->createView(), 'cover_remove_form' => $removeForm];
  126. }
  127. /**
  128. * get user cover
  129. *
  130. * @return mixed cover file
  131. */
  132. public function cover(): mixed
  133. {
  134. // $cover = DB::find('cover', ['actor_id' => Common::user()->getId()]);
  135. // if ($cover == null) {
  136. // return new Response('Cover not found',Response::HTTP_NOT_FOUND);
  137. // }
  138. // $file = $cover->getFile();
  139. // if ($file == null) {
  140. // return new Response('Cover File not found',Response::HTTP_NOT_FOUND);
  141. // }
  142. // return GSFile::sendFile($cover->getFilePath(), $file->getMimetype(), $file->getTitle());
  143. return new Response('Cover File not found', Response::HTTP_NOT_FOUND);
  144. }
  145. }