{"record":{"id":"5250d7719e7c6ace","repo":"babalae/better-genshin-impact","slug":"maxdownloadbytes-1024-1024-mb","errorCode":null,"errorMessage":"图片大小超过 {MaxDownloadBytes / 1024 / 1024} MB 限制。","messagePattern":"图片大小超过 (.+?) MB 限制。","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/Service/BannerImageService.cs","lineNumber":95,"sourceCode":"\n        using var timeoutCancellationTokenSource = new CancellationTokenSource(DownloadTimeout);\n        using var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(\n            cancellationToken,\n            timeoutCancellationTokenSource.Token);\n        var linkedCancellationToken = linkedCancellationTokenSource.Token;\n\n        try\n        {\n            // 下载图片\n            using var response = await _httpClient.GetAsync(\n                uri,\n                HttpCompletionOption.ResponseHeadersRead,\n                linkedCancellationToken).ConfigureAwait(false);\n            response.EnsureSuccessStatusCode();\n\n            if (response.Content.Headers.ContentLength > MaxDownloadBytes)\n            {\n                throw new InvalidDataException($\"图片大小超过 {MaxDownloadBytes / 1024 / 1024} MB 限制。\");\n            }\n\n            await using (var responseStream = await response.Content\n                             .ReadAsStreamAsync(linkedCancellationToken)\n                             .ConfigureAwait(false))\n            await using (var fileStream = new FileStream(\n                             tempPath,\n                             FileMode.CreateNew,\n                             FileAccess.Write,\n                             FileShare.None,\n                             81920,\n                             FileOptions.Asynchronous | FileOptions.SequentialScan))\n            {\n                var buffer = new byte[81920];\n                long totalBytes = 0;\n                int read;\n                while ((read = await responseStream\n                           .ReadAsync(buffer.AsMemory(), linkedCancellationToken)","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Service/BannerImageService.cs#L77-L113","documentation":"An InvalidDataException thrown by DownloadAndSaveAsync right after the response headers are read, when the declared Content-Length header exceeds MaxDownloadBytes. This is the early, cheap size guard that fails before streaming any body bytes. It protects against oversized banner images and memory/disk abuse.","triggerScenarios":"The server returns a response whose Content-Length header is greater than MaxDownloadBytes (the configured banner size limit). Triggered for legitimately large images or a wrong/abusive URL.","commonSituations":"Pointing the banner URL at a high-resolution original (tens of MB); an image host serving an uncompressed source; a misconfigured URL returning a large file; MaxDownloadBytes set lower than typical banner sizes.","solutions":["Use a smaller, web-optimized image (resize/compress the source to stay under the limit).","Pick a different URL whose Content-Length is within MaxDownloadBytes.","If the limit is genuinely too low for your use case, raise MaxDownloadBytes (review the trade-off first).","Catch InvalidDataException and tell the user the image is too large."],"exampleFix":"null","handlingStrategy":"try-catch","validationCode":"// Issue a HEAD request first to read Content-Length without downloading the body\nusing var head = await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, uri), ct);\nif (head.Content.Headers.ContentLength > MaxDownloadBytes) { /* reject early */ }","typeGuard":"null","tryCatchPattern":"try { await _banner.DownloadAndSaveAsync(url, ct); }\ncatch (InvalidDataException ex) { Toast.Warning(ex.Message); }","preventionTips":["Use web-optimized images under MaxDownloadBytes.","Pre-check size with a HEAD request when the source is user-supplied.","Catch InvalidDataException and inform the user."],"tags":["download","size-limit","validation","http-headers"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}