2021-04-19 16:28:53 +09:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 17:26:18 +09:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-04-19 16:28:53 +09:00
|
|
|
// {{{ License
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// }}}
|
|
|
|
|
2021-12-03 00:12:31 +09:00
|
|
|
namespace Component\Attachment;
|
2021-04-19 16:28:53 +09:00
|
|
|
|
2021-12-10 11:38:04 +09:00
|
|
|
use App\Core\Cache;
|
2022-03-27 23:19:09 +09:00
|
|
|
use App\Core\DB;
|
2021-12-03 00:12:31 +09:00
|
|
|
use App\Core\Event;
|
|
|
|
use App\Core\Modules\Component;
|
2022-03-28 00:43:59 +09:00
|
|
|
use App\Core\Router;
|
2021-12-24 18:27:24 +09:00
|
|
|
use App\Entity\Actor;
|
2021-12-10 11:38:04 +09:00
|
|
|
use App\Entity\Note;
|
2021-12-12 05:57:48 +09:00
|
|
|
use App\Util\Formatting;
|
2021-12-03 00:12:31 +09:00
|
|
|
use Component\Attachment\Controller as C;
|
2021-12-09 05:35:59 +09:00
|
|
|
use Component\Attachment\Entity as E;
|
2021-12-12 05:57:48 +09:00
|
|
|
use Doctrine\Common\Collections\ExpressionBuilder;
|
|
|
|
use Doctrine\ORM\Query\Expr;
|
|
|
|
use Doctrine\ORM\QueryBuilder;
|
2021-04-19 16:28:53 +09:00
|
|
|
|
2021-12-03 00:12:31 +09:00
|
|
|
class Attachment extends Component
|
2021-04-19 16:28:53 +09:00
|
|
|
{
|
2022-03-28 00:43:59 +09:00
|
|
|
public function onAddRoute(Router $r): bool
|
2021-04-19 16:28:53 +09:00
|
|
|
{
|
2021-12-27 11:47:04 +09:00
|
|
|
$r->connect('note_attachment_show', '/object/note/{note_id<\d+>}/attachment/{attachment_id<\d+>}', [C\Attachment::class, 'attachmentShowWithNote']);
|
|
|
|
$r->connect('note_attachment_view', '/object/note/{note_id<\d+>}/attachment/{attachment_id<\d+>}/view', [C\Attachment::class, 'attachmentViewWithNote']);
|
|
|
|
$r->connect('note_attachment_download', '/object/note/{note_id<\d+>}/attachment/{attachment_id<\d+>}/download', [C\Attachment::class, 'attachmentDownloadWithNote']);
|
|
|
|
$r->connect('note_attachment_thumbnail', '/object/note/{note_id<\d+>}/attachment/{attachment_id<\d+>}/thumbnail/{size<big|medium|small>}', [C\Attachment::class, 'attachmentThumbnailWithNote']);
|
2021-12-03 00:12:31 +09:00
|
|
|
return Event::next;
|
2021-04-19 16:28:53 +09:00
|
|
|
}
|
2021-12-09 05:35:59 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a unique representation of a file on disk
|
|
|
|
*
|
|
|
|
* This can be used in the future to deduplicate images by visual content
|
|
|
|
*/
|
|
|
|
public function onHashFile(string $filename, ?string &$out_hash): bool
|
|
|
|
{
|
|
|
|
$out_hash = hash_file(E\Attachment::FILEHASH_ALGO, $filename);
|
|
|
|
return Event::stop;
|
|
|
|
}
|
2021-12-10 11:38:04 +09:00
|
|
|
|
2021-12-28 15:15:39 +09:00
|
|
|
public function onNoteDeleteRelated(Note &$note, Actor $actor): bool
|
2021-12-10 11:38:04 +09:00
|
|
|
{
|
|
|
|
Cache::delete("note-attachments-{$note->getId()}");
|
2021-12-12 05:57:48 +09:00
|
|
|
foreach ($note->getAttachments() as $attachment) {
|
2021-12-10 11:38:04 +09:00
|
|
|
$attachment->kill();
|
|
|
|
}
|
2021-12-28 15:15:39 +09:00
|
|
|
DB::wrapInTransaction(fn () => E\AttachmentToNote::removeWhereNoteId($note->getId()));
|
|
|
|
Cache::delete("note-attachments-{$note->getId()}");
|
2021-12-10 11:38:04 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
2021-12-12 05:57:48 +09:00
|
|
|
|
2022-01-09 00:10:39 +09:00
|
|
|
public function onCollectionQueryAddJoins(QueryBuilder &$note_qb, QueryBuilder &$actor_qb): bool
|
2021-12-12 05:57:48 +09:00
|
|
|
{
|
2022-03-07 23:08:07 +09:00
|
|
|
if (!\in_array('attachment_to_note', $note_qb->getAllAliases())) {
|
|
|
|
$note_qb->leftJoin(
|
|
|
|
join: E\AttachmentToNote::class,
|
|
|
|
alias: 'attachment_to_note',
|
|
|
|
conditionType: Expr\Join::WITH,
|
|
|
|
condition: 'note.id = attachment_to_note.note_id',
|
|
|
|
);
|
|
|
|
}
|
2021-12-12 05:57:48 +09:00
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-12-16 19:52:06 +09:00
|
|
|
/**
|
|
|
|
* Populate $note_expr with the criteria for looking for notes with attachments
|
|
|
|
*/
|
2022-01-12 05:28:15 +09:00
|
|
|
public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $locale, ?Actor $actor, &$note_expr, &$actor_expr): bool
|
2021-12-12 05:57:48 +09:00
|
|
|
{
|
|
|
|
$include_term = str_contains($term, ':') ? explode(':', $term)[1] : $term;
|
|
|
|
if (Formatting::startsWith($term, ['note-types:', 'notes-incude:', 'note-filter:'])) {
|
|
|
|
if (\is_null($note_expr)) {
|
|
|
|
$note_expr = [];
|
|
|
|
}
|
|
|
|
if (array_intersect(explode(',', $include_term), ['media', 'image', 'images', 'attachment']) !== []) {
|
|
|
|
$note_expr[] = $eb->neq('attachment_to_note.note_id', null);
|
|
|
|
} else {
|
|
|
|
$note_expr[] = $eb->eq('attachment_to_note.note_id', null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Event::next;
|
|
|
|
}
|
2021-04-19 16:28:53 +09:00
|
|
|
}
|