OembedExtractor.php: finalize

This commit is contained in:
Zankaria 2025-03-17 12:47:08 +01:00
parent 4850a8ddd3
commit 5a8c661257

View file

@ -7,6 +7,7 @@ use Vichan\Data\OembedResponse;
class OembedExtractor { class OembedExtractor {
private const DEFAULT_CACHE_TIMEOUT = 3600; // 1 hour. private const DEFAULT_CACHE_TIMEOUT = 3600; // 1 hour.
private const MIN_CACHE_TIMEOUT = 900; // 15 minutes.
private CacheDriver $cache; private CacheDriver $cache;
private HttpDriver $http; private HttpDriver $http;
@ -41,30 +42,25 @@ class OembedExtractor {
); );
$json = \json_decode($body, null, 512, \JSON_THROW_ON_ERROR); $json = \json_decode($body, null, 512, \JSON_THROW_ON_ERROR);
if (!isset($json['title'])) {
throw new \RuntimeException("Missing type in response from $provider_url");
}
$ret = [ $ret = [
'type' => $json['type'], 'title' => $json['title'] ?? null,
'title' => $json['title'], 'thumbnail_url' => $json['thumbnail_url'] ?? null,
'cache_age' => $json['cache_age'],
'thumbnail_url' => $json['thumbnail_url'],
'thumbnail_width' => $json['thumbnail_width'],
'thumbnail_height' => $json['thumbnail_heigh']
]; ];
$cache_timeout = $ret['cache_age'] ?? self::DEFAULT_CACHE_TIMEOUT; $cache_timeout = self::DEFAULT_CACHE_TIMEOUT;
if (isset($json['cache_age'])) {
$cache_age = \intval($json['cache_age']);
if ($cache_age > 0) {
$cache_age = \max($cache_age, self::MIN_CACHE_TIMEOUT);
}
}
$this->cache->set("oembed_embedder_$url$provider_url", $ret, $cache_timeout); $this->cache->set("oembed_embedder_$url$provider_url", $ret, $cache_timeout);
} }
$resp = new OembedResponse(); $resp = new OembedResponse();
$resp->type = $ret['title']; $resp->title = $ret['title'];
$resp->title = $ret['title'] ?? null; $resp->thumbnail_url = $ret['thumbnail_url'];
$resp->cache_age = $ret['cache_age'] ?? null;
$resp->thumbnail_url = $ret['thumbnail_url'] ?? null;
$resp->thumbnail_width = $ret['thumbnail_width'] ?? null;
$resp->thumbnail_height = $ret['thumbnail_height'] ?? null;
return $ret; return $ret;
} }
} }