diff --git a/app/lib/features/chat/domain/chat_attachment_picker.dart b/app/lib/features/chat/domain/chat_attachment_picker.dart index 2a41516b..344442c6 100644 --- a/app/lib/features/chat/domain/chat_attachment_picker.dart +++ b/app/lib/features/chat/domain/chat_attachment_picker.dart @@ -3,6 +3,7 @@ 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' + if (dart.library.io) 'chat_attachment_picker_native.dart' as impl; /// One file the operator chose in the host's file picker, with the bytes the diff --git a/app/lib/features/chat/domain/chat_attachment_picker_native.dart b/app/lib/features/chat/domain/chat_attachment_picker_native.dart new file mode 100644 index 00000000..42a14c48 --- /dev/null +++ b/app/lib/features/chat/domain/chat_attachment_picker_native.dart @@ -0,0 +1,47 @@ +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 _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?> _selectFiles() async { + final selected = await FilePicker.pickFiles(type: FileType.any); + if (selected.isEmpty) return null; + final files = []; + 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; +} diff --git a/app/pubspec.lock b/app/pubspec.lock index ed2a34ae..f2f3fca9 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -141,18 +141,18 @@ packages: dependency: "direct main" description: name: file_picker - sha256: "98c0b156b6380ba55bc767a14e2e6b3feb246e713ad40092424596fa79005bea" + sha256: e67f1bcbef00f8b28f3b6ce09a1f0d2d9de37cf1e1e7e73b70df7a20d3eee8be url: "https://pub.dev" source: hosted - version: "13.1.0" + version: "13.3.0" file_picker_darwin: dependency: transitive description: name: file_picker_darwin - sha256: ca2918264c453400c80ef0d69ca3f8b816f894db6144bebac73064ad64927e0e + sha256: "13e731433fb3e59c1aff5a1197e41c5c5f930fad16342d101579086271a70d7b" url: "https://pub.dev" source: hosted - version: "2.3.3" + version: "2.4.0" file_picker_linux: dependency: transitive description: diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 99ad2420..f41f9a93 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -46,7 +46,7 @@ dependencies: path_provider: ^2.1.6 open_filex: ^5.0.0 share_plus: ^13.3.1 - file_picker: ^13.1.0 + file_picker: ^13.3.0 url_launcher: ^6.3.3 dev_dependencies: