fix(oembed.php): dry up response objects

This commit is contained in:
Cory Dransfeldt 2025-05-10 19:14:29 -07:00
parent 52257f50a1
commit 84a1c0d4cc
No known key found for this signature in database
4 changed files with 40 additions and 39 deletions

View file

@ -11,58 +11,58 @@ class OembedHandler extends BaseHandler
{
$requestUrl = $_GET['url'] ?? null;
$globals = $this->fetchGlobals();
if (!$requestUrl) {
$this->sendResponse([
'version' => '1.0',
'type' => 'link',
'title' => $globals['site_name'],
'author_name' => $globals['author'],
'provider_name' => $globals['site_name'],
'provider_url' => $globals['url'],
'thumbnail_url' => $globals['url'] . '/og/w800' . $globals['avatar'],
'html' => '<a href="' . $globals['url'] . '">' . $globals['site_name'] . '</a>',
]);
}
$parsed = parse_url($requestUrl);
$parsed = $requestUrl ? parse_url($requestUrl) : null;
$relativePath = $parsed['path'] ?? null;
if (!$requestUrl || $relativePath === '/') $this->sendResponse($this->buildResponse(
$globals['site_name'],
$globals['url'],
$globals['avatar'],
$globals
));
if (!$relativePath) $this->sendErrorResponse('Invalid url', 400);
if ($relativePath !== '/' && str_ends_with($relativePath, '/')) $relativePath = rtrim($relativePath, '/');
$results = $this->fetchFromApi('optimized_oembed', 'url=eq.' . urlencode($relativePath));
if (!empty($results)) {
$item = $results[0];
$this->sendResponse([
'version' => '1.0',
'type' => 'link',
'title' => $item['title'],
'author_name' => $globals['author'],
'provider_name' => $globals['site_name'],
'provider_url' => $globals['url'],
'thumbnail_url' => $globals['url'] . '/og/w800' . $item['image_url'],
'html' => '<a href="' . htmlspecialchars($item['url']) . '">' . htmlspecialchars($item['title']) . '</a>',
]);
$this->sendResponse($this->buildResponse(
$item['title'],
$item['url'],
$item['image_url'],
$globals
));
}
$segments = explode('/', trim($relativePath, '/'));
if (count($segments) === 1 && $segments[0] !== '') {
$title = ucwords(str_replace('-', ' ', $segments[0])) . ' • ' . $globals['author'];
$this->sendResponse([
'version' => '1.0',
'type' => 'link',
'title' => $title,
'author_name' => $globals['author'],
'provider_name' => $globals['site_name'],
'provider_url' => $globals['url'],
'thumbnail_url' => $globals['url'] . '/og/w800' . $globals['avatar'],
'html' => '<a href="' . htmlspecialchars($relativePath) . '">' . htmlspecialchars($title) . '</a>',
]);
$this->sendResponse($this->buildResponse(
$title,
$relativePath,
$globals['avatar'],
$globals
));
}
$this->sendErrorResponse('No match found', 404);
}
private function buildResponse(string $title, string $url, string $imagePath, array $globals): array
{
return [
'version' => '1.0',
'type' => 'link',
'title' => $title,
'author_name' => $globals['author'],
'provider_name' => $globals['site_name'],
'provider_url' => $globals['url'],
'thumbnail_url' => $globals['url'] . '/og/w800' . $imagePath,
'html' => '<a href="' . htmlspecialchars($url) . '">' . htmlspecialchars($title) . '</a>',
];
}
private function fetchGlobals(): array
{
$globals = $this->fetchFromApi('optimized_globals', 'limit=1');

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "coryd.dev",
"version": "4.0.3",
"version": "4.0.4",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "coryd.dev",
"version": "4.0.3",
"version": "4.0.4",
"license": "MIT",
"dependencies": {
"html-minifier-terser": "7.2.0",

View file

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

View file

@ -56,7 +56,8 @@ RewriteRule ^og/([a-z0-9\-]+)/([a-f0-9\-]+)\.([a-z0-9]+)$ /api/og-image.php?id=$
RewriteRule ^og/([a-z0-9\-]+)/([\d\.]+)/([a-f0-9\-]+)\.([a-z0-9]+)$ /api/og-image.php?id=$3&class=$1&v=$2&extension=$4 [L]
## oembed
RewriteRule ^oembed/(.*)$ /api/oembed.php?url=https://www.coryd.dev/$1 [L,QSA]
RewriteRule ^oembed/?$ /api/oembed.php [L]
RewriteRule ^oembed/(.+)$ /api/oembed.php?url=https://www.coryd.dev/$1 [L,QSA]
{% for redirect in redirects -%}
Redirect {{ redirect.status_code | default: "301" }} {{ redirect.source_url }} {{ redirect.destination_url }}