Attachment.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\Attachment\Controller;
  20. use App\Core\Controller;
  21. use App\Core\DB\DB;
  22. use App\Core\Event;
  23. use App\Core\GSFile;
  24. use function App\Core\I18n\_m;
  25. use App\Entity\Note;
  26. use App\Util\Common;
  27. use App\Util\Exception\ClientException;
  28. use App\Util\Exception\NoSuchFileException;
  29. use App\Util\Exception\NotFoundException;
  30. use App\Util\Exception\ServerException;
  31. use Component\Attachment\Entity\AttachmentThumbnail;
  32. use Symfony\Component\HttpFoundation\HeaderUtils;
  33. use Symfony\Component\HttpFoundation\Request;
  34. use Symfony\Component\HttpFoundation\Response;
  35. use Symfony\Component\Mime\MimeTypes;
  36. class Attachment extends Controller
  37. {
  38. /**
  39. * Generic function that handles getting a representation for an attachment
  40. */
  41. private function attachment(int $attachment_id, Note|int $note, callable $handle)
  42. {
  43. $attachment = DB::findOneBy('attachment', ['id' => $attachment_id]);
  44. $note = \is_int($note) ? Note::getById($note) : $note;
  45. // Before anything, ensure proper scope
  46. if (!$note->isVisibleTo(Common::actor())) {
  47. throw new ClientException(_m('You don\'t have permissions to view this attachment.'), 401);
  48. }
  49. $res = null;
  50. if (Event::handle('AttachmentFileInfo', [$attachment, $note, &$res]) !== Event::stop) {
  51. // If no one else claims this attachment, use the default representation
  52. try {
  53. $res = GSFile::getAttachmentFileInfo($attachment_id);
  54. } catch (NoSuchFileException $e) {
  55. // Continue below
  56. }
  57. }
  58. if (empty($res)) {
  59. throw new ClientException(_m('No such attachment'), 404);
  60. } else {
  61. if (!\array_key_exists('filepath', $res)) {
  62. // @codeCoverageIgnoreStart
  63. throw new ServerException('This attachment is not stored locally.');
  64. // @codeCoverageIgnoreEnd
  65. } else {
  66. $res['attachment'] = $attachment;
  67. $res['note'] = $note;
  68. $res['title'] = $attachment->getBestTitle($note);
  69. return $handle($res);
  70. }
  71. }
  72. }
  73. /**
  74. * The page where the attachment and it's info is shown
  75. */
  76. public function attachmentShowWithNote(Request $request, int $note_id, int $attachment_id)
  77. {
  78. try {
  79. return $this->attachment($attachment_id, $note_id, function ($res) use ($note_id, $attachment_id) {
  80. return [
  81. '_template' => '/cards/attachments/show.html.twig',
  82. 'download' => $res['attachment']->getDownloadUrl(note: $note_id),
  83. 'title' => $res['title'],
  84. 'attachment' => $res['attachment'],
  85. 'note' => $res['note'],
  86. 'right_panel_vars' => ['attachment_id' => $attachment_id, 'note_id' => $note_id],
  87. ];
  88. });
  89. } catch (NotFoundException) {
  90. throw new ClientException(_m('No such attachment.'), 404);
  91. }
  92. }
  93. /**
  94. * Display the attachment inline
  95. */
  96. public function attachmentViewWithNote(Request $request, int $note_id, int $attachment_id)
  97. {
  98. return $this->attachment(
  99. $attachment_id,
  100. $note_id,
  101. fn (array $res) => GSFile::sendFile(
  102. $res['filepath'],
  103. $res['mimetype'],
  104. GSFile::ensureFilenameWithProperExtension($res['title'], $res['mimetype']) ?? $res['filename'],
  105. HeaderUtils::DISPOSITION_INLINE,
  106. ),
  107. );
  108. }
  109. public function attachmentDownloadWithNote(Request $request, int $note_id, int $attachment_id)
  110. {
  111. return $this->attachment(
  112. $attachment_id,
  113. $note_id,
  114. fn (array $res) => GSFile::sendFile(
  115. $res['filepath'],
  116. $res['mimetype'],
  117. GSFile::ensureFilenameWithProperExtension($res['title'], $res['mimetype']) ?? $res['filename'],
  118. HeaderUtils::DISPOSITION_ATTACHMENT,
  119. ),
  120. );
  121. }
  122. /**
  123. * Controller to produce a thumbnail for a given attachment id
  124. *
  125. * @param int $attachment_id Attachment ID
  126. *
  127. * @throws \App\Util\Exception\DuplicateFoundException
  128. * @throws ClientException
  129. * @throws NotFoundException
  130. * @throws ServerException
  131. */
  132. public function attachmentThumbnailWithNote(Request $request, int $note_id, int $attachment_id, string $size = 'small'): Response
  133. {
  134. // Before anything, ensure proper scope
  135. if (!Note::getById($note_id)->isVisibleTo(Common::actor())) {
  136. throw new ClientException(_m('You don\'t have permissions to view this thumbnail.'), 401);
  137. }
  138. $attachment = DB::findOneBy('attachment', ['id' => $attachment_id]);
  139. $crop = Common::config('thumbnail', 'smart_crop');
  140. $thumbnail = AttachmentThumbnail::getOrCreate(attachment: $attachment, size: $size, crop: $crop);
  141. if (\is_null($thumbnail)) {
  142. throw new ClientException(_m('Can not generate thumbnail for attachment with id={id}', ['id' => $attachment->getId()]));
  143. }
  144. $filename = $thumbnail->getFilename();
  145. $path = $thumbnail->getPath();
  146. $mimetype = $thumbnail->getMimetype();
  147. return GSFile::sendFile(filepath: $path, mimetype: $mimetype, output_filename: $filename . '.' . MimeTypes::getDefault()->getExtensions($mimetype)[0], disposition: HeaderUtils::DISPOSITION_INLINE);
  148. }
  149. }