2026-11-21 14:29:52 +08:00
|
|
|
// Attachments shared by the chat composer and the new-session draft: what a
|
|
|
|
|
// picked file becomes (a data URL the server stores) and the chips that show
|
|
|
|
|
// it above the input.
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:file_picker/file_picker.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
2026-11-26 09:04:25 +08:00
|
|
|
import '../l10n.dart';
|
2026-11-21 14:29:52 +08:00
|
|
|
|
|
|
|
|
class ComposerAttachment {
|
|
|
|
|
final String name;
|
|
|
|
|
final String mime;
|
|
|
|
|
final String dataUrl;
|
|
|
|
|
final String? path;
|
|
|
|
|
|
|
|
|
|
const ComposerAttachment({
|
|
|
|
|
required this.name,
|
|
|
|
|
required this.mime,
|
|
|
|
|
required this.dataUrl,
|
|
|
|
|
this.path,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
bool get isImage => mime.startsWith('image/');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Opens the system picker and turns each choice into an attachment. Files
|
|
|
|
|
/// over 10 MB are refused with a message instead of being dropped silently.
|
|
|
|
|
Future<List<ComposerAttachment>> pickComposerAttachments(
|
|
|
|
|
BuildContext context) async {
|
|
|
|
|
final picked = await FilePicker.pickFiles();
|
|
|
|
|
if (picked.isEmpty) return const [];
|
|
|
|
|
final out = <ComposerAttachment>[];
|
|
|
|
|
for (final file in picked) {
|
|
|
|
|
final bytes = await file.readAsBytes();
|
|
|
|
|
if (bytes.length > 10 * 1024 * 1024) {
|
|
|
|
|
if (context.mounted) {
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2026-11-26 09:04:25 +08:00
|
|
|
const SnackBar(content: AppText('附件不能超过 10MB')));
|
2026-11-21 14:29:52 +08:00
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
final mime = _mimeOf(file.name);
|
|
|
|
|
String? keptPath;
|
|
|
|
|
if (mime.startsWith('image/') && file.path != null) {
|
|
|
|
|
keptPath = await _keepImage(file.path!, file.name);
|
|
|
|
|
}
|
|
|
|
|
out.add(ComposerAttachment(
|
|
|
|
|
name: file.name,
|
|
|
|
|
mime: mime,
|
|
|
|
|
dataUrl: 'data:$mime;base64,${base64Encode(bytes)}',
|
|
|
|
|
path: keptPath,
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Chips with a thumbnail, unfolded above the input bar.
|
|
|
|
|
class AttachmentTray extends StatelessWidget {
|
|
|
|
|
final List<ComposerAttachment> items;
|
|
|
|
|
final ValueChanged<int> onRemove;
|
|
|
|
|
|
|
|
|
|
const AttachmentTray({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.items,
|
|
|
|
|
required this.onRemove,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return SizedBox(
|
|
|
|
|
height: 46,
|
|
|
|
|
child: ListView(
|
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
|
children: [
|
|
|
|
|
for (var i = 0; i < items.length; i++)
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.only(right: 6),
|
|
|
|
|
child: InputChip(
|
|
|
|
|
avatar: items[i].path != null
|
|
|
|
|
? CircleAvatar(
|
|
|
|
|
backgroundImage: FileImage(File(items[i].path!)),
|
|
|
|
|
)
|
|
|
|
|
: const Icon(Icons.insert_drive_file, size: 16),
|
|
|
|
|
label: ConstrainedBox(
|
|
|
|
|
constraints: const BoxConstraints(maxWidth: 160),
|
2026-11-26 09:04:25 +08:00
|
|
|
child: AppText(items[i].name,
|
2026-11-21 14:29:52 +08:00
|
|
|
maxLines: 1, overflow: TextOverflow.ellipsis),
|
|
|
|
|
),
|
|
|
|
|
onDeleted: () => onRemove(i),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String _mimeOf(String name) {
|
|
|
|
|
final ext = name.split('.').last.toLowerCase();
|
|
|
|
|
return switch (ext) {
|
|
|
|
|
'png' => 'image/png',
|
|
|
|
|
'jpg' || 'jpeg' => 'image/jpeg',
|
|
|
|
|
'gif' => 'image/gif',
|
|
|
|
|
'webp' => 'image/webp',
|
|
|
|
|
'bmp' => 'image/bmp',
|
|
|
|
|
'avif' => 'image/avif',
|
|
|
|
|
'ico' => 'image/x-icon',
|
|
|
|
|
'pdf' => 'application/pdf',
|
|
|
|
|
'json' => 'application/json',
|
|
|
|
|
'md' || 'markdown' => 'text/markdown',
|
|
|
|
|
'txt' => 'text/plain',
|
|
|
|
|
'csv' => 'text/csv',
|
|
|
|
|
'html' => 'text/html',
|
|
|
|
|
'xml' => 'text/xml',
|
|
|
|
|
'zip' => 'application/zip',
|
|
|
|
|
_ => 'application/octet-stream',
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<String?> _keepImage(String src, String name) async {
|
|
|
|
|
try {
|
|
|
|
|
// No plugin needed: a cache copy is enough for the message being sent.
|
|
|
|
|
final dir = Directory(
|
|
|
|
|
'${Directory.systemTemp.path}/openchamber-attachments');
|
|
|
|
|
await dir.create(recursive: true);
|
|
|
|
|
final safe =
|
|
|
|
|
name.replaceAll(RegExp(r'[/\\:*?"<>|]'), '_');
|
|
|
|
|
final dst =
|
|
|
|
|
'${dir.path}/${DateTime.now().microsecondsSinceEpoch}-$safe';
|
|
|
|
|
await File(src).copy(dst);
|
|
|
|
|
return dst;
|
|
|
|
|
} catch (_) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-11-26 09:04:25 +08:00
|
|
|
}
|