feat: match gsmol's v2 encrypted export format; fix export on desktop

This commit is contained in:
randogoth 2026-09-27 00:09:29 +03:00
parent f80315e7c4
commit 7d3c8b423c
3 changed files with 104 additions and 28 deletions

View file

@ -68,19 +68,36 @@ class _SettingsScreenState extends ConsumerState<SettingsScreen> {
// it is picked up (SPEC.md §5). This backs up inbox, sent mail, contacts and
// pinned servers into one shareable file; never the seed.
Future<void> _export() async {
final data = ref.read(storeProvider).exportData();
final store = ref.read(storeProvider);
final data = store.exportData();
final stamp = DateTime.now().toIso8601String().substring(0, 10);
final file = File("${Directory.systemTemp.path}/smolmail-export-$stamp.json");
await file.writeAsString(jsonEncode(data));
await SharePlus.instance.share(ShareParams(
files: [XFile(file.path)], text: "kirakira backup $stamp"));
if (mounted) {
final messages = (data["inbox"] as List).length + (data["sent"] as List).length;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("exported $messages messages, "
"${(data["contacts"] as Map).length} contacts, "
"${(data["servers"] as Map).length} server keys")),
);
final fileName = "kirakira-backup-$stamp.json";
final bytes = Uint8List.fromList(utf8.encode(jsonEncode(data)));
try {
// share_plus has no file-sharing implementation on desktop platforms —
// it throws UnimplementedError for Linux/Windows/macOS — so those save
// straight to a chosen location instead of going through a share sheet.
if (Platform.isLinux || Platform.isWindows || Platform.isMacOS) {
await FilePicker.saveFile(fileName: fileName, bytes: bytes);
} else {
final file = File("${Directory.systemTemp.path}/$fileName");
await file.writeAsBytes(bytes);
await SharePlus.instance.share(ShareParams(
files: [XFile(file.path)], text: "kirakira backup $stamp"));
}
if (mounted) {
// The export payload is sealed now (v2), so the counts for this
// notice come straight from the store rather than the ciphertext.
final messages =
store.listMessages("inbox").length + store.listMessages("sent").length;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("exported $messages messages, "
"${store.allContacts().length} contacts, "
"${store.allPins().length} server keys")),
);
}
} catch (err) {
if (mounted) showErrorSnackBar(context, err.toString());
}
}