Attachment.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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;
  20. use App\Core\Cache;
  21. use App\Core\DB\DB;
  22. use App\Core\Event;
  23. use App\Core\Modules\Component;
  24. use App\Core\Router\RouteLoader;
  25. use App\Entity\Actor;
  26. use App\Entity\Note;
  27. use App\Util\Formatting;
  28. use Component\Attachment\Controller as C;
  29. use Component\Attachment\Entity as E;
  30. use Doctrine\Common\Collections\ExpressionBuilder;
  31. use Doctrine\ORM\Query\Expr;
  32. use Doctrine\ORM\QueryBuilder;
  33. class Attachment extends Component
  34. {
  35. public function onAddRoute(RouteLoader $r): bool
  36. {
  37. $r->connect('note_attachment_show', '/object/note/{note_id<\d+>}/attachment/{attachment_id<\d+>}', [C\Attachment::class, 'attachmentShowWithNote']);
  38. $r->connect('note_attachment_view', '/object/note/{note_id<\d+>}/attachment/{attachment_id<\d+>}/view', [C\Attachment::class, 'attachmentViewWithNote']);
  39. $r->connect('note_attachment_download', '/object/note/{note_id<\d+>}/attachment/{attachment_id<\d+>}/download', [C\Attachment::class, 'attachmentDownloadWithNote']);
  40. $r->connect('note_attachment_thumbnail', '/object/note/{note_id<\d+>}/attachment/{attachment_id<\d+>}/thumbnail/{size<big|medium|small>}', [C\Attachment::class, 'attachmentThumbnailWithNote']);
  41. return Event::next;
  42. }
  43. /**
  44. * Get a unique representation of a file on disk
  45. *
  46. * This can be used in the future to deduplicate images by visual content
  47. */
  48. public function onHashFile(string $filename, ?string &$out_hash): bool
  49. {
  50. $out_hash = hash_file(E\Attachment::FILEHASH_ALGO, $filename);
  51. return Event::stop;
  52. }
  53. public function onNoteDeleteRelated(Note &$note, Actor $actor): bool
  54. {
  55. Cache::delete("note-attachments-{$note->getId()}");
  56. foreach ($note->getAttachments() as $attachment) {
  57. $attachment->kill();
  58. }
  59. DB::wrapInTransaction(fn () => E\AttachmentToNote::removeWhereNoteId($note->getId()));
  60. Cache::delete("note-attachments-{$note->getId()}");
  61. return Event::next;
  62. }
  63. public function onCollectionQueryAddJoins(QueryBuilder &$note_qb, QueryBuilder &$actor_qb): bool
  64. {
  65. $note_qb->leftJoin(E\AttachmentToNote::class, 'attachment_to_note', Expr\Join::WITH, 'note.id = attachment_to_note.note_id');
  66. return Event::next;
  67. }
  68. /**
  69. * Populate $note_expr with the criteria for looking for notes with attachments
  70. */
  71. public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $locale, ?Actor $actor, &$note_expr, &$actor_expr): bool
  72. {
  73. $include_term = str_contains($term, ':') ? explode(':', $term)[1] : $term;
  74. if (Formatting::startsWith($term, ['note-types:', 'notes-incude:', 'note-filter:'])) {
  75. if (\is_null($note_expr)) {
  76. $note_expr = [];
  77. }
  78. if (array_intersect(explode(',', $include_term), ['media', 'image', 'images', 'attachment']) !== []) {
  79. $note_expr[] = $eb->neq('attachment_to_note.note_id', null);
  80. } else {
  81. $note_expr[] = $eb->eq('attachment_to_note.note_id', null);
  82. }
  83. }
  84. return Event::next;
  85. }
  86. }