generated from dellevin/template
代码优化,epub阅读标签
This commit is contained in:
@@ -105,35 +105,54 @@ class AutoBackupService {
|
||||
/// 获取备份目录(下载目录/mooknote)
|
||||
Future<Directory?> _getBackupDirectory() async {
|
||||
try {
|
||||
// 尝试获取下载目录
|
||||
Directory? downloadDir;
|
||||
|
||||
if (Platform.isAndroid) {
|
||||
// Android: 使用外部存储的下载目录
|
||||
// 优先级 1: 官方 API 获取下载目录
|
||||
try {
|
||||
final dirs = await getExternalStorageDirectories(
|
||||
type: StorageDirectory.downloads,
|
||||
);
|
||||
if (dirs != null && dirs.isNotEmpty) {
|
||||
return Directory('${dirs.first.path}/$_backupDirName');
|
||||
}
|
||||
} catch (_) {}
|
||||
|
||||
// 优先级 2: 标准路径直接拼
|
||||
final standardPath = '/storage/emulated/0/Download/$_backupDirName';
|
||||
final standardDir = Directory(standardPath);
|
||||
if (await standardDir.parent.exists()) {
|
||||
return standardDir;
|
||||
}
|
||||
|
||||
// 优先级 3: 从外部存储路径推导(旧逻辑兜底)
|
||||
final externalDir = await getExternalStorageDirectory();
|
||||
if (externalDir != null) {
|
||||
// 通常路径是 /storage/emulated/0/Android/data/.../files
|
||||
// 我们需要找到真正的下载目录
|
||||
final path = externalDir.path;
|
||||
final downloadPath = path.replaceAll(
|
||||
'/Android/data/${externalDir.uri.pathSegments[externalDir.uri.pathSegments.length - 3]}/files',
|
||||
'/Download',
|
||||
);
|
||||
downloadDir = Directory('$downloadPath/$_backupDirName');
|
||||
final segments = externalDir.uri.pathSegments;
|
||||
if (segments.length >= 3) {
|
||||
final pkg = segments[segments.length - 3];
|
||||
final downloadPath = externalDir.path.replaceAll(
|
||||
'/Android/data/$pkg/files',
|
||||
'/Download',
|
||||
);
|
||||
return Directory('$downloadPath/$_backupDirName');
|
||||
}
|
||||
}
|
||||
|
||||
// 优先级 4: 降级到 app 内部目录
|
||||
final appDir = await getApplicationDocumentsDirectory();
|
||||
return Directory('${appDir.path}/$_backupDirName');
|
||||
|
||||
} else if (Platform.isIOS) {
|
||||
// iOS: 使用文档目录
|
||||
final docDir = await getApplicationDocumentsDirectory();
|
||||
downloadDir = Directory('${docDir.path}/$_backupDirName');
|
||||
return Directory('${docDir.path}/$_backupDirName');
|
||||
} else {
|
||||
// 桌面端: 使用下载目录
|
||||
// 桌面端
|
||||
final home = Platform.environment['HOME'] ?? Platform.environment['USERPROFILE'];
|
||||
if (home != null) {
|
||||
downloadDir = Directory('$home/Downloads/$_backupDirName');
|
||||
return Directory('$home/Downloads/$_backupDirName');
|
||||
}
|
||||
final docDir = await getApplicationDocumentsDirectory();
|
||||
return Directory('${docDir.path}/$_backupDirName');
|
||||
}
|
||||
|
||||
return downloadDir;
|
||||
} catch (e) {
|
||||
debugPrint('AutoBackup: 获取备份目录失败 - $e');
|
||||
return null;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:archive/archive.dart';
|
||||
import 'package:archive/archive_io.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
@@ -89,57 +88,73 @@ class BackupService {
|
||||
},
|
||||
};
|
||||
|
||||
// 创建 ZIP
|
||||
final archive = Archive();
|
||||
final jsonString = const JsonEncoder.withIndent(' ').convert(backupData);
|
||||
final jsonBytes = Uint8List.fromList(utf8.encode(jsonString));
|
||||
archive.addFile(ArchiveFile('data.json', jsonBytes.length, jsonBytes));
|
||||
// 创建 ZIP(逐文件写入磁盘,避免全部加载到内存)
|
||||
final tempDir = await getTemporaryDirectory();
|
||||
final tempZipPath = path.join(tempDir.path, 'mooknote_backup_temp.zip');
|
||||
final encoder = ZipFileEncoder();
|
||||
encoder.create(tempZipPath);
|
||||
|
||||
int imageCount = 0;
|
||||
final appDir = await getApplicationDocumentsDirectory();
|
||||
final imagesRoot = path.join(appDir.path, 'images');
|
||||
try {
|
||||
// data.json
|
||||
final jsonString = const JsonEncoder.withIndent(' ').convert(backupData);
|
||||
final jsonBytes = Uint8List.fromList(utf8.encode(jsonString));
|
||||
final dataFile = File(path.join(tempDir.path, 'mooknote_data.json'));
|
||||
await dataFile.writeAsBytes(jsonBytes);
|
||||
encoder.addFile(dataFile, 'data.json');
|
||||
await dataFile.delete();
|
||||
|
||||
for (final imagePath in imagePaths) {
|
||||
final file = File(imagePath);
|
||||
if (await file.exists()) {
|
||||
final bytes = await file.readAsBytes();
|
||||
String relativePath;
|
||||
if (imagePath.startsWith(imagesRoot)) {
|
||||
relativePath = imagePath.substring(imagesRoot.length + 1);
|
||||
} else {
|
||||
relativePath = path.basename(imagePath);
|
||||
}
|
||||
archive.addFile(ArchiveFile('images/$relativePath', bytes.length, bytes));
|
||||
imageCount++;
|
||||
}
|
||||
}
|
||||
int imageCount = 0;
|
||||
final appDir = await getApplicationDocumentsDirectory();
|
||||
final imagesRoot = path.join(appDir.path, 'images');
|
||||
|
||||
// 收集 epub_books 目录下的 epub 文件
|
||||
int epubCount = 0;
|
||||
final epubRoot = path.join(appDir.path, 'epub_books');
|
||||
final epubDir = Directory(epubRoot);
|
||||
if (await epubDir.exists()) {
|
||||
await for (final entity in epubDir.list(recursive: true)) {
|
||||
if (entity is File) {
|
||||
final bytes = await entity.readAsBytes();
|
||||
final relativePath = entity.path.substring(epubRoot.length + 1);
|
||||
archive.addFile(ArchiveFile('epub_books/$relativePath', bytes.length, bytes));
|
||||
epubCount++;
|
||||
for (final imagePath in imagePaths) {
|
||||
final file = File(imagePath);
|
||||
if (await file.exists()) {
|
||||
String relativePath;
|
||||
if (imagePath.startsWith(imagesRoot)) {
|
||||
relativePath = imagePath.substring(imagesRoot.length + 1);
|
||||
} else {
|
||||
relativePath = path.basename(imagePath);
|
||||
}
|
||||
encoder.addFile(file, 'images/$relativePath');
|
||||
imageCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// 收集 epub_books 目录下的 epub 文件
|
||||
int epubCount = 0;
|
||||
final epubRoot = path.join(appDir.path, 'epub_books');
|
||||
final epubDir = Directory(epubRoot);
|
||||
if (await epubDir.exists()) {
|
||||
await for (final entity in epubDir.list(recursive: true)) {
|
||||
if (entity is File) {
|
||||
final relativePath = entity.path.substring(epubRoot.length + 1);
|
||||
encoder.addFile(entity, 'epub_books/$relativePath');
|
||||
epubCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
encoder.close();
|
||||
|
||||
// 读取最终 zip 文件
|
||||
final zipFile = File(tempZipPath);
|
||||
final zipBytes = await zipFile.readAsBytes();
|
||||
await zipFile.delete();
|
||||
|
||||
return _ExportData(
|
||||
zipBytes: Uint8List.fromList(zipBytes),
|
||||
movieCount: movies.length,
|
||||
bookCount: books.length,
|
||||
noteCount: notes.length,
|
||||
imageCount: imageCount,
|
||||
epubCount: epubCount,
|
||||
);
|
||||
} catch (e) {
|
||||
encoder.close();
|
||||
try { await File(tempZipPath).delete(); } catch (_) {}
|
||||
rethrow;
|
||||
}
|
||||
|
||||
final zipBytes = ZipEncoder().encode(archive);
|
||||
if (zipBytes == null) throw Exception('压缩备份文件失败');
|
||||
|
||||
return _ExportData(
|
||||
zipBytes: Uint8List.fromList(zipBytes),
|
||||
movieCount: movies.length,
|
||||
bookCount: books.length,
|
||||
noteCount: notes.length,
|
||||
imageCount: imageCount,
|
||||
epubCount: epubCount,
|
||||
);
|
||||
}
|
||||
|
||||
// ─── 手动导出 ─────────────────────────────────────────
|
||||
|
||||
@@ -311,11 +311,16 @@ class WebDAVService {
|
||||
}
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_lastSyncKey, DateTime.now().toIso8601String());
|
||||
|
||||
// 仅在上传成功或下载成功时记录同步时间
|
||||
final bool anySuccess = uploadedFiles > 0 || downloadedFiles > 0;
|
||||
if (anySuccess) {
|
||||
await prefs.setString(_lastSyncKey, DateTime.now().toIso8601String());
|
||||
}
|
||||
|
||||
return SyncResult(
|
||||
success: true,
|
||||
message: '同步完成',
|
||||
success: anySuccess,
|
||||
message: anySuccess ? '同步完成' : '同步未完成,未传输任何数据',
|
||||
lastSyncTime: DateTime.now(),
|
||||
uploadedFiles: uploadedFiles,
|
||||
downloadedFiles: downloadedFiles,
|
||||
|
||||
Reference in New Issue
Block a user