8c421952db
Operator-attached images render as rounded thumbnails above the user bubble and open a zoomable Material/Cupertino viewer. Inline data: URIs decode locally; file:// attachment-store URIs load through the new chat_attachment_image websocket request, which serves only regular image files directly inside the private attachment store (os.Root, <= 4 MiB, sniffed raster types). Missing files show an 'Image no longer available' placeholder; other files keep the file row.
108 lines
3.6 KiB
Dart
108 lines
3.6 KiB
Dart
import 'dart:typed_data';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
|
|
/// Raster types the transcript can draw. SVG and unknown types stay file rows.
|
|
const Set<String> _imageMimes = {
|
|
'image/png',
|
|
'image/jpeg',
|
|
'image/jpg',
|
|
'image/gif',
|
|
'image/webp',
|
|
'image/bmp',
|
|
};
|
|
|
|
const Map<String, String> _imageExtensions = {
|
|
'png': 'image/png',
|
|
'jpg': 'image/jpeg',
|
|
'jpeg': 'image/jpeg',
|
|
'gif': 'image/gif',
|
|
'webp': 'image/webp',
|
|
'bmp': 'image/bmp',
|
|
};
|
|
|
|
/// Where a transcript image's bytes come from.
|
|
enum ChatImageSource {
|
|
/// A `data:` URI the history already carries (Claude Code transcripts).
|
|
inline,
|
|
|
|
/// A server-local `file://` path read through `chat_attachment_image`.
|
|
stored,
|
|
|
|
/// A just-sent prompt's upload, known only by its stored name until the
|
|
/// server echoes the recorded turn.
|
|
upload,
|
|
}
|
|
|
|
/// The image type a file part names: its own MIME, else the type of a `data:`
|
|
/// URI, else its name or URI extension. Null when it is not a drawable image.
|
|
String? chatPartImageMime(JsonMap part) {
|
|
if (part['type'] != 'file') return null;
|
|
final mime = '${part['mime'] ?? ''}'.trim().toLowerCase();
|
|
if (mime.isNotEmpty && mime != 'application/octet-stream') {
|
|
return _imageMimes.contains(mime) ? mime : null;
|
|
}
|
|
final uri = '${part['uri'] ?? ''}'.trim();
|
|
if (uri.startsWith('data:')) {
|
|
final type = uri.substring(5).split(RegExp('[;,]')).first.toLowerCase();
|
|
return _imageMimes.contains(type) ? type : null;
|
|
}
|
|
for (final candidate in ['${part['name'] ?? ''}', uri]) {
|
|
final dot = candidate.lastIndexOf('.');
|
|
if (dot < 0) continue;
|
|
final type = _imageExtensions[candidate.substring(dot + 1).toLowerCase()];
|
|
if (type != null) return type;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// How a drawable image part is loaded, or null when its URI is one the client
|
|
/// does not fetch (remote http(s) URLs stay plain file rows).
|
|
ChatImageSource? chatImageSourceOf(String uri) {
|
|
if (uri.startsWith('data:')) return ChatImageSource.inline;
|
|
if (uri.startsWith('file:///')) return ChatImageSource.stored;
|
|
if (uri.startsWith(chatUploadUriPrefix)) return ChatImageSource.upload;
|
|
return null;
|
|
}
|
|
|
|
/// The optimistic row's URI for a file the composer uploaded ahead of the
|
|
/// prompt; the loader resolves it from the composer's own bytes.
|
|
const String chatUploadUriPrefix = 'upload:';
|
|
|
|
/// The URI a just-sent turn's file part carries before the server records it:
|
|
/// an inline file's own `data:` URI, an uploaded file's `upload:` name, else
|
|
/// its name (drawn as a plain file row).
|
|
String chatOptimisticAttachmentUri(JsonMap wirePart) {
|
|
final name = '${wirePart['name'] ?? ''}';
|
|
final data = '${wirePart['data'] ?? ''}';
|
|
final upload = '${wirePart['upload'] ?? ''}';
|
|
if (upload.isNotEmpty) return '$chatUploadUriPrefix$upload';
|
|
if (data.isNotEmpty) {
|
|
final mime = '${wirePart['mime'] ?? ''}'.trim();
|
|
return 'data:${mime.isEmpty ? 'application/octet-stream' : mime};base64,$data';
|
|
}
|
|
return name;
|
|
}
|
|
|
|
/// The stored file name a `file://` or `upload:` URI ends in; the upload store
|
|
/// names files `up-<id>-<name>`, so the same bytes match before and after the
|
|
/// server records the turn.
|
|
String chatStoredFileName(String uri) {
|
|
if (uri.startsWith(chatUploadUriPrefix)) {
|
|
return uri.substring(chatUploadUriPrefix.length);
|
|
}
|
|
final path = Uri.tryParse(uri)?.pathSegments ?? const <String>[];
|
|
return path.isEmpty ? '' : path.last;
|
|
}
|
|
|
|
/// The bytes of a base64 `data:` image URI, or null when it is malformed.
|
|
Uint8List? chatDataUriBytes(String uri) {
|
|
try {
|
|
final data = UriData.parse(uri);
|
|
if (!data.isBase64) return null;
|
|
return data.contentAsBytes();
|
|
} on FormatException {
|
|
return null;
|
|
}
|
|
}
|