Improve URL processing on embed service

This commit is contained in:
2025-02-06 14:06:45 +01:00
parent 33b2ad3952
commit a9a8a2b577
+34 -2
View File
@@ -28,12 +28,18 @@ class EmbedService
$this->embera = $embera;
}
public function getData(string $url): ?EmbedData
/**
* @param string $url A URL to the a video resource
*
* @throws \Exception When the URL does not contain any embedable data
*/
public function getData(string $url): EmbedData
{
$url = $this->parseUrl($url);
$data = $this->embera->getUrlData($url);
if (empty($data)) {
return null;
throw new \Exception(\sprintf('Could not extract embed data from %s', $url));
}
$data = $data[$url];
@@ -44,6 +50,32 @@ class EmbedService
);
}
private function parseUrl(string $url): string
{
$urlData = \parse_url($url);
if (!\array_key_exists('path', $urlData) || empty($urlData['path'])) {
return '';
}
if (!\array_key_exists('scheme', $urlData)) {
return $this->parseUrl(\sprintf('https://%s', $url));
}
$query = '';
if (\array_key_exists('query', $urlData)) {
$query = \sprintf('?%s', $urlData['query']);
}
return \sprintf(
'%s://%s%s%s',
$urlData['scheme'],
$urlData['host'],
$urlData['path'],
$query
);
}
private function getIframeSrc(string $iframe): string
{
$dom = new \DOMDocument();