新增本地自动备份

This commit is contained in:
DelLevin-Home
2026-03-08 13:34:57 +08:00
parent bb7b2c0191
commit c894921472
7 changed files with 744 additions and 174 deletions

View File

@@ -0,0 +1,216 @@
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as path;
import 'package:shared_preferences/shared_preferences.dart';
import 'backup_service.dart';
/// 自动备份服务 - 定时自动备份到下载目录
class AutoBackupService {
static final AutoBackupService instance = AutoBackupService._init();
AutoBackupService._init();
Timer? _timer;
bool _isRunning = false;
static const String _prefsKey = 'auto_backup_enabled';
static const String _backupDirName = 'mooknote';
static const int _maxBackups = 10;
static const Duration _backupInterval = Duration(minutes: 2);
/// 是否正在运行
bool get isRunning => _isRunning;
/// 获取自动备份状态
Future<bool> getEnabled() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getBool(_prefsKey) ?? false;
}
/// 设置自动备份状态
Future<void> setEnabled(bool enabled) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_prefsKey, enabled);
if (enabled) {
await start();
} else {
await stop();
}
}
/// 启动自动备份
Future<void> start() async {
if (_isRunning) return;
// 立即执行一次备份
await _performBackup();
// 启动定时器
_timer = Timer.periodic(_backupInterval, (_) async {
await _performBackup();
});
_isRunning = true;
}
/// 停止自动备份
Future<void> stop() async {
_timer?.cancel();
_timer = null;
_isRunning = false;
}
/// 执行备份
Future<void> _performBackup() async {
try {
final backupDir = await _getBackupDirectory();
if (backupDir == null) {
print('AutoBackup: 无法获取备份目录');
return;
}
// 确保备份目录存在
if (!await backupDir.exists()) {
await backupDir.create(recursive: true);
}
// 导出数据
final result = await BackupService.instance.exportDataForAutoBackup();
if (!result.success) {
print('AutoBackup: 导出失败 - ${result.errorMessage}');
return;
}
// 生成备份文件名
final fileName = 'auto_backup_${_formatDateTime(DateTime.now())}.zip';
final backupFile = File(path.join(backupDir.path, fileName));
// 写入备份文件
await backupFile.writeAsBytes(result.zipBytes!);
print('AutoBackup: 备份成功 - ${backupFile.path}');
// 清理旧备份只保留最新的10个
await _cleanupOldBackups(backupDir);
} catch (e) {
print('AutoBackup: 备份失败 - $e');
}
}
/// 获取备份目录(下载目录/mooknote
Future<Directory?> _getBackupDirectory() async {
try {
// 尝试获取下载目录
Directory? downloadDir;
if (Platform.isAndroid) {
// Android: 使用外部存储的下载目录
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');
}
} else if (Platform.isIOS) {
// iOS: 使用文档目录
final docDir = await getApplicationDocumentsDirectory();
downloadDir = Directory('${docDir.path}/$_backupDirName');
} else {
// 桌面端: 使用下载目录
final home = Platform.environment['HOME'] ?? Platform.environment['USERPROFILE'];
if (home != null) {
downloadDir = Directory('$home/Downloads/$_backupDirName');
}
}
return downloadDir;
} catch (e) {
print('AutoBackup: 获取备份目录失败 - $e');
return null;
}
}
/// 清理旧备份只保留最新的10个
Future<void> _cleanupOldBackups(Directory backupDir) async {
try {
final files = await backupDir
.list()
.where((entity) => entity is File && entity.path.endsWith('.zip'))
.cast<File>()
.toList();
// 按修改时间排序(最新的在前)
files.sort((a, b) {
final aStat = a.statSync();
final bStat = b.statSync();
return bStat.modified.compareTo(aStat.modified);
});
// 删除超过10个的旧备份
if (files.length > _maxBackups) {
for (var i = _maxBackups; i < files.length; i++) {
try {
await files[i].delete();
print('AutoBackup: 删除旧备份 - ${files[i].path}');
} catch (e) {
print('AutoBackup: 删除旧备份失败 - $e');
}
}
}
} catch (e) {
print('AutoBackup: 清理旧备份失败 - $e');
}
}
/// 获取备份文件列表
Future<List<File>> getBackupFiles() async {
try {
final backupDir = await _getBackupDirectory();
if (backupDir == null || !await backupDir.exists()) {
return [];
}
final files = await backupDir
.list()
.where((entity) => entity is File && entity.path.endsWith('.zip'))
.cast<File>()
.toList();
// 按修改时间排序(最新的在前)
files.sort((a, b) {
final aStat = a.statSync();
final bStat = b.statSync();
return bStat.modified.compareTo(aStat.modified);
});
return files;
} catch (e) {
print('AutoBackup: 获取备份列表失败 - $e');
return [];
}
}
/// 获取备份目录路径
Future<String?> getBackupDirectoryPath() async {
final dir = await _getBackupDirectory();
return dir?.path;
}
/// 格式化日期时间用于文件名
String _formatDateTime(DateTime dateTime) {
return '${dateTime.year}${_pad(dateTime.month)}${_pad(dateTime.day)}_${_pad(dateTime.hour)}${_pad(dateTime.minute)}${_pad(dateTime.second)}';
}
String _pad(int number) {
return number.toString().padLeft(2, '0');
}
}

