{"record":{"id":"1fc32333080e1934","repo":"jackwener/OpenCLI","slug":"midjourney-media-download-returned-an-empty-file-f","errorCode":null,"errorMessage":"Midjourney media download returned an empty file from ${url}","messagePattern":"Midjourney media download returned an empty file from (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/midjourney/utils.js","lineNumber":564,"sourceCode":"        return {\n          ok: true,\n          status: response.status,\n          type: response.headers.get('content-type') || '',\n          size: bytes.length,\n        };\n      }, url, transferKey));\n    } catch (error) {\n      throw new CommandExecutionError(`Midjourney browser-context media fetch failed: ${errorMessage(error)}`);\n    }\n    if (!payload || typeof payload !== 'object' || !payload.ok) {\n      throw new CommandExecutionError(`Midjourney media download failed: HTTP ${payload?.status ?? 0} from ${url}`);\n    }\n    if (!String(payload.type || '').startsWith(expectedMimePrefix)) {\n      throw new CommandExecutionError(`Midjourney media download returned unexpected content type \"${payload.type || 'unknown'}\"`);\n    }\n    const size = Number(payload.size);\n    if (!Number.isInteger(size) || size <= 0) {\n      throw new CommandExecutionError(`Midjourney media download returned an empty file from ${url}`);\n    }\n\n    // Returning a complete base64 file in one Browser Bridge response can\n    // exceed the daemon message limit. Pull it out in bounded chunks instead.\n    const parts = [];\n    const chunkSize = 96 * 1024;\n    for (let offset = 0; offset < size; offset += chunkSize) {\n      const base64 = unwrapEvaluateResult(await page.evaluate((key, start, length) => {\n        const bytes = window[key];\n        if (!(bytes instanceof Uint8Array)) return null;\n        const chunk = bytes.subarray(start, Math.min(bytes.length, start + length));\n        let binary = '';\n        const binaryChunkSize = 0x8000;\n        for (let index = 0; index < chunk.length; index += binaryChunkSize) {\n          binary += String.fromCharCode(...chunk.subarray(index, index + binaryChunkSize));\n        }\n        return btoa(binary);\n      }, transferKey, offset, chunkSize));","sourceCodeStart":546,"sourceCodeEnd":582,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/midjourney/utils.js#L546-L582","documentation":"The download produced zero bytes (or a non-integer/non-positive size), so the file would be empty. The library treats empty payloads as a hard failure rather than writing a 0-byte file to disk.","triggerScenarios":"The CDN returned HTTP 200 with an empty body; the in-page buffer reported a size of 0 or a non-numeric size; a redirect to an empty resource.","commonSituations":"Midjourney CDN serving empty responses for just-deleted or not-yet-finished media; transient CDN faults; fetching media for a job whose generation silently failed.","solutions":["Retry the download; often transient.","Check the job status first to confirm the media actually exists.","Re-resolve the media URL and verify it serves bytes (e.g. via curl).","Wait until the job fully completes before downloading."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"const res = await fetch(url, { method: 'HEAD' });\nconst len = Number(res.headers.get('content-length') || 0);\nif (!len) throw new Error('No content at ' + url + '; job media may not exist yet');","typeGuard":null,"tryCatchPattern":"try {\n  await downloadMedia(page, url);\n} catch (err) {\n  if (/empty file/.test(err.message)) {\n    await sleep(5000);\n    await downloadMedia(page, url); // transient empty responses often resolve\n  } else throw err;\n}","preventionTips":["Confirm the job reached a completed status before downloading.","Retry with delay; empty 200s from CDNs are frequently transient.","Verify the URL serves bytes out-of-band (curl -I) when debugging.","Avoid downloading immediately at job creation time."],"tags":["validation","empty-response","cdn","midjourney"],"backgroundTag":"empty-download-response","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}