{"record":{"id":"ce59acd99d4c36af","repo":"danny-avila/LibreChat","slug":"could-not-determine-file-extension-from-mime-type","errorCode":null,"errorMessage":"Could not determine file extension from MIME type: ${type}","messagePattern":"Could not determine file extension from MIME type: (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"api/server/services/Files/process.js","lineNumber":1269,"sourceCode":"    throw new Error(`Failed to convert base64 to buffer: ${error.message}`);\n  }\n}\n\nasync function saveBase64Image(\n  url,\n  { req, file_id: _file_id, filename: _filename, endpoint, context, resolution },\n) {\n  const appConfig = req.config;\n  const effectiveResolution = resolution ?? appConfig.fileConfig?.imageGeneration ?? 'high';\n  const file_id = _file_id ?? v4();\n  let filename = `${file_id}-${_filename}`;\n  const { buffer: inputBuffer, type } = base64ToBuffer(url);\n  if (!path.extname(_filename)) {\n    const extension = mime.getExtension(type);\n    if (extension) {\n      filename += `.${extension}`;\n    } else {\n      throw new Error(`Could not determine file extension from MIME type: ${type}`);\n    }\n  }\n\n  const image = await resizeImageBuffer(inputBuffer, effectiveResolution, endpoint);\n  const source = getFileStrategy(appConfig, { isImage: true });\n  const { saveBuffer } = getStrategyFunctions(source);\n  const filepath = await saveBuffer({\n    userId: req.user.id,\n    fileName: filename,\n    buffer: image.buffer,\n    tenantId: req.user.tenantId,\n  });\n  const storageMetadata = getStorageMetadata({ filepath, source });\n  return await db.createFile(\n    {\n      type,\n      source,\n      context,","sourceCodeStart":1251,"sourceCodeEnd":1287,"githubUrl":"https://github.com/danny-avila/LibreChat/blob/5ff282f9006c436e561de1afd39a481bea1ef0d8/api/server/services/Files/process.js#L1251-L1287","documentation":"Thrown by saveBase64Image when the parsed MIME `type` from the data URL prefix yields no extension via `mime.getExtension(type)` AND the supplied `_filename` has no extension already. The function needs a file extension to build a storage filename; without one (and no resolvable MIME extension), it refuses rather than store an extension-less file.","triggerScenarios":"Caller passes a `data:` URL whose MIME segment is unregistered or malformed (e.g., `data:image-jpg;base64,` — note the dash instead of slash — or `data:foo/bar;base64,`) AND omits an extension on `_filename`. mime.getExtension returns undefined for unknown MIME strings, triggering the throw.","commonSituations":"Producer emits a typo'd MIME like `image/jpg` (mime expects `image/jpeg`), `image/png-`, or a private/custom MIME; client sends `filename: 'avatar'` (no extension) alongside such a MIME; hand-crafted data URLs with incorrect Content-Type segments.","solutions":["Use the canonical MIME spelling: `image/jpeg` (not `image/jpg`), `image/png`, `image/webp`, `image/gif`.","Pass a filename that already has the correct extension, so the MIME-extension lookup is skipped.","Sanitize the MIME segment before calling: ensure it matches `^[a-z]+/[a-z0-9.+-]+$`.","If you genuinely need a custom type, add an extension on `_filename` to bypass the lookup."],"exampleFix":"// before\nsaveBase64Image('data:image/jpg;base64,...', { req, filename: 'avatar' });\n\n// after\nsaveBase64Image('data:image/jpeg;base64,...', { req, filename: 'avatar.jpg' });","handlingStrategy":"validation","validationCode":"const MIME_EXT = { 'image/jpeg': 'jpg', 'image/png': 'png', 'image/webp': 'webp', 'image/gif': 'gif' };\nfunction ensureFilenameExt(filename, mime) {\n  if (path.extname(filename)) return filename;\n  const ext = MIME_EXT[mime] || mime.getExtension(mime);\n  if (!ext) throw new Error(`Unknown MIME type: ${mime}`);\n  return `${filename}.${ext}`;\n}","typeGuard":null,"tryCatchPattern":"try { await saveBase64Image(url, ctx); }\ncatch (e) {\n  if (/Could not determine file extension/.test(e.message)) return res.status(400).json({ error: 'Unrecognized image MIME type' });\n  throw e;\n}","preventionTips":["Use canonical MIME spellings (`image/jpeg`, not `image/jpg`).","Always pass a filename with the correct extension as a fallback.","Validate the MIME segment format (`type/subtype`) before constructing the data URL."],"tags":["mime-type","filename","base64","input-validation"],"backgroundTag":null,"analyzedSha":"5ff282f9006c436e561de1afd39a481bea1ef0d8","analyzedAt":"2026-08-12T21:38:08.145Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}