{"record":{"id":"959cbb7e2e93cb91","repo":"jackwener/OpenCLI","slug":"midjourney-cdn-reported-media-mime-unknown","errorCode":null,"errorMessage":"Midjourney CDN reported ${media.mime || 'unknown'} for ${resolved.url}; detected ${actualMime} from file bytes","messagePattern":"Midjourney CDN reported (.+?) for (.+?); detected (.+?) from file bytes","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"clis/midjourney/utils.js","lineNumber":667,"sourceCode":"  let media = null;\n  let resolved = null;\n  let lastError = null;\n  for (const candidate of candidates) {\n    try {\n      media = await fetchMediaThroughPage(page, candidate.url, 'image/');\n      resolved = candidate;\n      break;\n    } catch (error) {\n      lastError = error;\n    }\n  }\n  if (!media || !resolved) throw lastError || new CommandExecutionError(`No Midjourney original image was available for ${jobId}`);\n  const actualMime = sniffMediaMime(media.buffer);\n  if (!actualMime?.startsWith('image/')) {\n    throw new CommandExecutionError(`Midjourney original image returned invalid media bytes from ${resolved.url}`);\n  }\n  if (media.mime !== actualMime) {\n    log.warn(`Midjourney CDN reported ${media.mime || 'unknown'} for ${resolved.url}; detected ${actualMime} from file bytes`);\n  }\n  const extension = actualMime === 'image/png'\n    ? '.png'\n    : actualMime === 'image/webp'\n      ? '.webp'\n      : actualMime === 'image/gif'\n        ? '.gif'\n        : '.jpg';\n  const filePath = path.join(outputDir, `${jobId}_${index}${extension}`);\n\n  const tempPath = `${filePath}.part-${process.pid}-${Date.now()}`;\n  try {\n    await fs.writeFile(tempPath, media.buffer);\n    await fs.rename(tempPath, filePath);\n  } catch (error) {\n    await fs.unlink(tempPath).catch(() => {});\n    throw new CommandExecutionError(`Could not write Midjourney image ${filePath}: ${errorMessage(error)}`);\n  }","sourceCodeStart":649,"sourceCodeEnd":685,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/midjourney/utils.js#L649-L685","documentation":"The Content-Type the Midjourney CDN reported for an original image disagrees with the MIME type sniffed from the actual downloaded bytes. The code already validated the bytes are a real image and only logs a warning, then uses the sniffed type (e.g., to pick the file extension).","triggerScenarios":"Downloading a Midjourney original image where the HTTP response header mime (media.mime) differs from sniffMediaMime(buffer) — but the bytes are still a valid image/* type.","commonSituations":"CDN behind a proxy/CDN layer rewriting Content-Type headers (e.g., serving application/octet-stream), signed URLs served with generic MIME, misconfigured CDN cache, images transcoded mid-flight.","solutions":["No action strictly required — the sniffed MIME wins and the correct extension is applied","If extension matters, verify the saved file opens correctly with the sniffed type","Check whether a proxy/antivirus is rewriting response headers in your environment","Retry the download if the file is actually corrupted"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const mime = require('mime-sniffer'); // or file-type\nconst type = await sniff(buffer);\nif (!type?.startsWith('image/')) throw new Error('Invalid image bytes');","typeGuard":"function isImageMime(m) { return typeof m === 'string' && m.startsWith('image/'); }","tryCatchPattern":"try {\n  await downloadOriginalImage(page, jobId, index);\n} catch (err) {\n  if (err instanceof CommandExecutionError && /invalid media bytes/.test(err.message)) {\n    // retry download or skip this asset\n  }\n}","preventionTips":["Trust sniffed bytes over CDN headers when they disagree","Probe saved files (file(1)/ffprobe) after bulk downloads","Bypass misbehaving proxies when downloading media","Validate media.mime vs actual bytes in download wrappers"],"tags":["mime-mismatch","cdn","midjourney","media-download"],"backgroundTag":"content-type-mismatch","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}