import { ImageResponse, NextRequest, NextResponse, } from "next/server"; import createErrorPayload from "utils/apiHelper/createErrorPayload"; import ExtendedError from "utils/generalHelper/extendedError"; import { API_Response } from "types/api"; import { FileResponse, FilesResponse, } from "types/api/files"; import { FilePath, ValidateFilePathResponse, } from "types/api/path"; import { RequestContext } from "types/general"; import { Constant } from "types/general/constant"; import apiConfig from "config/api.config"; import siteConfig from "config/site.config"; export const runtime = "edge"; export const alt = siteConfig.siteName; export const size = { width: 1200, height: 630, }; export async function GET( request: NextRequest, { params }: RequestContext<"path", string[]>, ) { const _start = Date.now(); const { path } = params; try { const pathValidation = await fetch( `${apiConfig.basePath}/api/validate/${path.join( "/", )}`, { headers: { [Constant.cookieMaster]: process.env .MASTER_KEY as string, }, }, ).then( (res) => res.json() as Promise< API_Response >, ); if (!pathValidation.success) { throw new ExtendedError( Constant.apiFileNotFound, 404, "notFound", "Can't find the file you're looking for.", ); } const targetFile = pathValidation.data?.[ pathValidation.data.length - 1 ] as FilePath; const fetchFile = fetch( `${apiConfig.basePath}/api/files/${targetFile.encryptedId}`, { headers: { [Constant.cookieMaster]: process.env .MASTER_KEY as string, }, }, ).then( (res) => res.json() as Promise< API_Response >, ); const fetchFontBold = fetch( `${apiConfig.basePath}/fonts/Exo2-Bold.ttf`, ).then((res) => res.arrayBuffer()); const fetchFontRegular = fetch( `${apiConfig.basePath}/fonts/Exo2-Regular.ttf`, ).then((res) => res.arrayBuffer()); const [file, fontBold, fontRegular] = await Promise.all( [fetchFile, fetchFontBold, fetchFontRegular], ); if ( targetFile.mimeType === "application/vnd.google-apps.folder" && !(file.data as FilesResponse).isBannerExists ) { return new ImageResponse( ( {alt} ), { ...size, }, ); } return new ImageResponse( (
{""}
{""}
{siteConfig.siteName}
{targetFile.name}
{""}
Powered by
{""}
next-gdrive-index
), { ...size, fonts: [ { name: "Exo2", data: fontRegular, weight: 400, style: "normal", }, { name: "Exo2", data: fontBold, weight: 700, style: "normal", }, ], }, ); } catch (error: any) { const payload = createErrorPayload( error, "GET /opengraph", _start, ); return NextResponse.json(payload, { status: payload.code, }); } }