{"record":{"id":"4ddba3199d312a45","repo":"jackwener/OpenCLI","slug":"midjourney-original-image-returned-invalid-media-b","errorCode":null,"errorMessage":"Midjourney original image returned invalid media bytes from ${resolved.url}","messagePattern":"Midjourney original image returned invalid media bytes from (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/midjourney/utils.js","lineNumber":664,"sourceCode":"    }\n  }\n\n  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) {","sourceCodeStart":646,"sourceCodeEnd":682,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/midjourney/utils.js#L646-L682","documentation":"Even though the transfer completed, sniffing the actual magic bytes of the downloaded buffer showed it is not an image (actualMime does not start with 'image/'). The library validates file content, not just headers, so corrupt or wrongly-typed payloads are caught before writing the file.","triggerScenarios":"The CDN (or an intermediary) served HTML/JSON/error text that happened to pass header checks; the downloaded bytes are truncated or corrupted so no known magic number matches; the URL resolves to a non-image resource.","commonSituations":"Proxy/cache serving a captive-portal or error page with a 200; partially flushed downloads; requesting a URL that is actually a webpage.","solutions":["Re-download with a freshly resolved URL.","Inspect the downloaded bytes (hexdump the first bytes) to see what was actually served.","Check for proxies/VPNs injecting content and disable them.","Confirm the job produced an original image (not a video) for this index."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const res = await fetch(url);\nconst head = new Uint8Array(await res.arrayBuffer()).slice(0, 8);\nconst isPng = head[0] === 0x89 && head[1] === 0x50;\nconst isJpeg = head[0] === 0xff && head[1] === 0xd8;\nif (!isPng && !isJpeg) throw new Error('URL does not serve image bytes');","typeGuard":"function isImageBuffer(buf) {\n  return buf.length > 4 &&\n    ((buf[0] === 0x89 && buf[1] === 0x50) || (buf[0] === 0xff && buf[1] === 0xd8));\n}","tryCatchPattern":"try {\n  await downloadOriginals(page, jobId, indices, outDir);\n} catch (err) {\n  if (/invalid media bytes/.test(err.message)) {\n    const freshUrl = await resolveFreshUrl(jobId);\n    await downloadOriginals(page, jobId, indices, outDir);\n  } else throw err;\n}","preventionTips":["Prefer freshly resolved CDN URLs over cached ones.","Check network middleboxes (proxies/VPNs) that can inject HTML responses.","Verify job output type (image vs video) before downloading originals.","Keep the library's magic-byte sniffing enabled rather than bypassing it."],"tags":["validation","magic-bytes","corruption","midjourney"],"backgroundTag":"invalid-file-bytes","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}