11import { multiParser } from "https://deno.land/x/multiparser@0.114.0/mod.ts" ;
22
3+ /**
4+ * Handles the image dimensions request.
5+ *
6+ * @param request - The incoming HTTP request.
7+ * @returns The HTTP response with the image dimensions or an error message.
8+ * @throws Will throw an error if the request method is not POST or if processing fails.
9+ *
10+ * @example
11+ * // How to call the function.
12+ * fetch('/api/image-dimensions', { method: 'POST', body: formData });
13+ */
314export default async ( request : Request ) => {
415 if ( request . method === "POST" ) {
516 try {
@@ -63,6 +74,17 @@ export default async (request: Request) => {
6374 return new Response ( "Method Not Allowed" , { status : 405 } ) ;
6475} ;
6576
77+ /**
78+ * Extracts the dimensions of a JPEG image.
79+ *
80+ * @param data - The image data as a Uint8Array.
81+ * @returns The width and height of the image.
82+ * @throws Will throw an error if the JPEG file is invalid or dimensions are not found.
83+ *
84+ * @example
85+ * // How to use the function.
86+ * const dimensions = getJpegDimensions(fileData);
87+ */
6688function getJpegDimensions ( data : Uint8Array ) : {
6789 width : number ;
6890 height : number ;
@@ -94,6 +116,17 @@ function getJpegDimensions(data: Uint8Array): {
94116 throw new Error ( "No size information found in JPEG" ) ;
95117}
96118
119+ /**
120+ * Extracts the dimensions of a PNG image.
121+ *
122+ * @param data - The image data as a Uint8Array.
123+ * @returns The width and height of the image.
124+ * @throws Will throw an error if the PNG file is invalid.
125+ *
126+ * @example
127+ * // How to use the function.
128+ * const dimensions = getPngDimensions(fileData);
129+ */
97130function getPngDimensions ( data : Uint8Array ) : { width : number ; height : number } {
98131 if (
99132 data [ 0 ] !== 0x89 ||
@@ -109,6 +142,17 @@ function getPngDimensions(data: Uint8Array): { width: number; height: number } {
109142 return { width, height } ;
110143}
111144
145+ /**
146+ * Extracts the dimensions of a GIF image.
147+ *
148+ * @param data - The image data as a Uint8Array.
149+ * @returns The width and height of the image.
150+ * @throws Will throw an error if the GIF file is invalid.
151+ *
152+ * @example
153+ * // How to use the function.
154+ * const dimensions = getGifDimensions(fileData);
155+ */
112156function getGifDimensions ( data : Uint8Array ) : { width : number ; height : number } {
113157 if ( data [ 0 ] !== 0x47 || data [ 1 ] !== 0x49 || data [ 2 ] !== 0x46 ) {
114158 throw new Error ( "Invalid GIF file" ) ;
@@ -119,13 +163,35 @@ function getGifDimensions(data: Uint8Array): { width: number; height: number } {
119163 return { width, height } ;
120164}
121165
166+ /**
167+ * Extracts the dimensions of a BMP image.
168+ *
169+ * @param data - The image data as a Uint8Array.
170+ * @returns The width and height of the image.
171+ * @throws Will throw an error if the BMP file is invalid.
172+ *
173+ * @example
174+ * // How to use the function.
175+ * const dimensions = getBmpDimensions(fileData);
176+ */
122177function getBmpDimensions ( data : Uint8Array ) : { width : number ; height : number } {
123178 const view = new DataView ( data . buffer ) ;
124179 const width = view . getInt32 ( 18 , true ) ; // Little endian
125180 const height = view . getInt32 ( 22 , true ) ; // Little endian
126181 return { width, height } ;
127182}
128183
184+ /**
185+ * Extracts the dimensions of a TIFF image.
186+ *
187+ * @param data - The image data as a Uint8Array.
188+ * @returns The width and height of the image.
189+ * @throws Will throw an error if the TIFF file is invalid or dimensions are not found.
190+ *
191+ * @example
192+ * // How to use the function.
193+ * const dimensions = getTiffDimensions(fileData);
194+ */
129195function getTiffDimensions ( data : Uint8Array ) : {
130196 width : number ;
131197 height : number ;
@@ -173,6 +239,17 @@ function getTiffDimensions(data: Uint8Array): {
173239 return { width, height } ;
174240}
175241
242+ /**
243+ * Extracts the dimensions of an SVG image.
244+ *
245+ * @param data - The image data as a Uint8Array.
246+ * @returns The width and height of the image.
247+ * @throws Will throw an error if the SVG file is invalid.
248+ *
249+ * @example
250+ * // How to use the function.
251+ * const dimensions = getSvgDimensions(fileData);
252+ */
176253function getSvgDimensions ( data : Uint8Array ) : { width : number ; height : number } {
177254 const decoder = new TextDecoder ( "utf-8" ) ;
178255 const svgContent = decoder . decode ( data ) ;
0 commit comments