{"record":{"id":"268c4e342cf43b20","repo":"wavetermdev/waveterm","slug":"image-too-large-5mb","errorCode":null,"errorMessage":"Image too large (>5MB)","messagePattern":"Image too large \\(>5MB\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"frontend/app/view/term/termutil.ts","lineNumber":84,"sourceCode":"    \"image/heif\": \"heif\",\n    \"image/avif\": \"avif\",\n    \"image/x-icon\": \"ico\",\n    \"image/vnd.microsoft.icon\": \"ico\",\n};\n\n/**\n * Creates a temporary file from a Blob (typically an image).\n * Validates size, generates a unique filename, saves to temp directory,\n * and returns the file path.\n *\n * @param blob - The Blob to save\n * @returns The path to the created temporary file\n * @throws Error if blob is too large (>5MB) or data URL is invalid\n */\nexport async function createTempFileFromBlob(blob: Blob): Promise<string> {\n    // Check size limit (5MB)\n    if (blob.size > 5 * 1024 * 1024) {\n        throw new Error(\"Image too large (>5MB)\");\n    }\n\n    // Get file extension from MIME type\n    if (!blob.type.startsWith(\"image/\") || !MIME_TO_EXT[blob.type]) {\n        throw new Error(`Unsupported or invalid image type: ${blob.type}`);\n    }\n    const ext = MIME_TO_EXT[blob.type];\n\n    // Generate unique filename with timestamp and random component\n    const timestamp = Date.now();\n    const random = Math.random().toString(36).substring(2, 8);\n    const filename = `waveterm_paste_${timestamp}_${random}.${ext}`;\n\n    const arrayBuffer = await new Promise<ArrayBuffer>((resolve, reject) => {\n        const reader = new FileReader();\n        reader.onload = () => resolve(reader.result as ArrayBuffer);\n        reader.onerror = reject;\n        reader.readAsArrayBuffer(blob);","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/frontend/app/view/term/termutil.ts#L66-L102","documentation":"createTempFileFromBlob refuses blobs larger than 5MB before writing a temporary image file (used e.g. for pasting images into the terminal). The 5MB cap protects memory and disk from oversized payloads.","triggerScenarios":"Calling createTempFileFromBlob(tempPath) with a Blob whose blob.size > 5 * 1024 * 1024.","commonSituations":"User pastes or drops a high-resolution screenshot/photo into the terminal; clipboard contains a large image; programmatic image upload exceeds the cap.","solutions":["Resize or recompress the image (e.g. canvas downscale) so the blob is under 5MB","Convert to a more compact format (JPEG/WebP) before creating the temp file","If larger images must be supported, raise the limit in termutil.ts deliberately and test memory impact"],"exampleFix":"// before\nawait createTempFileFromBlob(hugeBlob);\n\n// after\nif (hugeBlob.size > 5 * 1024 * 1024) {\n    hugeBlob = await compressImage(hugeBlob, 0.7);\n}\nconst path = await createTempFileFromBlob(hugeBlob);","handlingStrategy":"validation","validationCode":"const MAX = 5 * 1024 * 1024;\nif (!(blob instanceof Blob) || blob.size > MAX) {\n    throw new Error(\"image must be a Blob under 5MB\");\n}","typeGuard":"function isUploadableImage(blob: unknown): blob is Blob {\n    return blob instanceof Blob && blob.size <= 5 * 1024 * 1024;\n}","tryCatchPattern":"try {\n    const path = await createTempFileFromBlob(blob);\n} catch (e) {\n    if (String(e.message).startsWith(\"Image too large\")) {\n        notifyUser(\"Please use an image under 5MB\");\n    } else { throw e; }\n}","preventionTips":["Downscale screenshots on the client before creating temp files","Check blob.size before any paste/drop handler calls the function","Show a size indicator/limit in the UI for image attachments"],"tags":["file-size","image","validation"],"backgroundTag":"file-too-large","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}