[ATTACHMENTS] Follow URL redirects and don't duplicate attachments
This commit is contained in:
parent
adb6680a01
commit
1e7d8cac9a
@ -22,6 +22,7 @@
|
|||||||
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;
|
||||||
@ -75,17 +76,24 @@ class GSFile
|
|||||||
{
|
{
|
||||||
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)
|
||||||
$headers = $head->getHeaders();
|
$headers = $head->getHeaders();
|
||||||
|
$url = $head->getInfo('url'); // The last effective url (after getHeaders so it follows redirects)
|
||||||
|
$url_hash = hash(Attachment::URLHASH_ALGO, $url);
|
||||||
|
try {
|
||||||
|
return DB::findOneBy('attachment', ['remote_url_hash' => $url_hash]);
|
||||||
|
} catch (NotFoundException) {
|
||||||
$headers = array_change_key_case($headers, CASE_LOWER);
|
$headers = array_change_key_case($headers, CASE_LOWER);
|
||||||
$attachment = Attachment::create([
|
$attachment = Attachment::create([
|
||||||
'remote_url' => $url,
|
'remote_url' => $url,
|
||||||
'remote_url_hash' => hash(Attachment::URLHASH_ALGO, $url),
|
'remote_url_hash' => $url_hash,
|
||||||
'mimetype' => $headers['content-type'][0],
|
'mimetype' => $headers['content-type'][0],
|
||||||
'is_local' => false,
|
'is_local' => false,
|
||||||
]);
|
]);
|
||||||
DB::persist($attachment);
|
DB::persist($attachment);
|
||||||
Event::handle('AttachmentStoreNew', [&$attachment]);
|
Event::handle('AttachmentStoreNew', [&$attachment]);
|
||||||
return $attachment;
|
return $attachment;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new \InvalidArgumentException();
|
throw new \InvalidArgumentException();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user