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

This commit is contained in:
2021-08-12 04:41:00 +01:00
committed by Hugo Sales
parent d076781c74
commit 4cc4523632
3 changed files with 56 additions and 35 deletions

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