Skip to content

High performance self-hosted photo and video management solution.

fix: cannot load thumbnail from unknown content length (#21192)

Alex3138048b96c6
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<int> 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<int> 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 = <List<int>>[];
+ int totalLength = 0;
+ final subscription = response.listen((List<int> 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);
}