Cover.php 5.5 KB

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