48 lines
1.5 KiB
Dart
48 lines
1.5 KiB
Dart
import 'package:file_picker/file_picker.dart';
|
|
|
|
import 'chat_attachment_picker.dart';
|
|
|
|
/// The native (Android/iOS) file picker.
|
|
///
|
|
/// It uses the platform's own document picker through `file_picker`, reads the
|
|
/// bytes with the selection so the draft owns one copy, and records the file's
|
|
/// name and a MIME type derived from its extension. A cancelled selection
|
|
/// returns null, exactly like the web picker.
|
|
ChatAttachmentPicker get hostChatAttachmentPicker =>
|
|
const ChatAttachmentPicker(select: _selectFiles);
|
|
|
|
const Map<String, String> _mimeByExtension = {
|
|
'png': 'image/png',
|
|
'jpg': 'image/jpeg',
|
|
'jpeg': 'image/jpeg',
|
|
'gif': 'image/gif',
|
|
'webp': 'image/webp',
|
|
'heic': 'image/heic',
|
|
'pdf': 'application/pdf',
|
|
'txt': 'text/plain',
|
|
'md': 'text/markdown',
|
|
'json': 'application/json',
|
|
'csv': 'text/csv',
|
|
'html': 'text/html',
|
|
'zip': 'application/zip',
|
|
};
|
|
|
|
String _mimeFor(String name) {
|
|
final dot = name.lastIndexOf('.');
|
|
if (dot < 0 || dot == name.length - 1) return '';
|
|
return _mimeByExtension[name.substring(dot + 1).toLowerCase()] ?? '';
|
|
}
|
|
|
|
Future<List<ChatPickedFile>?> _selectFiles() async {
|
|
final selected = await FilePicker.pickFiles(type: FileType.any);
|
|
if (selected.isEmpty) return null;
|
|
final files = <ChatPickedFile>[];
|
|
for (final file in selected) {
|
|
final bytes = await file.xFile.readAsBytes();
|
|
files.add(
|
|
ChatPickedFile(name: file.name, mime: _mimeFor(file.name), bytes: bytes),
|
|
);
|
|
}
|
|
return files;
|
|
}
|