2021-04-19 08:28:53 +01:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-04-19 08:28:53 +01: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-02 15:12:31 +00:00
|
|
|
namespace Component\Attachment;
|
2021-04-19 08:28:53 +01:00
|
|
|
|
2021-12-10 02:38:04 +00:00
|
|
|
use App\Core\Cache;
|
2021-12-28 06:15:39 +00:00
|
|
|
use App\Core\DB\DB;
|
2021-12-02 15:12:31 +00:00
|
|
|
use App\Core\Event;
|
|
|
|
use App\Core\Modules\Component;
|
2021-04-19 08:28:53 +01:00
|
|
|
use App\Core\Router\RouteLoader;
|
2021-12-24 09:27:24 +00:00
|
|
|
use App\Entity\Actor;
|
2021-12-10 02:38:04 +00:00
|
|
|
use App\Entity\Note;
|
2021-12-11 20:57:48 +00:00
|
|
|
use App\Util\Formatting;
|
2021-12-02 15:12:31 +00:00
|
|
|
use Component\Attachment\Controller as C;
|
2021-12-08 20:35:59 +00:00
|
|
|
use Component\Attachment\Entity as E;
|
2021-12-11 20:57:48 +00:00
|
|
|
use Doctrine\Common\Collections\ExpressionBuilder;
|
|
|
|
use Doctrine\ORM\Query\Expr;
|
|
|
|
use Doctrine\ORM\QueryBuilder;
|
2021-04-19 08:28:53 +01:00
|
|
|
|
2021-12-02 15:12:31 +00:00
|
|
|
class Attachment extends Component
|
2021-04-19 08:28:53 +01:00
|
|
|
{
|
2021-12-02 15:12:31 +00:00
|
|
|
public function onAddRoute(RouteLoader $r): bool
|
2021-04-19 08:28:53 +01:00
|
|
|
{
|
2021-12-27 02:47:04 +00: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-02 15:12:31 +00:00
|
|
|
return Event::next;
|
2021-04-19 08:28:53 +01:00
|
|
|
}
|
2021-12-08 20:35:59 +00: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 02:38:04 +00:00
|
|
|
|
2021-12-28 06:15:39 +00:00
|
|
|
public function onNoteDeleteRelated(Note &$note, Actor $actor): bool
|
2021-12-10 02:38:04 +00:00
|
|
|
{
|
|
|
|
Cache::delete("note-attachments-{$note->getId()}");
|
2021-12-11 20:57:48 +00:00
|
|
|
foreach ($note->getAttachments() as $attachment) {
|
2021-12-10 02:38:04 +00:00
|
|
|
$attachment->kill();
|
|
|
|
}
|
2021-12-28 06:15:39 +00:00
|
|
|
DB::wrapInTransaction(fn () => E\AttachmentToNote::removeWhereNoteId($note->getId()));
|
|
|
|
Cache::delete("note-attachments-{$note->getId()}");
|
2021-12-10 02:38:04 +00:00
|
|
|
return Event::next;
|
|
|
|
}
|
2021-12-11 20:57:48 +00:00
|
|
|
|
2022-01-08 15:10:39 +00:00
|
|
|
public function onCollectionQueryAddJoins(QueryBuilder &$note_qb, QueryBuilder &$actor_qb): bool
|
2021-12-11 20:57:48 +00:00
|
|
|
{
|
|
|
|
$note_qb->leftJoin(E\AttachmentToNote::class, 'attachment_to_note', Expr\Join::WITH, 'note.id = attachment_to_note.note_id');
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-12-16 10:52:06 +00:00
|
|
|
/**
|
|
|
|
* Populate $note_expr with the criteria for looking for notes with attachments
|
|
|
|
*/
|
2022-01-08 15:10:39 +00:00
|
|
|
public function onCollectionQueryCreateExpression(ExpressionBuilder $eb, string $term, ?string $language, ?Actor $actor, &$note_expr, &$actor_expr): bool
|
2021-12-11 20:57:48 +00: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 08:28:53 +01:00
|
|
|
}
|