|
| 1 | +import { NextRequest, NextResponse } from "next/server"; |
| 2 | +import parseURL from "@/helpers/parser"; |
| 3 | +import { DocumentInfo } from "@/types"; |
| 4 | +import axios from "axios"; |
| 5 | +import { metadata } from "@/app/page"; |
| 6 | + |
| 7 | +export async function GET(request: NextRequest) { |
| 8 | + const Base64searchParams = request.nextUrl.searchParams.get('base64'); |
| 9 | + const URLsearchParams = request.nextUrl.searchParams.get('url'); |
| 10 | + |
| 11 | + try { |
| 12 | + if (!Base64searchParams && !URLsearchParams) return new NextResponse(null, { status: 200 }); |
| 13 | + let info: DocumentInfo | null = null; |
| 14 | + |
| 15 | + if (Base64searchParams) { |
| 16 | + // directly run the parsing function |
| 17 | + info = await parseURL(Base64searchParams); |
| 18 | + } |
| 19 | + if (URLsearchParams) { |
| 20 | + // fetch the document information from the URL |
| 21 | + try { |
| 22 | + const response = await axios.get(URLsearchParams); |
| 23 | + if (response.status === 200) { |
| 24 | + info = await parseURL(response.data); |
| 25 | + } else { |
| 26 | + return new NextResponse("Not a valid URL", { status: 500 }); |
| 27 | + } |
| 28 | + } catch (error) { |
| 29 | + return new NextResponse("Not a valid URL", { status: 500 }); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + if (!info) { |
| 34 | + const ogImage = "https://raw.githubusercontent.com/asyncapi/studio/master/apps/studio-next/public/img/meta-studio-og-image.jpeg"; |
| 35 | + |
| 36 | + const crawlerInfo = ` |
| 37 | + <!DOCTYPE html> |
| 38 | + <html lang="en"> |
| 39 | + <head> |
| 40 | + <meta charset="UTF-8"> |
| 41 | + <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 42 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 43 | + <title>"${metadata.openGraph?.title}"</title> |
| 44 | + <meta property="og:title" content="${metadata.openGraph?.title}" /> |
| 45 | + <meta property="og:description" content="${metadata.openGraph?.description}" /> |
| 46 | + <meta property="og:url" content="${metadata.openGraph?.url}" /> |
| 47 | + <meta property="og:image" content="${ogImage}" /> |
| 48 | + ` |
| 49 | + return new NextResponse(crawlerInfo, { |
| 50 | + headers: { |
| 51 | + 'Content-Type': 'text/html', |
| 52 | + }, |
| 53 | + }) |
| 54 | + } |
| 55 | + |
| 56 | + let ogImageParams = new URLSearchParams(); |
| 57 | + |
| 58 | + if (info.title) { |
| 59 | + ogImageParams.append('title', info.title.toString()); |
| 60 | + } |
| 61 | + if (info.description) { |
| 62 | + ogImageParams.append('description', info.description.toString()); |
| 63 | + } |
| 64 | + if (info.numServers) { |
| 65 | + ogImageParams.append('numServers', info.numServers.toString()); |
| 66 | + } |
| 67 | + if (info.numChannels) { |
| 68 | + ogImageParams.append('numChannels', info.numChannels.toString()); |
| 69 | + } |
| 70 | + if (info.numOperations) { |
| 71 | + ogImageParams.append('numOperations', info.numOperations.toString()); |
| 72 | + } |
| 73 | + if (info.numMessages) { |
| 74 | + ogImageParams.append('numMessages', info.numMessages.toString()); |
| 75 | + } |
| 76 | + |
| 77 | + const ogImageurl = `https://ogp-studio.netlify.app/api/og?${ogImageParams.toString()}`; |
| 78 | + |
| 79 | + const crawlerInfo = ` |
| 80 | + <!DOCTYPE html> |
| 81 | + <html lang="en"> |
| 82 | + <head> |
| 83 | + <meta charset="UTF-8"> |
| 84 | + <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 85 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 86 | + <title>${info.title}</title> |
| 87 | + ${info.title ? `<meta property="og:title" content="${info.title}" />` : ''} |
| 88 | + ${info.description ? `<meta property="og:description" content="${info.description}" />` : ''} |
| 89 | + <meta property="og:image" content=${ogImageurl} /> |
| 90 | + </head> |
| 91 | + </html> |
| 92 | + `; |
| 93 | + |
| 94 | + return new NextResponse(crawlerInfo, { |
| 95 | + status: 200, |
| 96 | + headers: { |
| 97 | + 'Content-Type': 'text/html', |
| 98 | + }, |
| 99 | + }); |
| 100 | + } catch (err) { |
| 101 | + return new NextResponse("Not a valid URL", { status: 500 }); |
| 102 | + } |
| 103 | +} |
| 104 | + |
0 commit comments