[ATTACHMENTS] Don't store an attachment if it's a dupplicate, reuse it

This commit is contained in:
Hugo Sales 2021-05-02 15:46:12 +00:00
parent 2b83a4b627
commit 4f936108a1
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
1 changed files with 27 additions and 23 deletions

View File

@ -22,12 +22,12 @@
namespace App\Core;
use App\Core\DB\DB;
use App\Util\Exception\NotFoundException;
use function App\Core\I18n\_m;
use App\Entity\Attachment;
use App\Util\Common;
use App\Util\Exception\ClientException;
use App\Util\Exception\NoSuchFileException;
use App\Util\Exception\NotFoundException;
use App\Util\Exception\ServerException;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
@ -46,6 +46,9 @@ class GSFile
int $actor_id = null): Attachment
{
Event::handle('HashFile', [$sfile->getPathname(), &$hash]);
try {
return DB::findOneBy('attachment', ['file_hash' => $hash]);
} catch (NotFoundException) {
// The following properly gets the mimetype with `file` or other
// available methods, so should be safe
$mimetype = $sfile->getMimeType();
@ -66,6 +69,7 @@ class GSFile
Event::handle('AttachmentStoreNew', [&$attachment]);
return $attachment;
}
}
/**
* Create an attachment for the given URL, fetching the mimetype