From 4cc4523632d725fb64820f391ce31b7d4c427274 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Thu, 12 Aug 2021 04:41:00 +0100 Subject: [PATCH] [Posting] Re-add original file to attachment on upload, if it was previously removed --- components/Posting/Posting.php | 4 +- src/Core/GSFile.php | 15 +++++++ src/Entity/Attachment.php | 72 ++++++++++++++++++---------------- 3 files changed, 56 insertions(+), 35 deletions(-) diff --git a/components/Posting/Posting.php b/components/Posting/Posting.php index 5f6b58579e..3a411e9a2b 100644 --- a/components/Posting/Posting.php +++ b/components/Posting/Posting.php @@ -129,7 +129,9 @@ END; DB::flush(); if ($processed_attachments != []) { foreach ($processed_attachments as $a) { - DB::persist(GSActorToAttachment::create(['attachment_id' => $a->getId(), 'gsactor_id' => $actor_id])); + if (is_null(DB::findBy('gsactor_to_attachment', ['attachment_id' => $a->getId(), 'gsactor_id' => $actor_id]))) { + DB::persist(GSActorToAttachment::create(['attachment_id' => $a->getId(), 'gsactor_id' => $actor_id])); + } DB::persist(AttachmentToNote::create(['attachment_id' => $a->getId(), 'note_id' => $note->getId()])); } DB::flush(); diff --git a/src/Core/GSFile.php b/src/Core/GSFile.php index 43e127f871..85648cb8c3 100644 --- a/src/Core/GSFile.php +++ b/src/Core/GSFile.php @@ -67,8 +67,23 @@ class GSFile Event::handle('HashFile', [$file->getPathname(), &$hash]); try { $attachment = DB::findOneBy('attachment', ['filehash' => $hash]); + // Attachment Exists $attachment->livesIncrementAndGet(); + if (is_null($attachment->getFilename())) { + $mimetype = $attachment->getMimetype(); + $width = $attachment->getWidth(); + $height = $attachment->getHeight(); + Event::handle('AttachmentSanitization', [&$file, &$mimetype, &$width, &$height]); + $attachment->setFilename($hash); + $attachment->setMimetype($mimetype); + $attachment->setWidth($width); + $attachment->setHeight($height); + $attachment->setSize($file->getSize()); + $file->move(Common::config('attachments', 'dir'), $hash); + DB::persist($attachment); + } } catch (NotFoundException) { + // Create an Attachment // The following properly gets the mimetype with `file` or other // available methods, so should be safe $mimetype = $file->getMimeType(); diff --git a/src/Entity/Attachment.php b/src/Entity/Attachment.php index 17d5a3f41f..05ac40980c 100644 --- a/src/Entity/Attachment.php +++ b/src/Entity/Attachment.php @@ -24,6 +24,9 @@ namespace App\Entity; use App\Core\DB\DB; use App\Core\Entity; use App\Core\GSFile; +use App\Util\Exception\DuplicateFoundException; +use App\Util\Exception\NotFoundException; +use App\Util\Exception\ServerException; use function App\Core\I18n\_m; use App\Core\Log; use App\Util\Common; @@ -56,7 +59,7 @@ class Attachment extends Entity private ?int $size; private ?int $width; private ?int $height; - private \DateTimeInterface $modified; + private DateTimeInterface $modified; public function setId(int $id): self { @@ -130,39 +133,6 @@ class Attachment extends Entity return $this->filename; } - /** - * TODO: Maybe this isn't the best way of handling titles - * - * @param null|Note $note - * - * @throws \App\Util\Exception\DuplicateFoundException - * @throws \App\Util\Exception\NotFoundException - * @throws \App\Util\Exception\ServerException - * - * @return string - */ - public function getBestTitle(?Note $note = null): string - { - // If we have a note, then the best title is the title itself - if (!is_null(($note))) { - $attachment_to_note = DB::findOneBy('attachment_to_note', [ - 'attachment_id' => $this->getId(), - 'note_id' => $note->getId(), - ]); - if (!is_null($attachment_to_note->getTitle())) { - return $attachment_to_note->getTitle(); - } - } - // Else - if (!is_null($filename = $this->getFilename())) { - // A filename would do just as well - return $filename; - } else { - // Welp - return _m('Untitled Attachment.'); - } - } - public function setSize(?int $size): self { $this->size = $size; @@ -250,6 +220,7 @@ class Attachment extends Entity return false; } else { $this->setFilename(null); + $this->setSize(null); DB::persist($this); DB::flush(); } @@ -291,6 +262,39 @@ class Attachment extends Entity return true; } + /** + * TODO: Maybe this isn't the best way of handling titles + * + * @param null|Note $note + * + * @throws DuplicateFoundException + * @throws NotFoundException + * @throws ServerException + * + * @return string + */ + public function getBestTitle(?Note $note = null): string + { + // If we have a note, then the best title is the title itself + if (!is_null(($note))) { + $attachment_to_note = DB::findOneBy('attachment_to_note', [ + 'attachment_id' => $this->getId(), + 'note_id' => $note->getId(), + ]); + if (!is_null($attachment_to_note->getTitle())) { + return $attachment_to_note->getTitle(); + } + } + // Else + if (!is_null($filename = $this->getFilename())) { + // A filename would do just as well + return $filename; + } else { + // Welp + return _m('Untitled Attachment.'); + } + } + /** * Find all thumbnails associated with this attachment. Don't bother caching as this is not supposed to be a common operation */