fix(oembed.php): clean up endpoint formatting

This commit is contained in:
Cory Dransfeldt 2025-05-15 12:30:09 -07:00
parent 8fb3e761b3
commit 61af1e0f23
No known key found for this signature in database
3 changed files with 13 additions and 6 deletions

View file

@ -25,9 +25,11 @@ class OembedHandler extends BaseHandler
if (!$relativePath) $this->sendErrorResponse('Invalid url', 400);
$relativePath = '/' . ltrim($relativePath ?? '', '/');
if ($relativePath !== '/' && str_ends_with($relativePath, '/')) $relativePath = rtrim($relativePath, '/');
$cacheKey = 'oembed:' . md5($relativePath);
if ($this->cache && $this->cache->exists($cacheKey)) {
$cachedItem = json_decode($this->cache->get($cacheKey), true);
$this->sendResponse($this->buildResponse(
@ -42,6 +44,7 @@ class OembedHandler extends BaseHandler
$results = $this->fetchFromApi('optimized_oembed', 'url=eq.' . urlencode($relativePath));
if (!empty($results)) {
$item = $results[0];
if ($this->cache) $this->cache->setex($cacheKey, 300, json_encode($item));
$this->sendResponse($this->buildResponse(
@ -56,6 +59,7 @@ class OembedHandler extends BaseHandler
$segments = explode('/', trim($relativePath, '/'));
if (count($segments) === 1 && $segments[0] !== '') {
$title = ucwords(str_replace('-', ' ', $segments[0])) . ' • ' . $globals['author'];
$this->sendResponse($this->buildResponse(
$title,
$relativePath,
@ -71,6 +75,7 @@ class OembedHandler extends BaseHandler
{
$safeDescription = truncateText(strip_tags(parseMarkdown($description)), 175);
$html = '<p><a href="' . htmlspecialchars($url) . '">' . htmlspecialchars($title) . '</a></p>';
if ($description) $html .= '<p>' . htmlspecialchars($safeDescription, ENT_QUOTES, 'UTF-8') . '</p>';
return [
@ -89,6 +94,7 @@ class OembedHandler extends BaseHandler
private function fetchGlobals(): array
{
$cacheKey = 'globals_data';
if ($this->cache && $this->cache->exists($cacheKey)) return json_decode($this->cache->get($cacheKey), true);
$globals = $this->fetchFromApi('optimized_globals', 'limit=1')[0] ?? [
@ -97,6 +103,7 @@ class OembedHandler extends BaseHandler
'url' => 'https://www.coryd.dev',
'avatar' => ''
];
if ($this->cache) $this->cache->setex($cacheKey, 3600, json_encode($globals));
return $globals;