From 09d512b1e5bc1d6a838d98435d0f4aa50bc96541 Mon Sep 17 00:00:00 2001 From: daniel-lxs Date: Tue, 28 Oct 2025 13:09:17 -0500 Subject: [PATCH] chore(webview): add image utils for size estimation and 10MB limit enforcement [TS.image.ts](webview-ui/src/utils/image.ts:1) --- webview-ui/src/utils/image.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 webview-ui/src/utils/image.ts diff --git a/webview-ui/src/utils/image.ts b/webview-ui/src/utils/image.ts new file mode 100644 index 0000000000..2fa737df33 --- /dev/null +++ b/webview-ui/src/utils/image.ts @@ -0,0 +1,16 @@ +/** + * Utilities for image handling in webview. + */ +export const MAX_IMAGE_BYTES = 10 * 1024 * 1024 + +/** + * Estimate raw bytes from a base64 data URL. Returns 0 for invalid input. + */ +export function estimateBytesFromBase64DataUrl(dataUrl: string): number { + if (typeof dataUrl !== "string") return 0 + const idx = dataUrl.indexOf(",") + if (idx === -1) return 0 + const base64 = dataUrl.slice(idx + 1).replace(/=+$/, "") + // 4 base64 chars represent 3 bytes + return Math.floor((base64.length * 3) / 4) +}