feat: Link Preview block (carte aperçu URL) + proxy images
- Bloc Link Preview : colle une URL → carte avec titre, description, image, favicon - API /api/link-preview : extraction OpenGraph + meta tags - API /api/image-proxy : contourne le hotlinking (Referer spoofing) - Métadonnées persistées en HTML (data-preview JSON) — pas de refetch au reload - Texte indexable : titre + description + URL inclus pour recherche/embeddings - Modal propre pour saisir l'URL (plus de prompt()) - Slash menu + smart paste 'Coller comme carte aperçu' - i18n FR/EN complet - Fix: bouton calendrier retiré du sélecteur de vue
This commit is contained in:
43
memento-note/app/api/image-proxy/route.ts
Normal file
43
memento-note/app/api/image-proxy/route.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const url = req.nextUrl.searchParams.get('url')
|
||||
if (!url) return NextResponse.json({ error: 'Missing url' }, { status: 400 })
|
||||
|
||||
try {
|
||||
const parsed = new URL(url)
|
||||
if (!['http:', 'https:'].includes(parsed.protocol)) {
|
||||
return NextResponse.json({ error: 'Invalid protocol' }, { status: 400 })
|
||||
}
|
||||
|
||||
const controller = new AbortController()
|
||||
const timeout = setTimeout(() => controller.abort(), 5000)
|
||||
|
||||
const res = await fetch(url, {
|
||||
signal: controller.signal,
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (compatible; MementoBot/1.0)',
|
||||
'Accept': 'image/*',
|
||||
'Referer': parsed.origin,
|
||||
},
|
||||
})
|
||||
clearTimeout(timeout)
|
||||
|
||||
if (!res.ok) return NextResponse.json({ error: 'Fetch failed' }, { status: 502 })
|
||||
|
||||
const contentType = res.headers.get('content-type') || 'image/jpeg'
|
||||
if (!contentType.startsWith('image/')) {
|
||||
return NextResponse.json({ error: 'Not an image' }, { status: 400 })
|
||||
}
|
||||
|
||||
const buffer = await res.arrayBuffer()
|
||||
return new NextResponse(buffer, {
|
||||
headers: {
|
||||
'Content-Type': contentType,
|
||||
'Cache-Control': 'public, s-maxage=604800',
|
||||
},
|
||||
})
|
||||
} catch {
|
||||
return NextResponse.json({ error: 'Failed' }, { status: 502 })
|
||||
}
|
||||
}
|
||||
101
memento-note/app/api/link-preview/route.ts
Normal file
101
memento-note/app/api/link-preview/route.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const url = req.nextUrl.searchParams.get('url')
|
||||
if (!url) {
|
||||
return NextResponse.json({ error: 'Missing url' }, { status: 400 })
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = new URL(url)
|
||||
if (!['http:', 'https:'].includes(parsed.protocol)) {
|
||||
return NextResponse.json({ error: 'Invalid protocol' }, { status: 400 })
|
||||
}
|
||||
|
||||
const controller = new AbortController()
|
||||
const timeout = setTimeout(() => controller.abort(), 8000)
|
||||
|
||||
const res = await fetch(url, {
|
||||
signal: controller.signal,
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (compatible; MementoBot/1.0)',
|
||||
'Accept': 'text/html',
|
||||
},
|
||||
redirect: 'follow',
|
||||
})
|
||||
|
||||
clearTimeout(timeout)
|
||||
|
||||
if (!res.ok) {
|
||||
return NextResponse.json({ error: 'Fetch failed' }, { status: 502 })
|
||||
}
|
||||
|
||||
const html = await res.text()
|
||||
const data = extractMetadata(html, parsed)
|
||||
|
||||
return NextResponse.json(data, {
|
||||
headers: { 'Cache-Control': 'public, s-maxage=86400' },
|
||||
})
|
||||
} catch {
|
||||
return NextResponse.json({ error: 'Failed to fetch' }, { status: 502 })
|
||||
}
|
||||
}
|
||||
|
||||
function extractMetadata(html: string, url: URL) {
|
||||
const getMeta = (pattern: RegExp): string | null => {
|
||||
const m = html.match(pattern)
|
||||
return m?.[1]?.trim() || null
|
||||
}
|
||||
|
||||
const getMetaProperty = (prop: string): string | null => {
|
||||
return getMeta(new RegExp(`<meta[^>]+(?:property|name)=["']${prop}["'][^>]+content=["']([^"']+)["']`, 'i'))
|
||||
|| getMeta(new RegExp(`<meta[^>]+content=["']([^"']+)["'][^>]+(?:property|name)=["']${prop}["']`, 'i'))
|
||||
}
|
||||
|
||||
const title =
|
||||
getMetaProperty('og:title') ||
|
||||
getMeta(new RegExp('<title[^>]*>([^<]+)</title>', 'i')) ||
|
||||
null
|
||||
|
||||
const description =
|
||||
getMetaProperty('og:description') ||
|
||||
getMetaProperty('description') ||
|
||||
getMetaProperty('twitter:description') ||
|
||||
null
|
||||
|
||||
const image =
|
||||
getMetaProperty('og:image') ||
|
||||
getMetaProperty('twitter:image') ||
|
||||
null
|
||||
|
||||
const siteName =
|
||||
getMetaProperty('og:site_name') ||
|
||||
null
|
||||
|
||||
const favicon = image
|
||||
? null
|
||||
: `https://www.google.com/s2/favicons?domain=${url.hostname}&sz=32`
|
||||
|
||||
const finalImage = image
|
||||
? (image.startsWith('http') ? image : new URL(image, url.origin).href)
|
||||
: null
|
||||
|
||||
return {
|
||||
title: title ? decodeEntities(title) : null,
|
||||
description: description ? decodeEntities(description).slice(0, 200) : null,
|
||||
image: finalImage,
|
||||
favicon,
|
||||
siteName: siteName || url.hostname.replace('www.', ''),
|
||||
}
|
||||
}
|
||||
|
||||
function decodeEntities(text: string): string {
|
||||
return text
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, "'")
|
||||
.replace(/'/g, "'")
|
||||
.replace(/ /g, ' ')
|
||||
}
|
||||
Reference in New Issue
Block a user