AVoid throwing an error / store the size of pages

This commit is contained in:
Michael 2024-09-01 18:09:00 +00:00
parent 4dd64b1a5d
commit 7fdf244bae
3 changed files with 8 additions and 4 deletions

View file

@ -193,7 +193,7 @@ class Media
$media['mimetype'] = $curlResult->getContentType() ?? '';
}
if (empty($media['size'])) {
$media['size'] = (int)($curlResult->getHeader('Content-Length')[0] ?? 0);
$media['size'] = (int)($curlResult->getHeader('Content-Length')[0] ?? strlen($curlResult->getBodyString() ?? ''));
}
} else {
Logger::notice('Could not fetch head', ['media' => $media]);
@ -363,6 +363,7 @@ class Media
private static function addPage(array $media): array
{
$data = ParseUrl::getSiteinfoCached($media['url']);
$media['size'] = $data['size'] ?? null;
$media['preview'] = $data['images'][0]['src'] ?? null;
$media['preview-height'] = $data['images'][0]['height'] ?? null;
$media['preview-width'] = $data['images'][0]['width'] ?? null;

View file

@ -61,11 +61,13 @@ class HttpClient implements ICanSendHttpRequests
$host = parse_url($url, PHP_URL_HOST);
if (empty($host)) {
throw new \InvalidArgumentException('Unable to retrieve the host in URL: ' . $url);
$this->logger->notice('Unable to retrieve the host in URL', ['url' => $url]);
$this->profiler->stopRecording();
return CurlResult::createErrorCurl($this->logger, $url);
}
if (!filter_var($host, FILTER_VALIDATE_IP) && !@dns_get_record($host . '.', DNS_A) && !@dns_get_record($host . '.', DNS_AAAA)) {
$this->logger->debug('URL cannot be resolved.', ['url' => $url]);
$this->logger->info('URL cannot be resolved.', ['url' => $url]);
$this->profiler->stopRecording();
return CurlResult::createErrorCurl($this->logger, $url);
}
@ -75,7 +77,7 @@ class HttpClient implements ICanSendHttpRequests
}
if (strlen($url) > 1000) {
$this->logger->debug('URL is longer than 1000 characters.', ['url' => $url]);
$this->logger->info('URL is longer than 1000 characters.', ['url' => $url]);
$this->profiler->stopRecording();
return CurlResult::createErrorCurl($this->logger, substr($url, 0, 200));
}

View file

@ -245,6 +245,7 @@ class ParseUrl
}
$body = $curlResult->getBodyString();
$siteinfo['size'] = mb_strlen($body);
$charset = '';
try {