67427e0231
Record picked attachment metadata and keep the web composer's 1 MiB total budget with a refusal that leaves the draft unchanged.
88 lines
3.0 KiB
Dart
88 lines
3.0 KiB
Dart
/// One file picked for the composer's next chat message.
|
|
///
|
|
/// Only the metadata the composer needs is recorded: the file's own name, its
|
|
/// MIME type and its size. The bytes stay with the platform file handle; this
|
|
/// model never reads or copies them.
|
|
class ChatAttachment {
|
|
const ChatAttachment({
|
|
required this.name,
|
|
required this.mime,
|
|
required this.sizeBytes,
|
|
});
|
|
|
|
/// The file name exactly as the picker supplied it. A blank name is refused
|
|
/// by [ChatAttachmentDraft.add] rather than replaced with a fallback.
|
|
final String name;
|
|
|
|
/// The recorded MIME type, possibly empty when the picker supplied none.
|
|
final String mime;
|
|
|
|
/// The recorded file size in bytes.
|
|
final int sizeBytes;
|
|
|
|
@override
|
|
bool operator ==(Object other) =>
|
|
other is ChatAttachment &&
|
|
other.name == name &&
|
|
other.mime == mime &&
|
|
other.sizeBytes == sizeBytes;
|
|
|
|
@override
|
|
int get hashCode => Object.hash(name, mime, sizeBytes);
|
|
}
|
|
|
|
/// Why a draft refused an attachment. [message] is the operator-facing reason;
|
|
/// the refusal leaves the draft exactly as it was.
|
|
enum ChatAttachmentRefusal {
|
|
/// The attachment has no recorded file name.
|
|
blankName('An attachment needs a file name.'),
|
|
|
|
/// Keeping this attachment would take the draft past
|
|
/// [ChatAttachmentDraft.maxTotalBytes]. The wording matches the web composer.
|
|
totalTooLarge('Attachments are too large for web chat (1 MiB total).');
|
|
|
|
const ChatAttachmentRefusal(this.message);
|
|
|
|
final String message;
|
|
}
|
|
|
|
/// The files attached to one conversation's next message.
|
|
///
|
|
/// Mirrors the web composer's 1 MiB total budget across attachments: an
|
|
/// attachment that would exceed it is refused with its own reason, and every
|
|
/// attachment already in the draft stays. Attachments are memory-only; nothing
|
|
/// here persists, reads file bytes or sends a prompt.
|
|
class ChatAttachmentDraft {
|
|
/// The largest total the web composer allows across all attachments.
|
|
static const int maxTotalBytes = 1 << 20;
|
|
|
|
final List<ChatAttachment> _attachments = [];
|
|
|
|
/// The attachments in the order they were added.
|
|
List<ChatAttachment> get attachments => List.unmodifiable(_attachments);
|
|
|
|
/// The recorded sizes added together.
|
|
int get totalBytes =>
|
|
_attachments.fold(0, (total, attachment) => total + attachment.sizeBytes);
|
|
|
|
bool get isEmpty => _attachments.isEmpty;
|
|
|
|
/// Adds [attachment] unless it has a blank name or would exceed the total,
|
|
/// in which case nothing changes and the refusal is returned. A null result
|
|
/// means the attachment was kept.
|
|
ChatAttachmentRefusal? add(ChatAttachment attachment) {
|
|
if (attachment.name.trim().isEmpty) {
|
|
return ChatAttachmentRefusal.blankName;
|
|
}
|
|
if (attachment.sizeBytes > maxTotalBytes - totalBytes) {
|
|
return ChatAttachmentRefusal.totalTooLarge;
|
|
}
|
|
_attachments.add(attachment);
|
|
return null;
|
|
}
|
|
|
|
/// Removes one matching attachment, leaving the others in order. Returns
|
|
/// false when it is not in the draft.
|
|
bool remove(ChatAttachment attachment) => _attachments.remove(attachment);
|
|
}
|