1
0
mirror of https://github.com/Kovah/LinkAce.git synced 2025-04-19 06:27:00 +02:00

Properly handle og:images without host

This commit is contained in:
Kovah 2021-04-18 13:46:07 +02:00
parent ae0283ee8a
commit 9caeadfddb
No known key found for this signature in database
GPG Key ID: AAAA031BA9830D7B
2 changed files with 23 additions and 2 deletions

View File

@ -100,6 +100,13 @@ class HtmlMeta
?? $this->meta['twitter:image']
?? null;
if (parse_url($thumbnail, PHP_URL_HOST) === null) {
// If the thumbnail does not contain the domain, add it in front of it
$urlInfo = parse_url($this->url);
$baseUrl = sprintf('%s://%s/', $urlInfo['scheme'], $urlInfo['host']);
$thumbnail = $baseUrl . trim($thumbnail, '/');
}
/*
* Edge case of Youtube only (because of Youtube EU cookie consent)
* Formula based on https://stackoverflow.com/a/2068371, returns Youtube image url

View File

@ -30,7 +30,6 @@ class HtmlMetaHelperTest extends TestCase
$result = (new HtmlMeta)->getFromUrl($url);
$this->assertArrayHasKey('title', $result);
$this->assertEquals('DuckDuckGo', $result['title']);
$this->assertEquals('This an example description', $result['description']);
$this->assertEquals('https://duckduckgo.com/assets/logo_social-media.png', $result['thumbnail']);
@ -53,12 +52,27 @@ class HtmlMetaHelperTest extends TestCase
$result = (new HtmlMeta)->getFromUrl($url);
$this->assertArrayHasKey('title', $result);
$this->assertEquals('DuckDuckGo', $result['title']);
$this->assertEquals('This an example description', $result['description']);
$this->assertTrue($result['success']);
}
public function testThumbnailWithoutHostFromValidURL(): void
{
$testHtml = '<!DOCTYPE html><head>' .
'<meta property="og:image" content="/assets/logo_social-media.png">' .
'</head></html>';
Http::fake(['*' => Http::response($testHtml)]);
$url = 'https://duckduckgo.com/about-us?utm_source=foo';
$result = (new HtmlMeta)->getFromUrl($url);
$this->assertEquals('https://duckduckgo.com/assets/logo_social-media.png', $result['thumbnail']);
$this->assertTrue($result['success']);
}
/*
* Test the HtmlMeta helper with a YouTube link. Must return a special YouTube thumbnail.
*/