{"record":{"id":"26a0c26a6b1cdcb1","repo":"jackwener/OpenCLI","slug":"failed-to-verify-downloaded-bytes-for-item-filen","errorCode":null,"errorMessage":"Failed to verify downloaded bytes for ${item.filename}","messagePattern":"Failed to verify downloaded bytes for (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/instagram/download.js","lineNumber":335,"sourceCode":"        throw new CliError('RATE_LIMITED', message, 'Wait a few minutes and retry, or switch to a browser session with a warmer Instagram login state.', EXIT_CODES.TEMPFAIL);\n    }\n    if (result.errorCode === 'PRIVATE_OR_UNAVAILABLE') {\n        throw new CommandExecutionError(message, 'Open the post in a logged-in browser session and retry');\n    }\n    throw new CommandExecutionError(message);\n}\nasync function downloadInstagramMedia(items, outputDir) {\n    fs.mkdirSync(outputDir, { recursive: true });\n    for (const item of items) {\n        const destPath = path.join(outputDir, item.filename);\n        const result = await httpDownload(item.url, destPath, {\n            timeout: item.type === 'video' ? 120000 : 60000,\n        });\n        if (!result.success) {\n            throw new CommandExecutionError(`Failed to download ${item.filename}: ${result.error || 'unknown error'}`);\n        }\n        if (!Number.isFinite(result.size) || result.size <= 0) {\n            throw new CommandExecutionError(`Failed to verify downloaded bytes for ${item.filename}`);\n        }\n    }\n}\ncli({\n    site: 'instagram',\n    name: 'download',\n    access: 'read',\n    description: 'Download images and videos from Instagram posts and reels',\n    domain: 'www.instagram.com',\n    strategy: Strategy.COOKIE,\n    navigateBefore: false,\n    args: [\n        { name: 'url', positional: true, required: true, help: 'Instagram post / reel / tv URL' },\n        { name: 'path', default: '~/Downloads/Instagram', help: 'Download directory' },\n    ],\n    func: async (page, kwargs) => {\n        const browserPage = ensurePage(page);\n        const target = parseInstagramMediaTarget(String(kwargs.url ?? ''));","sourceCodeStart":317,"sourceCodeEnd":353,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/instagram/download.js#L317-L353","documentation":"After a successful httpDownload, downloadInstagramMedia verifies result.size is a finite positive number; otherwise it throws this CommandExecutionError. It guards against zero-byte or corrupted files (e.g. the CDN returned an error page or empty body) being silently saved as valid media.","triggerScenarios":"httpDownload reports success but result.size is 0, negative, or NaN — empty response body, server returned 200 with zero bytes, or the download implementation failed to report written byte count.","commonSituations":"Instagram CDN returning an empty 200 response; disk-full or quota issues that truncate writes; a bug or interception in httpDownload that doesn't populate size; content-encoding issues producing empty bodies.","solutions":["Delete the zero-byte file and re-run the download","Check free disk space in the output directory","Retry — transient CDN issues often resolve","If reproducible, upgrade the CLI or inspect httpDownload for size reporting bugs"],"exampleFix":"// after catching, clean up and retry\ntry {\n  await downloadInstagramMedia(mediaItems, savedDir);\n} catch (e) {\n  if (String(e.message).includes('Failed to verify downloaded bytes')) {\n    fs.rmSync(savedDir, { recursive: true, force: true });\n  }\n  throw e;\n}","handlingStrategy":"validation","validationCode":"// after download, before trusting the file\nconst stat = fs.statSync(destPath);\nif (!stat.isFile() || stat.size === 0) throw new Error('Downloaded file is empty: ' + destPath);","typeGuard":"function hasValidSize(result) {\n  return Number.isFinite(result.size) && result.size > 0;\n}","tryCatchPattern":"try {\n  await downloadInstagramMedia(items, dir);\n} catch (e) {\n  if (String(e.message).includes('Failed to verify downloaded bytes')) {\n    fs.rmSync(dir, { recursive: true, force: true }); // remove corrupt output\n  } else throw e;\n}","preventionTips":["Check disk free space before large downloads","Delete zero-byte files before re-running","Validate file size / magic bytes after every download","If it recurs for all files, suspect an httpDownload bug and update the CLI"],"tags":["instagram","download-failure","corrupt-file","validation"],"backgroundTag":"empty-file-downloaded","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}