[Posting] Re-add original file to attachment on upload, if it was previously removed

This commit is contained in:
Diogo Peralta Cordeiro 2021-08-12 04:41:00 +01:00 committed by Hugo Sales
parent d076781c74
commit 4cc4523632
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 56 additions and 35 deletions

View File

@ -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();

View File

@ -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();

View File

@ -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
*/