|
1 | 1 | import { ImageResponse } from "next/og"; |
2 | 2 | import type { NextRequest } from "next/server"; |
3 | 3 | import type { SatoriOptions } from "satori"; |
4 | | -import { z } from "zod"; |
| 4 | +import { z, ZodError } from "zod"; |
5 | 5 |
|
6 | 6 | import { Meeting, App, Generic } from "@calcom/lib/OgImages"; |
7 | 7 | import { WEBAPP_URL } from "@calcom/lib/constants"; |
@@ -34,75 +34,128 @@ async function handler(req: NextRequest) { |
34 | 34 | const { searchParams } = req.nextUrl; |
35 | 35 | const imageType = searchParams.get("type"); |
36 | 36 |
|
37 | | - const [calFontData, interFontData, interFontMediumData] = await Promise.all([ |
38 | | - fetch(new URL("/fonts/cal.ttf", WEBAPP_URL)).then((res) => res.arrayBuffer()), |
39 | | - fetch(new URL("/fonts/Inter-Regular.ttf", WEBAPP_URL)).then((res) => res.arrayBuffer()), |
40 | | - fetch(new URL("/fonts/Inter-Medium.ttf", WEBAPP_URL)).then((res) => res.arrayBuffer()), |
41 | | - ]); |
42 | | - const ogConfig = { |
43 | | - width: 1200, |
44 | | - height: 630, |
45 | | - fonts: [ |
46 | | - { name: "inter", data: interFontData, weight: 400 }, |
47 | | - { name: "inter", data: interFontMediumData, weight: 500 }, |
48 | | - { name: "cal", data: calFontData, weight: 400 }, |
49 | | - { name: "cal", data: calFontData, weight: 600 }, |
50 | | - ] as SatoriOptions["fonts"], |
51 | | - }; |
52 | | - |
53 | | - switch (imageType) { |
54 | | - case "meeting": { |
55 | | - const { names, usernames, title, meetingProfileName, meetingImage } = meetingSchema.parse({ |
56 | | - names: searchParams.getAll("names"), |
57 | | - usernames: searchParams.getAll("usernames"), |
58 | | - title: searchParams.get("title"), |
59 | | - meetingProfileName: searchParams.get("meetingProfileName"), |
60 | | - meetingImage: searchParams.get("meetingImage"), |
61 | | - imageType, |
62 | | - }); |
63 | | - |
64 | | - const img = new ImageResponse( |
65 | | - ( |
66 | | - <Meeting |
67 | | - title={title} |
68 | | - profile={{ name: meetingProfileName, image: meetingImage }} |
69 | | - users={names.map((name, index) => ({ name, username: usernames[index] }))} |
70 | | - /> |
71 | | - ), |
72 | | - ogConfig |
73 | | - ); |
74 | | - |
75 | | - return new Response(img.body, { |
76 | | - status: 200, |
77 | | - headers: { "Content-Type": "image/png", "cache-control": "max-age=0" }, |
78 | | - }); |
| 37 | + try { |
| 38 | + const [calFontData, interFontData, interFontMediumData] = await Promise.all([ |
| 39 | + fetch(new URL("/fonts/cal.ttf", WEBAPP_URL)).then((res) => res.arrayBuffer()), |
| 40 | + fetch(new URL("/fonts/Inter-Regular.ttf", WEBAPP_URL)).then((res) => res.arrayBuffer()), |
| 41 | + fetch(new URL("/fonts/Inter-Medium.ttf", WEBAPP_URL)).then((res) => res.arrayBuffer()), |
| 42 | + ]); |
| 43 | + const ogConfig = { |
| 44 | + width: 1200, |
| 45 | + height: 630, |
| 46 | + fonts: [ |
| 47 | + { name: "inter", data: interFontData, weight: 400 }, |
| 48 | + { name: "inter", data: interFontMediumData, weight: 500 }, |
| 49 | + { name: "cal", data: calFontData, weight: 400 }, |
| 50 | + { name: "cal", data: calFontData, weight: 600 }, |
| 51 | + ] as SatoriOptions["fonts"], |
| 52 | + }; |
| 53 | + |
| 54 | + switch (imageType) { |
| 55 | + case "meeting": { |
| 56 | + try { |
| 57 | + const { names, usernames, title, meetingProfileName, meetingImage } = meetingSchema.parse({ |
| 58 | + names: searchParams.getAll("names"), |
| 59 | + usernames: searchParams.getAll("usernames"), |
| 60 | + title: searchParams.get("title"), |
| 61 | + meetingProfileName: searchParams.get("meetingProfileName"), |
| 62 | + meetingImage: searchParams.get("meetingImage"), |
| 63 | + imageType, |
| 64 | + }); |
| 65 | + |
| 66 | + const img = new ImageResponse( |
| 67 | + ( |
| 68 | + <Meeting |
| 69 | + title={title} |
| 70 | + profile={{ name: meetingProfileName, image: meetingImage }} |
| 71 | + users={names.map((name, index) => ({ name, username: usernames[index] }))} |
| 72 | + /> |
| 73 | + ), |
| 74 | + ogConfig |
| 75 | + ); |
| 76 | + |
| 77 | + return new Response(img.body, { |
| 78 | + status: 200, |
| 79 | + headers: { "Content-Type": "image/png", "cache-control": "max-age=0" }, |
| 80 | + }); |
| 81 | + } catch (error) { |
| 82 | + if (error instanceof ZodError) { |
| 83 | + return new Response( |
| 84 | + JSON.stringify({ |
| 85 | + error: "Invalid parameters for meeting image", |
| 86 | + message: |
| 87 | + "Required parameters: title, meetingProfileName. Optional: names, usernames, meetingImage", |
| 88 | + }), |
| 89 | + { |
| 90 | + status: 400, |
| 91 | + headers: { "Content-Type": "application/json" }, |
| 92 | + } |
| 93 | + ); |
| 94 | + } |
| 95 | + throw error; |
| 96 | + } |
| 97 | + } |
| 98 | + case "app": { |
| 99 | + try { |
| 100 | + const { name, description, slug } = appSchema.parse({ |
| 101 | + name: searchParams.get("name"), |
| 102 | + description: searchParams.get("description"), |
| 103 | + slug: searchParams.get("slug"), |
| 104 | + imageType, |
| 105 | + }); |
| 106 | + const img = new ImageResponse(<App name={name} description={description} slug={slug} />, ogConfig); |
| 107 | + |
| 108 | + return new Response(img.body, { status: 200, headers: { "Content-Type": "image/png" } }); |
| 109 | + } catch (error) { |
| 110 | + if (error instanceof ZodError) { |
| 111 | + return new Response( |
| 112 | + JSON.stringify({ |
| 113 | + error: "Invalid parameters for app image", |
| 114 | + message: "Required parameters: name, description, slug", |
| 115 | + }), |
| 116 | + { |
| 117 | + status: 400, |
| 118 | + headers: { "Content-Type": "application/json" }, |
| 119 | + } |
| 120 | + ); |
| 121 | + } |
| 122 | + throw error; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + case "generic": { |
| 127 | + try { |
| 128 | + const { title, description } = genericSchema.parse({ |
| 129 | + title: searchParams.get("title"), |
| 130 | + description: searchParams.get("description"), |
| 131 | + imageType, |
| 132 | + }); |
| 133 | + |
| 134 | + const img = new ImageResponse(<Generic title={title} description={description} />, ogConfig); |
| 135 | + |
| 136 | + return new Response(img.body, { status: 200, headers: { "Content-Type": "image/png" } }); |
| 137 | + } catch (error) { |
| 138 | + if (error instanceof ZodError) { |
| 139 | + return new Response( |
| 140 | + JSON.stringify({ |
| 141 | + error: "Invalid parameters for generic image", |
| 142 | + message: "Required parameters: title, description", |
| 143 | + }), |
| 144 | + { |
| 145 | + status: 400, |
| 146 | + headers: { "Content-Type": "application/json" }, |
| 147 | + } |
| 148 | + ); |
| 149 | + } |
| 150 | + throw error; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + default: |
| 155 | + return new Response("What you're looking for is not here..", { status: 404 }); |
79 | 156 | } |
80 | | - case "app": { |
81 | | - const { name, description, slug } = appSchema.parse({ |
82 | | - name: searchParams.get("name"), |
83 | | - description: searchParams.get("description"), |
84 | | - slug: searchParams.get("slug"), |
85 | | - imageType, |
86 | | - }); |
87 | | - const img = new ImageResponse(<App name={name} description={description} slug={slug} />, ogConfig); |
88 | | - |
89 | | - return new Response(img.body, { status: 200, headers: { "Content-Type": "image/png" } }); |
90 | | - } |
91 | | - |
92 | | - case "generic": { |
93 | | - const { title, description } = genericSchema.parse({ |
94 | | - title: searchParams.get("title"), |
95 | | - description: searchParams.get("description"), |
96 | | - imageType, |
97 | | - }); |
98 | | - |
99 | | - const img = new ImageResponse(<Generic title={title} description={description} />, ogConfig); |
100 | | - |
101 | | - return new Response(img.body, { status: 200, headers: { "Content-Type": "image/png" } }); |
102 | | - } |
103 | | - |
104 | | - default: |
105 | | - return new Response("What you're looking for is not here..", { status: 404 }); |
| 157 | + } catch (error) { |
| 158 | + return new Response("Internal server error", { status: 500 }); |
106 | 159 | } |
107 | 160 | } |
108 | 161 |
|
|
0 commit comments