Files
pipelines/app/lib/features/chat/files/files_thumbnail_image.dart
T

80 lines
2.3 KiB
Dart

import 'dart:ui' as ui;
import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart';
import '../../../core/session/server_session.dart';
import '../../artifacts/ui/artifact_viewer_fetch.dart';
typedef ThumbnailHeaders = Map<String, String> Function(Uri uri);
/// Native thumbnail transport shares the viewer's scoped, no-redirect fetch.
/// Cache keys retain only the address and credential snapshot, never photo bytes.
class FileThumbnailImage extends ImageProvider<FileThumbnailImage> {
FileThumbnailImage(
this.uri, {
this.fetch = fetchArtifact,
ThumbnailHeaders headers = ServerSession.headersFor,
}) : _headers = headers,
_credentials = Map.unmodifiable(headers(uri));
final Uri uri;
final ArtifactFetch fetch;
final ThumbnailHeaders _headers;
final Map<String, String> _credentials;
@override
Future<FileThumbnailImage> obtainKey(ImageConfiguration configuration) =>
SynchronousFuture<FileThumbnailImage>(this);
@override
ImageStreamCompleter loadImage(
FileThumbnailImage key,
ImageDecoderCallback decode,
) => MultiFrameImageStreamCompleter(
codec: key._load(decode),
scale: 1,
debugLabel: 'Authenticated file thumbnail',
);
void _checkCredentials() {
if (!mapEquals(_credentials, _headers(uri))) {
throw StateError('The thumbnail sign-in changed. Reload the view.');
}
}
Future<ui.Codec> _load(ImageDecoderCallback decode) async {
_checkCredentials();
final response = await fetch(uri);
_checkCredentials();
if (!response.ok) {
throw NetworkImageLoadException(statusCode: response.status, uri: uri);
}
final buffer = await ui.ImmutableBuffer.fromUint8List(response.bytes);
return decode(buffer);
}
@override
bool operator ==(Object other) =>
other is FileThumbnailImage &&
other.uri == uri &&
other.fetch == fetch &&
mapEquals(other._credentials, _credentials);
@override
int get hashCode {
final names = _credentials.keys.toList()..sort();
return Object.hash(
uri,
fetch,
Object.hashAll(
names.map((name) => Object.hash(name, _credentials[name])),
),
);
}
// Widget/error diagnostics must never print credential values.
@override
String toString() => 'FileThumbnailImage';
}