Files
pipelines/app/lib/features/chat/domain/chat_attachment_picker.dart
T

88 lines
3.1 KiB
Dart

import 'dart:typed_data';
import 'chat_attachment_draft.dart';
import 'chat_attachment_picker_stub.dart'
if (dart.library.js_interop) 'chat_attachment_picker_web.dart'
as impl;
/// One file the operator chose in the host's file picker, with the bytes the
/// host handed back.
///
/// The seam records only what the picker supplied: the file's own name, its
/// MIME type and its bytes. It applies no name fallback and no size or count
/// policy; [ChatAttachmentDraft] owns those decisions.
class ChatPickedFile {
const ChatPickedFile({
required this.name,
required this.mime,
required this.bytes,
});
/// The file name exactly as the host recorded it, blank when the host
/// supplied none. [ChatAttachmentDraft.add] refuses a blank name rather than
/// replacing it.
final String name;
/// The MIME type the host recorded, empty when the host supplied none.
final String mime;
/// The file's complete contents as read when it was chosen.
final Uint8List bytes;
/// The recorded size, taken from the bytes that were actually read.
int get sizeBytes => bytes.length;
/// The draft row for this file. The draft still owns the blank-name and
/// total-size decisions.
ChatAttachment get attachment =>
ChatAttachment(name: name, mime: mime, sizeBytes: sizeBytes);
}
/// Opens the host's file selection and returns the files the operator chose.
///
/// A null result means the operator dismissed the picker without choosing
/// anything, which is different from an empty list. Only one selection is
/// returned per call.
typedef ChatFileSelection = Future<List<ChatPickedFile>?> Function();
/// The file picker seam for one host.
///
/// [pick] is the operator's explicit pick action: on web it opens the host's
/// native file input, and the chosen files are read before the future
/// completes. A host without a picker constructs
/// [ChatAttachmentPicker.unsupported] so the missing capability is reported
/// plainly instead of silently returning nothing. Tests inject a fake
/// [select] to exercise the seam without a browser.
class ChatAttachmentPicker {
const ChatAttachmentPicker({required ChatFileSelection this.select})
: unsupportedMessage = null;
const ChatAttachmentPicker.unsupported(String message)
: select = null,
unsupportedMessage = message;
/// The host selection, or null when this host cannot pick files.
final ChatFileSelection? select;
/// Why this host cannot pick files, when [select] is null.
final String? unsupportedMessage;
/// Whether this host can open a file selection at all.
bool get isSupported => select != null;
/// Opens the host's file selection and resolves with the chosen files, or
/// null when the operator cancelled.
///
/// Throws [UnsupportedError] with [unsupportedMessage] on a host that cannot
/// pick files.
Future<List<ChatPickedFile>?> pick() async {
final select = this.select;
if (select == null) throw UnsupportedError(unsupportedMessage!);
return select();
}
}
/// The picker for the running host.
ChatAttachmentPicker get hostChatAttachmentPicker =>
impl.hostChatAttachmentPicker;