View File

@@ -182,6 +182,126 @@ class BackupService {
);
}
/// 导出数据用于自动备份(返回字节数据而不是保存到文件)
Future<AutoBackupExportResult> exportDataForAutoBackup() async {
try {
final db = await DatabaseHelper.instance.database;
// 导出所有表的数据
final movies = await db.query('movies');
final books = await db.query('books');
final notes = await db.query('notes');
final movieReviews = await db.query('movie_reviews');
final moviePosters = await db.query('movie_posters');
// 收集所有图片路径
final imagePaths = <String>{};
// 收集影视海报
for (final movie in movies) {
final posterPath = movie['poster_path'] as String?;
if (posterPath != null && posterPath.isNotEmpty) {
imagePaths.add(posterPath);
}
}
// 收集书籍封面
for (final book in books) {
final coverPath = book['cover_path'] as String?;
if (coverPath != null && coverPath.isNotEmpty) {
imagePaths.add(coverPath);
}
}
// 收集海报墙图片
for (final poster in moviePosters) {
final posterPath = poster['poster_path'] as String?;
if (posterPath != null && posterPath.isNotEmpty) {
imagePaths.add(posterPath);
}
}
// 收集笔记图片
for (final note in notes) {
final imagesJson = note['images'] as String?;
if (imagesJson != null && imagesJson.isNotEmpty) {
try {
final images = jsonDecode(imagesJson) as List<dynamic>;
for (final imagePath in images) {
if (imagePath is String && imagePath.isNotEmpty) {
imagePaths.add(imagePath);
}
}
} catch (e) {
// 解析失败,跳过
}
}
}
// 构建备份数据
final backupData = {
'version': 2,
'exportTime': DateTime.now().toIso8601String(),
'appName': 'MookNote',
'hasImages': true,
'data': {
'movies': movies,
'books': books,
'notes': notes,
'movie_reviews': movieReviews,
'movie_posters': moviePosters,
},
};
// 创建 ZIP 文件
final archive = Archive();
// 添加 JSON 数据
final jsonString = const JsonEncoder.withIndent(' ').convert(backupData);
final jsonBytes = Uint8List.fromList(utf8.encode(jsonString));
archive.addFile(ArchiveFile('data.json', jsonBytes.length, jsonBytes));
// 添加图片文件,保持目录结构
int imageCount = 0;
final appDir = await getApplicationDocumentsDirectory();
final imagesRoot = path.join(appDir.path, 'images');
for (final imagePath in imagePaths) {
final file = File(imagePath);
if (await file.exists()) {
final bytes = await file.readAsBytes();
// 计算相对路径(如 movies/1/poster.jpg
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++;
}
}
// 压缩 ZIP
final zipEncoder = ZipEncoder();
final zipBytes = zipEncoder.encode(archive);
if (zipBytes == null) {
return AutoBackupExportResult.error('压缩备份文件失败');
}
return AutoBackupExportResult.success(
zipBytes: Uint8List.fromList(zipBytes),
movieCount: movies.length,
bookCount: books.length,
noteCount: notes.length,
imageCount: imageCount,
);
} catch (e) {
return AutoBackupExportResult.error('导出失败: $e');
}
}
/// 选择并导入备份文件(支持 ZIP 格式)
Future<ImportResult> importData() async {
try {
@@ -450,6 +570,48 @@ class BackupService {
}
}
/// 自动备份导出结果
class AutoBackupExportResult {
final bool success;
final String? errorMessage;
final Uint8List? zipBytes;
final int movieCount;
final int bookCount;
final int noteCount;
final int imageCount;
AutoBackupExportResult._({
required this.success,
this.errorMessage,
this.zipBytes,
this.movieCount = 0,
this.bookCount = 0,
this.noteCount = 0,
this.imageCount = 0,
});
factory AutoBackupExportResult.success({
required Uint8List zipBytes,
required int movieCount,
required int bookCount,
required int noteCount,
required int imageCount,
}) {
return AutoBackupExportResult._(
success: true,
zipBytes: zipBytes,
movieCount: movieCount,
bookCount: bookCount,
noteCount: noteCount,
imageCount: imageCount,
);
}
factory AutoBackupExportResult.error(String message) {
return AutoBackupExportResult._(success: false, errorMessage: message);
}
}
/// 导出结果
class ExportResult {
final bool success;