[ATTACHMENTS] Don't store an attachment if it's a dupplicate, reuse it
This commit is contained in:
parent
2b83a4b627
commit
4f936108a1
@ -22,12 +22,12 @@
|
|||||||
namespace App\Core;
|
namespace App\Core;
|
||||||
|
|
||||||
use App\Core\DB\DB;
|
use App\Core\DB\DB;
|
||||||
use App\Util\Exception\NotFoundException;
|
|
||||||
use function App\Core\I18n\_m;
|
use function App\Core\I18n\_m;
|
||||||
use App\Entity\Attachment;
|
use App\Entity\Attachment;
|
||||||
use App\Util\Common;
|
use App\Util\Common;
|
||||||
use App\Util\Exception\ClientException;
|
use App\Util\Exception\ClientException;
|
||||||
use App\Util\Exception\NoSuchFileException;
|
use App\Util\Exception\NoSuchFileException;
|
||||||
|
use App\Util\Exception\NotFoundException;
|
||||||
use App\Util\Exception\ServerException;
|
use App\Util\Exception\ServerException;
|
||||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||||
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
|
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
|
||||||
@ -46,25 +46,29 @@ class GSFile
|
|||||||
int $actor_id = null): Attachment
|
int $actor_id = null): Attachment
|
||||||
{
|
{
|
||||||
Event::handle('HashFile', [$sfile->getPathname(), &$hash]);
|
Event::handle('HashFile', [$sfile->getPathname(), &$hash]);
|
||||||
// The following properly gets the mimetype with `file` or other
|
try {
|
||||||
// available methods, so should be safe
|
return DB::findOneBy('attachment', ['file_hash' => $hash]);
|
||||||
$mimetype = $sfile->getMimeType();
|
} catch (NotFoundException) {
|
||||||
Event::handle('AttachmentValidation', [&$sfile, &$mimetype, &$title, &$width, &$height]);
|
// The following properly gets the mimetype with `file` or other
|
||||||
$attachment = Attachment::create([
|
// available methods, so should be safe
|
||||||
'file_hash' => $hash,
|
$mimetype = $sfile->getMimeType();
|
||||||
'gsactor_id' => $actor_id,
|
Event::handle('AttachmentValidation', [&$sfile, &$mimetype, &$title, &$width, &$height]);
|
||||||
'mimetype' => $mimetype,
|
$attachment = Attachment::create([
|
||||||
'title' => $title ?: _m('Untitled attachment'),
|
'file_hash' => $hash,
|
||||||
'filename' => $hash,
|
'gsactor_id' => $actor_id,
|
||||||
'is_local' => $is_local,
|
'mimetype' => $mimetype,
|
||||||
'size' => $sfile->getSize(),
|
'title' => $title ?: _m('Untitled attachment'),
|
||||||
'width' => $width,
|
'filename' => $hash,
|
||||||
'height' => $height,
|
'is_local' => $is_local,
|
||||||
]);
|
'size' => $sfile->getSize(),
|
||||||
$sfile->move($dest_dir, $hash);
|
'width' => $width,
|
||||||
DB::persist($attachment);
|
'height' => $height,
|
||||||
Event::handle('AttachmentStoreNew', [&$attachment]);
|
]);
|
||||||
return $attachment;
|
$sfile->move($dest_dir, $hash);
|
||||||
|
DB::persist($attachment);
|
||||||
|
Event::handle('AttachmentStoreNew', [&$attachment]);
|
||||||
|
return $attachment;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -75,10 +79,10 @@ class GSFile
|
|||||||
public static function validateAndStoreURLAsAttachment(string $url): Attachment
|
public static function validateAndStoreURLAsAttachment(string $url): Attachment
|
||||||
{
|
{
|
||||||
if (Common::isValidHttpUrl($url)) {
|
if (Common::isValidHttpUrl($url)) {
|
||||||
$head = HTTPClient::head($url);
|
$head = HTTPClient::head($url);
|
||||||
// This must come before getInfo given that Symfony HTTPClient is lazy (thus forcing curl exec)
|
// This must come before getInfo given that Symfony HTTPClient is lazy (thus forcing curl exec)
|
||||||
$headers = $head->getHeaders();
|
$headers = $head->getHeaders();
|
||||||
$url = $head->getInfo('url'); // The last effective url (after getHeaders so it follows redirects)
|
$url = $head->getInfo('url'); // The last effective url (after getHeaders so it follows redirects)
|
||||||
$url_hash = hash(Attachment::URLHASH_ALGO, $url);
|
$url_hash = hash(Attachment::URLHASH_ALGO, $url);
|
||||||
try {
|
try {
|
||||||
return DB::findOneBy('attachment', ['remote_url_hash' => $url_hash]);
|
return DB::findOneBy('attachment', ['remote_url_hash' => $url_hash]);
|
||||||
|
Loading…
Reference in New Issue
Block a user