Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
2.8 kB ยท 107 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108import { readFileSync } from "node:fs";import { join } from "node:path";
interface ImageDimensions { width: number; height: number;}
/** * Get image dimensions from PNG file * PNG spec: width and height are stored at bytes 16-23 as 4-byte integers */function getPngDimensions(buffer: Buffer): ImageDimensions { const width = buffer.readUInt32BE(16); const height = buffer.readUInt32BE(20); return { width, height };}
/** * Get image dimensions from JPEG file */function getJpegDimensions(buffer: Buffer): ImageDimensions { let offset = 2; // Skip SOI marker
while (offset < buffer.length) { // Check for marker if (buffer[offset] !== 0xff) break;
const marker = buffer[offset + 1]; offset += 2;
// SOF markers (Start of Frame) if ( marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc ) { const height = buffer.readUInt16BE(offset + 3); const width = buffer.readUInt16BE(offset + 5); return { width, height }; }
// Skip to next marker const segmentLength = buffer.readUInt16BE(offset); offset += segmentLength; }
throw new Error("Could not find JPEG dimensions");}
function getSvgDimensions(content: string): ImageDimensions | null { const svgTag = content.match(/<svg\b[^>]*>/i)?.[0]; if (!svgTag) return null;
const widthAttr = svgTag.match( /\bwidth\s*=\s*["']?(\d+(?:\.\d+)?)(?:px)?["'\s/>]/i, )?.[1]; const heightAttr = svgTag.match( /\bheight\s*=\s*["']?(\d+(?:\.\d+)?)(?:px)?["'\s/>]/i, )?.[1]; if (widthAttr && heightAttr) { return { width: Number(widthAttr), height: Number(heightAttr) }; }
const viewBox = svgTag.match(/\bviewBox\s*=\s*["']([^"']+)["']/i)?.[1]; if (viewBox) { const parts = viewBox .trim() .split(/[\s,]+/) .map(Number); if (parts.length === 4 && parts.every((n) => Number.isFinite(n))) { return { width: parts[2], height: parts[3] }; } }
return null;}
/** * Get dimensions for an image in the public directory */export function getImageDimensions(publicPath: string): ImageDimensions | null { try { const fullPath = join(process.cwd(), "public", publicPath); const buffer = readFileSync(fullPath);
// Check file signature if (buffer[0] === 0x89 && buffer.toString("ascii", 1, 4) === "PNG") { return getPngDimensions(buffer); }
if (buffer[0] === 0xff && buffer[1] === 0xd8) { return getJpegDimensions(buffer); }
if (publicPath.toLowerCase().endsWith(".svg")) { return getSvgDimensions(buffer.toString("utf8")); }
return null; } catch (error) { console.warn(`Failed to get dimensions for ${publicPath}:`, error); return null; }}