[ENTITY][Link] Sometimes URLs don't work, handle that

This commit is contained in:
Diogo Peralta Cordeiro 2021-08-14 15:39:55 +01:00 committed by Hugo Sales
parent 1906d4f276
commit 060a5abef1
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -29,6 +29,7 @@ use App\Util\Exception\DuplicateFoundException;
use App\Util\Exception\NotFoundException; use App\Util\Exception\NotFoundException;
use DateTimeInterface; use DateTimeInterface;
use InvalidArgumentException; use InvalidArgumentException;
use Symfony\Component\HttpClient\Exception\ClientException;
/** /**
* Entity for representing a Link * Entity for representing a Link
@ -138,7 +139,11 @@ class Link extends Entity
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(); try {
$headers = $head->getHeaders();
} catch (ClientException $e) {
throw new InvalidArgumentException(previous: $e);
}
$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(self::URLHASH_ALGO, $url); $url_hash = hash(self::URLHASH_ALGO, $url);
try { try {