Files
Synapsis/src/app/api/media/preview/route.ts
T
cyph3rasi b4a9d82d05 Add docker publish flow, node blocklist & API updates
Replace docker/metadata GH Action with a docker-publish.sh driven workflow (supports auto-versioning, extra tags, multi-arch builds and optional builder). Update docs and READMEs to use the updater and new publish flow. Add server-side swarm/node blocklist support and admin nodes API. Improve auth endpoints (me, logout, switch, session accounts) and support account switching. Extend bots API to support custom LLM provider and optional endpoint. Enforce node blocklist on chat send/receive, refactor link preview handling into dedicated preview modules, and enhance notifications and posts like/repost handlers to accept both signed actions and regular auth flows. Misc: remove admin update UI details, add helper libs (media previews, node-blocklist, reposts, notifications, etc.), and minor housekeeping (pyc, workflow tweaks).
2026-03-10 12:40:05 -07:00

65 lines
2.1 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import type { LinkPreviewData } from '@/lib/media/linkPreview';
import { fetchRedditRichPreview } from '@/lib/media/redditPreview';
import { fetchGenericLinkPreview } from '@/lib/media/genericPreview';
function isRedditUrl(url: string): boolean {
try {
const parsed = new URL(url);
return parsed.hostname.endsWith('reddit.com') || parsed.hostname === 'redd.it';
} catch {
return false;
}
}
function buildBasicPreview(url: string, title?: string | null, description?: string | null, image?: string | null): LinkPreviewData {
return {
url,
title: title || url,
description: description || null,
image: image || null,
type: image ? 'image' : 'card',
videoUrl: null,
media: image ? [{ url: image }] : null,
};
}
export async function GET(req: NextRequest) {
try {
const { searchParams } = new URL(req.url);
let url = searchParams.get('url');
if (!url) {
return NextResponse.json({ error: 'No URL provided' }, { status: 400 });
}
if (!url.startsWith('http://') && !url.startsWith('https://')) {
url = 'https://' + url;
}
if (isRedditUrl(url)) {
const preview = await fetchRedditRichPreview(url);
if (preview) {
return NextResponse.json(preview);
}
return NextResponse.json(buildBasicPreview(url, 'Reddit'));
}
const preview = await fetchGenericLinkPreview(url);
if (!preview) {
return NextResponse.json({ error: 'Could not reach the URL' }, { status: 404 });
}
return NextResponse.json(buildBasicPreview(
preview.url,
preview.title?.trim() || url,
preview.description?.trim() || null,
preview.image?.trim() || null,
));
} catch (error) {
console.error('Link preview error:', error);
return NextResponse.json({ error: 'Failed to fetch preview' }, { status: 500 });
}
}