From 3138048b96c6f8094bdd50357579c83ea322763d Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 23 Aug 2025 15:25:12 -0500 Subject: [PATCH] fix: cannot load thumbnail from unknown content length (#21192) * fix: cannot load thumbnail from unknown content length * pr feedback * pr feedback --- .../loaders/remote_image_request.dart | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/mobile/lib/infrastructure/loaders/remote_image_request.dart b/mobile/lib/infrastructure/loaders/remote_image_request.dart index fe62469461..f228f5de17 100644 --- a/mobile/lib/infrastructure/loaders/remote_image_request.dart +++ b/mobile/lib/infrastructure/loaders/remote_image_request.dart @@ -64,18 +64,48 @@ class RemoteImageRequest extends ImageRequest { if (_isCancelled) { return null; } - final bytes = Uint8List(response.contentLength); + + // Handle unknown content length from reverse proxy + final contentLength = response.contentLength; + final Uint8List bytes; int offset = 0; - final subscription = response.listen((List chunk) { - // this is important to break the response stream if the request is cancelled - if (_isCancelled) { - throw StateError('Cancelled request'); + + if (contentLength >= 0) { + // Known content length - use pre-allocated buffer + bytes = Uint8List(contentLength); + final subscription = response.listen((List chunk) { + // this is important to break the response stream if the request is cancelled + if (_isCancelled) { + throw StateError('Cancelled request'); + } + bytes.setAll(offset, chunk); + offset += chunk.length; + }, cancelOnError: true); + cacheManager?.putStreamedFile(url, response); + await subscription.asFuture(); + } else { + // Unknown content length - collect chunks dynamically + final chunks = >[]; + int totalLength = 0; + final subscription = response.listen((List chunk) { + // this is important to break the response stream if the request is cancelled + if (_isCancelled) { + throw StateError('Cancelled request'); + } + chunks.add(chunk); + totalLength += chunk.length; + }, cancelOnError: true); + cacheManager?.putStreamedFile(url, response); + await subscription.asFuture(); + + // Combine all chunks into a single buffer + bytes = Uint8List(totalLength); + for (final chunk in chunks) { + bytes.setAll(offset, chunk); + offset += chunk.length; } - bytes.setAll(offset, chunk); - offset += chunk.length; - }, cancelOnError: true); - cacheManager?.putStreamedFile(url, response); - await subscription.asFuture(); + } + return await ImmutableBuffer.fromUint8List(bytes); }