{"record":{"id":"5ed7a55bbcf2b744","repo":"jackwener/OpenCLI","slug":"could-not-write-midjourney-image-filepath-er","errorCode":null,"errorMessage":"Could not write Midjourney image ${filePath}: ${errorMessage(error)}","messagePattern":"Could not write Midjourney image (.+?): (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/midjourney/utils.js","lineNumber":684,"sourceCode":"  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  }\n  return { index, filePath, bytes: media.buffer.length, url: resolved.url, mime: actualMime, cached: false };\n}\n\nexport async function downloadOriginals(page, jobId, indices, outputDir, force = false) {\n  await fs.mkdir(outputDir, { recursive: true });\n  const files = [];\n  for (const index of indices) files.push(await downloadOne(page, jobId, index, outputDir, force));\n  return files;\n}\n\nexport function rawVideoUrl(jobId, index) {\n  return `${MIDJOURNEY_CDN}/video/${jobId}/${index}.mp4`;\n}\n\nasync function writeMediaBuffer(filePath, buffer) {\n  const tempPath = `${filePath}.part-${process.pid}-${Date.now()}`;\n  try {","sourceCodeStart":666,"sourceCodeEnd":702,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/midjourney/utils.js#L666-L702","documentation":"Writing the downloaded image bytes to filePath failed at the filesystem level (temp write or atomic rename). The temp .part file is cleaned up and the underlying OS error is surfaced via errorMessage(error).","triggerScenarios":"The output directory does not exist or is not writable; disk full (ENOSPC); permission denied on the target path; target path too long or on a read-only mount; fs.rename fails across devices or on a locked file.","commonSituations":"Wrong output directory configured; running in a container with a read-only volume; quota/disk-space exhaustion on large image batches; antivirus locking files on Windows.","solutions":["Check that outputDir exists and is writable (mkdir -p, check permissions).","Free disk space if ENOSPC is reported.","Fix permissions on the output path or run with a user that can write there.","Ensure filePath is valid and not too long for the filesystem."],"exampleFix":"// before\nawait downloadOriginals(page, jobId, [0], '/read-only/out');\n// after\nawait fs.mkdir('/data/out', { recursive: true });\nawait downloadOriginals(page, jobId, [0], '/data/out');","handlingStrategy":"validation","validationCode":"import fs from 'node:fs/promises';\nawait fs.mkdir(outputDir, { recursive: true });\nawait fs.access(outputDir, fs.constants.W_OK);","typeGuard":null,"tryCatchPattern":"try {\n  await downloadOriginals(page, jobId, indices, outDir);\n} catch (err) {\n  if (/Could not write Midjourney image/.test(err.message)) {\n    await fs.mkdir(outDir, { recursive: true });\n    if (err.message.includes('ENOSPC')) throw new Error('Free disk space required');\n    await downloadOriginals(page, jobId, indices, outDir);\n  } else throw err;\n}","preventionTips":["Create the output directory with { recursive: true } before downloading.","Check write permissions and disk space before batch downloads.","Avoid read-only or network mounts as output destinations.","Run the CLI as a user with write access to the target path."],"tags":["filesystem","io","write-failure","midjourney"],"backgroundTag":"file-write-failed","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}