mirror of
https://github.com/immich-app/immich.git
synced 2025-08-30 23:02:39 -04:00
fix: cannot load thumbnail from unknown content length (#21192)
* fix: cannot load thumbnail from unknown content length * pr feedback * pr feedback
This commit is contained in:
parent
f8b41ea8aa
commit
3138048b96
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user