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

10
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "coryd.dev", "name": "coryd.dev",
"version": "5.1.3", "version": "5.1.4",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "coryd.dev", "name": "coryd.dev",
"version": "5.1.3", "version": "5.1.4",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"html-minifier-terser": "7.2.0", "html-minifier-terser": "7.2.0",
@ -1737,9 +1737,9 @@
"license": "MIT" "license": "MIT"
}, },
"node_modules/electron-to-chromium": { "node_modules/electron-to-chromium": {
"version": "1.5.154", "version": "1.5.155",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.154.tgz", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.155.tgz",
"integrity": "sha512-G4VCFAyKbp1QJ+sWdXYIRYsPGvlV5sDACfCmoMFog3rjm1syLhI41WXm/swZypwCIWIm4IFLWzHY14joWMQ5Fw==", "integrity": "sha512-ps5KcGGmwL8VaeJlvlDlu4fORQpv3+GIcF5I3f9tUKUlJ/wsysh6HU8P5L1XWRYeXfA0oJd4PyM8ds8zTFf6Ng==",
"dev": true, "dev": true,
"license": "ISC" "license": "ISC"
}, },

View file

@ -1,6 +1,6 @@
{ {
"name": "coryd.dev", "name": "coryd.dev",
"version": "5.1.3", "version": "5.1.4",
"description": "The source for my personal site. Built using 11ty (and other tools).", "description": "The source for my personal site. Built using 11ty (and other tools).",
"type": "module", "type": "module",
"engines": { "engines": {