|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import type { |
| 4 | + FastifyInstance, |
| 5 | + FastifyReply, |
| 6 | + FastifyRequest, |
| 7 | + FastifySchema, |
| 8 | +} from "fastify"; |
| 9 | + |
| 10 | +const imageSchema: FastifySchema = { |
| 11 | + description: |
| 12 | + "Returns an image based on Accept header (supports jpeg, png, svg, webp)", |
| 13 | + tags: ["Images"], |
| 14 | + headers: { |
| 15 | + type: "object", |
| 16 | + properties: { |
| 17 | + accept: { |
| 18 | + type: "string", |
| 19 | + description: "Accept header for content negotiation", |
| 20 | + }, |
| 21 | + }, |
| 22 | + }, |
| 23 | + response: { |
| 24 | + 200: { |
| 25 | + description: "Image file", |
| 26 | + type: "string", |
| 27 | + format: "binary", |
| 28 | + }, |
| 29 | + }, |
| 30 | +}; |
| 31 | + |
| 32 | +const jpegSchema: FastifySchema = { |
| 33 | + description: "Returns a JPEG image", |
| 34 | + tags: ["Images"], |
| 35 | + response: { |
| 36 | + 200: { |
| 37 | + description: "JPEG image", |
| 38 | + type: "string", |
| 39 | + format: "binary", |
| 40 | + }, |
| 41 | + }, |
| 42 | +}; |
| 43 | + |
| 44 | +const pngSchema: FastifySchema = { |
| 45 | + description: "Returns a PNG image", |
| 46 | + tags: ["Images"], |
| 47 | + response: { |
| 48 | + 200: { |
| 49 | + description: "PNG image", |
| 50 | + type: "string", |
| 51 | + format: "binary", |
| 52 | + }, |
| 53 | + }, |
| 54 | +}; |
| 55 | + |
| 56 | +const svgSchema: FastifySchema = { |
| 57 | + description: "Returns an SVG image", |
| 58 | + tags: ["Images"], |
| 59 | + response: { |
| 60 | + 200: { |
| 61 | + description: "SVG image", |
| 62 | + type: "string", |
| 63 | + format: "binary", |
| 64 | + }, |
| 65 | + }, |
| 66 | +}; |
| 67 | + |
| 68 | +const webpSchema: FastifySchema = { |
| 69 | + description: "Returns a WEBP image", |
| 70 | + tags: ["Images"], |
| 71 | + response: { |
| 72 | + 200: { |
| 73 | + description: "WEBP image", |
| 74 | + type: "string", |
| 75 | + format: "binary", |
| 76 | + }, |
| 77 | + }, |
| 78 | +}; |
| 79 | + |
| 80 | +export const imageRoutes = (fastify: FastifyInstance) => { |
| 81 | + const publicPath = join(process.cwd(), "public"); |
| 82 | + |
| 83 | + // Content negotiation route |
| 84 | + fastify.get( |
| 85 | + "/image", |
| 86 | + { schema: imageSchema }, |
| 87 | + async (request: FastifyRequest, reply: FastifyReply) => { |
| 88 | + const accept = request.headers.accept || ""; |
| 89 | + |
| 90 | + let file = "logo.png"; |
| 91 | + let contentType = "image/png"; |
| 92 | + |
| 93 | + if (accept.includes("image/jpeg") || accept.includes("image/jpg")) { |
| 94 | + file = "logo.jpg"; |
| 95 | + contentType = "image/jpeg"; |
| 96 | + } else if ( |
| 97 | + accept.includes("image/svg+xml") || |
| 98 | + accept.includes("image/svg") |
| 99 | + ) { |
| 100 | + file = "logo.svg"; |
| 101 | + contentType = "image/svg+xml"; |
| 102 | + } else if (accept.includes("image/webp")) { |
| 103 | + file = "logo.webp"; |
| 104 | + contentType = "image/webp"; |
| 105 | + } else if (accept.includes("image/png")) { |
| 106 | + file = "logo.png"; |
| 107 | + contentType = "image/png"; |
| 108 | + } |
| 109 | + |
| 110 | + try { |
| 111 | + const imagePath = join(publicPath, file); |
| 112 | + const imageBuffer = readFileSync(imagePath); |
| 113 | + |
| 114 | + reply.type(contentType); |
| 115 | + return imageBuffer; |
| 116 | + } catch (error) { |
| 117 | + /* v8 ignore next -- @preserve */ |
| 118 | + reply.code(500); |
| 119 | + /* v8 ignore next -- @preserve */ |
| 120 | + return { error: `${error}` }; |
| 121 | + } |
| 122 | + }, |
| 123 | + ); |
| 124 | + |
| 125 | + // JPEG specific route |
| 126 | + fastify.get( |
| 127 | + "/image/jpeg", |
| 128 | + { schema: jpegSchema }, |
| 129 | + async (_request: FastifyRequest, reply: FastifyReply) => { |
| 130 | + try { |
| 131 | + const imagePath = join(publicPath, "logo.jpg"); |
| 132 | + const imageBuffer = readFileSync(imagePath); |
| 133 | + reply.type("image/jpeg"); |
| 134 | + return imageBuffer; |
| 135 | + } catch (error) { |
| 136 | + /* v8 ignore next -- @preserve */ |
| 137 | + reply.code(500); |
| 138 | + /* v8 ignore next -- @preserve */ |
| 139 | + return { error: `${error}` }; |
| 140 | + } |
| 141 | + }, |
| 142 | + ); |
| 143 | + |
| 144 | + // PNG specific route |
| 145 | + fastify.get( |
| 146 | + "/image/png", |
| 147 | + { schema: pngSchema }, |
| 148 | + async (_request: FastifyRequest, reply: FastifyReply) => { |
| 149 | + try { |
| 150 | + const imagePath = join(publicPath, "logo.png"); |
| 151 | + const imageBuffer = readFileSync(imagePath); |
| 152 | + reply.type("image/png"); |
| 153 | + return imageBuffer; |
| 154 | + } catch (error) { |
| 155 | + /* v8 ignore next -- @preserve */ |
| 156 | + reply.code(500); |
| 157 | + /* v8 ignore next -- @preserve */ |
| 158 | + return { error: `${error}` }; |
| 159 | + } |
| 160 | + }, |
| 161 | + ); |
| 162 | + |
| 163 | + // SVG specific route |
| 164 | + fastify.get( |
| 165 | + "/image/svg", |
| 166 | + { schema: svgSchema }, |
| 167 | + async (_request: FastifyRequest, reply: FastifyReply) => { |
| 168 | + try { |
| 169 | + const imagePath = join(publicPath, "logo.svg"); |
| 170 | + const imageBuffer = readFileSync(imagePath); |
| 171 | + reply.type("image/svg+xml"); |
| 172 | + return imageBuffer; |
| 173 | + } catch (error) { |
| 174 | + /* v8 ignore next -- @preserve */ |
| 175 | + reply.code(500); |
| 176 | + /* v8 ignore next -- @preserve */ |
| 177 | + return { error: `${error}` }; |
| 178 | + } |
| 179 | + }, |
| 180 | + ); |
| 181 | + |
| 182 | + // WEBP specific route |
| 183 | + fastify.get( |
| 184 | + "/image/webp", |
| 185 | + { schema: webpSchema }, |
| 186 | + async (_request: FastifyRequest, reply: FastifyReply) => { |
| 187 | + try { |
| 188 | + const imagePath = join(publicPath, "logo.webp"); |
| 189 | + const imageBuffer = readFileSync(imagePath); |
| 190 | + reply.type("image/webp"); |
| 191 | + return imageBuffer; |
| 192 | + } catch (error) { |
| 193 | + /* v8 ignore next -- @preserve */ |
| 194 | + reply.code(500); |
| 195 | + /* v8 ignore next -- @preserve */ |
| 196 | + return { error: `${error}` }; |
| 197 | + } |
| 198 | + }, |
| 199 | + ); |
| 200 | +}; |
0 commit comments