|
| 1 | +use std::fs; |
| 2 | +use std::path::Path; |
| 3 | +use std::time::SystemTime; |
| 4 | + |
| 5 | +use reina_path::{get_base_data_dir, get_base_data_dir_for_mode}; |
| 6 | + |
| 7 | +use crate::utils::fs::move_file; |
| 8 | + |
| 9 | +#[derive(Debug, Default)] |
| 10 | +pub struct StartupMigrationResult { |
| 11 | + pub migrated_files: usize, |
| 12 | + pub replaced_files: usize, |
| 13 | + pub removed_legacy_files: usize, |
| 14 | + pub skipped: usize, |
| 15 | + pub executed: usize, |
| 16 | +} |
| 17 | + |
| 18 | +pub fn run_startup_migrations() -> Result<StartupMigrationResult, String> { |
| 19 | + let mut result = StartupMigrationResult::default(); |
| 20 | + |
| 21 | + run_startup_migration(&mut result, m20260326_000001_migrate_legacy_covers)?; |
| 22 | + |
| 23 | + Ok(result) |
| 24 | +} |
| 25 | + |
| 26 | +fn run_startup_migration( |
| 27 | + aggregate: &mut StartupMigrationResult, |
| 28 | + migration: fn() -> Result<StartupMigrationResult, String>, |
| 29 | +) -> Result<(), String> { |
| 30 | + let result = migration()?; |
| 31 | + |
| 32 | + aggregate.migrated_files += result.migrated_files; |
| 33 | + aggregate.replaced_files += result.replaced_files; |
| 34 | + aggregate.removed_legacy_files += result.removed_legacy_files; |
| 35 | + aggregate.skipped += result.skipped; |
| 36 | + aggregate.executed += result.executed; |
| 37 | + |
| 38 | + Ok(()) |
| 39 | +} |
| 40 | + |
| 41 | +fn m20260326_000001_migrate_legacy_covers() -> Result<StartupMigrationResult, String> { |
| 42 | + let legacy_covers_dir = get_base_data_dir_for_mode(true)?.join("covers"); |
| 43 | + let current_covers_dir = get_base_data_dir()?.join("covers"); |
| 44 | + |
| 45 | + if current_covers_dir == legacy_covers_dir || !legacy_covers_dir.exists() { |
| 46 | + return Ok(StartupMigrationResult { |
| 47 | + skipped: 1, |
| 48 | + ..Default::default() |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + if !legacy_covers_dir.is_dir() { |
| 53 | + return Err(format!( |
| 54 | + "旧版 covers 路径不是目录: {}", |
| 55 | + legacy_covers_dir.display() |
| 56 | + )); |
| 57 | + } |
| 58 | + |
| 59 | + fs::create_dir_all(¤t_covers_dir).map_err(|e| { |
| 60 | + format!( |
| 61 | + "无法创建新的 covers 目录 {}: {}", |
| 62 | + current_covers_dir.display(), |
| 63 | + e |
| 64 | + ) |
| 65 | + })?; |
| 66 | + |
| 67 | + let mut result = StartupMigrationResult { |
| 68 | + executed: 1, |
| 69 | + ..Default::default() |
| 70 | + }; |
| 71 | + |
| 72 | + merge_covers_dir(&legacy_covers_dir, ¤t_covers_dir, &mut result)?; |
| 73 | + remove_dir_if_empty(&legacy_covers_dir)?; |
| 74 | + |
| 75 | + Ok(result) |
| 76 | +} |
| 77 | + |
| 78 | +fn merge_covers_dir( |
| 79 | + from_dir: &Path, |
| 80 | + to_dir: &Path, |
| 81 | + result: &mut StartupMigrationResult, |
| 82 | +) -> Result<(), String> { |
| 83 | + for entry in fs::read_dir(from_dir) |
| 84 | + .map_err(|e| format!("读取 legacy covers 目录失败 {}: {}", from_dir.display(), e))? |
| 85 | + { |
| 86 | + let entry = entry.map_err(|e| format!("读取 legacy covers 项失败: {}", e))?; |
| 87 | + let from_path = entry.path(); |
| 88 | + let to_path = to_dir.join(entry.file_name()); |
| 89 | + |
| 90 | + if from_path.is_dir() { |
| 91 | + fs::create_dir_all(&to_path) |
| 92 | + .map_err(|e| format!("创建目标目录失败 {}: {}", to_path.display(), e))?; |
| 93 | + merge_covers_dir(&from_path, &to_path, result)?; |
| 94 | + remove_dir_if_empty(&from_path)?; |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + if !from_path.is_file() { |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + if !to_path.exists() { |
| 103 | + move_file(&from_path, &to_path)?; |
| 104 | + result.migrated_files += 1; |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + if !to_path.is_file() { |
| 109 | + return Err(format!( |
| 110 | + "目标 covers 路径已存在且不是文件: {}", |
| 111 | + to_path.display() |
| 112 | + )); |
| 113 | + } |
| 114 | + |
| 115 | + let from_modified = file_modified_time(&from_path)?; |
| 116 | + let to_modified = file_modified_time(&to_path)?; |
| 117 | + |
| 118 | + if from_modified > to_modified { |
| 119 | + fs::remove_file(&to_path) |
| 120 | + .map_err(|e| format!("删除旧目标文件失败 {}: {}", to_path.display(), e))?; |
| 121 | + move_file(&from_path, &to_path)?; |
| 122 | + result.migrated_files += 1; |
| 123 | + result.replaced_files += 1; |
| 124 | + } else { |
| 125 | + fs::remove_file(&from_path) |
| 126 | + .map_err(|e| format!("删除 legacy 重复文件失败 {}: {}", from_path.display(), e))?; |
| 127 | + result.removed_legacy_files += 1; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + Ok(()) |
| 132 | +} |
| 133 | + |
| 134 | +fn file_modified_time(path: &Path) -> Result<SystemTime, String> { |
| 135 | + fs::metadata(path) |
| 136 | + .and_then(|metadata| metadata.modified()) |
| 137 | + .map_err(|e| format!("读取文件修改时间失败 {}: {}", path.display(), e)) |
| 138 | +} |
| 139 | + |
| 140 | +fn remove_dir_if_empty(path: &Path) -> Result<(), String> { |
| 141 | + if !path.exists() || !path.is_dir() { |
| 142 | + return Ok(()); |
| 143 | + } |
| 144 | + |
| 145 | + let mut entries = fs::read_dir(path) |
| 146 | + .map_err(|e| format!("检查目录是否为空失败 {}: {}", path.display(), e))?; |
| 147 | + |
| 148 | + if entries.next().is_none() { |
| 149 | + fs::remove_dir(path).map_err(|e| format!("删除空目录失败 {}: {}", path.display(), e))?; |
| 150 | + } |
| 151 | + |
| 152 | + Ok(()) |
| 153 | +} |
0 commit comments