|
| 1 | +use std::path::{Path, PathBuf}; |
| 2 | + |
| 3 | +use cap_project::RecordingMeta; |
| 4 | +use futures::StreamExt; |
| 5 | +use tauri::AppHandle; |
| 6 | +use tokio::fs; |
| 7 | + |
| 8 | +use crate::recordings_path; |
| 9 | + |
| 10 | +const STORE_KEY: &str = "uuid_projects_migrated"; |
| 11 | + |
| 12 | +pub fn migrate_if_needed(app: &AppHandle) -> Result<(), String> { |
| 13 | + use tauri_plugin_store::StoreExt; |
| 14 | + |
| 15 | + let store = app |
| 16 | + .store("store") |
| 17 | + .map_err(|e| format!("Failed to access store: {}", e))?; |
| 18 | + |
| 19 | + if store |
| 20 | + .get(STORE_KEY) |
| 21 | + .and_then(|v| v.as_bool()) |
| 22 | + .unwrap_or(false) |
| 23 | + { |
| 24 | + return Ok(()); |
| 25 | + } |
| 26 | + |
| 27 | + if let Err(err) = futures::executor::block_on(migrate(app)) { |
| 28 | + tracing::error!("Updating project names failed: {err}"); |
| 29 | + } |
| 30 | + |
| 31 | + store.set(STORE_KEY, true); |
| 32 | + store |
| 33 | + .save() |
| 34 | + .map_err(|e| format!("Failed to save store: {}", e))?; |
| 35 | + |
| 36 | + Ok(()) |
| 37 | +} |
| 38 | + |
| 39 | +/// Performs a one-time migration of all UUID-named projects to pretty name-based naming. |
| 40 | +pub async fn migrate(app: &AppHandle) -> Result<(), String> { |
| 41 | + let recordings_dir = recordings_path(app); |
| 42 | + if !fs::try_exists(&recordings_dir) |
| 43 | + .await |
| 44 | + .map_err(|e| format!("Failed to check recordings directory: {}", e))? |
| 45 | + { |
| 46 | + return Ok(()); |
| 47 | + } |
| 48 | + |
| 49 | + let uuid_projects = collect_uuid_projects(&recordings_dir).await?; |
| 50 | + if uuid_projects.is_empty() { |
| 51 | + tracing::debug!("No UUID-named projects found to migrate"); |
| 52 | + return Ok(()); |
| 53 | + } |
| 54 | + |
| 55 | + tracing::info!( |
| 56 | + "Found {} UUID-named projects to migrate", |
| 57 | + uuid_projects.len() |
| 58 | + ); |
| 59 | + |
| 60 | + let total_found = uuid_projects.len(); |
| 61 | + let concurrency_limit = std::thread::available_parallelism() |
| 62 | + .map(|n| n.get()) |
| 63 | + .unwrap_or(4) |
| 64 | + .max(2) |
| 65 | + .min(16) |
| 66 | + .min(total_found); |
| 67 | + tracing::debug!("Using concurrency limit of {}", concurrency_limit); |
| 68 | + |
| 69 | + let migration_results = futures::stream::iter(uuid_projects) |
| 70 | + .map(migrate_single_project) |
| 71 | + .buffer_unordered(concurrency_limit) |
| 72 | + .collect::<Vec<_>>() |
| 73 | + .await; |
| 74 | + |
| 75 | + // Aggregate results |
| 76 | + let mut migrated = 0; |
| 77 | + let mut skipped = 0; |
| 78 | + let mut failed = 0; |
| 79 | + |
| 80 | + for result in migration_results { |
| 81 | + match result { |
| 82 | + Ok(ProjectMigrationResult::Migrated) => migrated += 1, |
| 83 | + Ok(ProjectMigrationResult::Skipped) => skipped += 1, |
| 84 | + Err(_) => failed += 1, |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + tracing::info!( |
| 89 | + total_found = total_found, |
| 90 | + migrated = migrated, |
| 91 | + skipped = skipped, |
| 92 | + failed = failed, |
| 93 | + "Migration complete" |
| 94 | + ); |
| 95 | + |
| 96 | + Ok(()) |
| 97 | +} |
| 98 | + |
| 99 | +async fn collect_uuid_projects(recordings_dir: &Path) -> Result<Vec<PathBuf>, String> { |
| 100 | + let mut uuid_projects = Vec::new(); |
| 101 | + let mut entries = fs::read_dir(recordings_dir) |
| 102 | + .await |
| 103 | + .map_err(|e| format!("Failed to read recordings directory: {}", e))?; |
| 104 | + |
| 105 | + while let Some(entry) = entries |
| 106 | + .next_entry() |
| 107 | + .await |
| 108 | + .map_err(|e| format!("Failed to read directory entry: {}", e))? |
| 109 | + { |
| 110 | + let path = entry.path(); |
| 111 | + if !path.is_dir() { |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + let Some(filename) = path.file_name().and_then(|s| s.to_str()) else { |
| 116 | + continue; |
| 117 | + }; |
| 118 | + |
| 119 | + if filename.ends_with(".cap") && fast_is_project_filename_uuid(filename) { |
| 120 | + uuid_projects.push(path); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + Ok(uuid_projects) |
| 125 | +} |
| 126 | + |
| 127 | +#[derive(Debug)] |
| 128 | +enum ProjectMigrationResult { |
| 129 | + Migrated, |
| 130 | + Skipped, |
| 131 | +} |
| 132 | + |
| 133 | +async fn migrate_single_project(path: PathBuf) -> Result<ProjectMigrationResult, String> { |
| 134 | + let filename = path |
| 135 | + .file_name() |
| 136 | + .and_then(|s| s.to_str()) |
| 137 | + .unwrap_or("unknown"); |
| 138 | + |
| 139 | + let meta = match RecordingMeta::load_for_project(&path) { |
| 140 | + Ok(meta) => meta, |
| 141 | + Err(e) => { |
| 142 | + tracing::warn!("Failed to load metadata for {}: {}", filename, e); |
| 143 | + return Err(format!("Failed to load metadata: {}", e)); |
| 144 | + } |
| 145 | + }; |
| 146 | + |
| 147 | + match migrate_project_filename_async(&path, &meta).await { |
| 148 | + Ok(new_path) => { |
| 149 | + if new_path != path { |
| 150 | + let new_name = new_path.file_name().unwrap().to_string_lossy(); |
| 151 | + tracing::info!("Updated name: \"{}\" -> \"{}\"", filename, new_name); |
| 152 | + Ok(ProjectMigrationResult::Migrated) |
| 153 | + } else { |
| 154 | + Ok(ProjectMigrationResult::Skipped) |
| 155 | + } |
| 156 | + } |
| 157 | + Err(e) => { |
| 158 | + tracing::error!("Failed to migrate {}: {}", filename, e); |
| 159 | + Err(e) |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +/// Migrates a project filename from UUID to sanitized pretty name |
| 165 | +async fn migrate_project_filename_async( |
| 166 | + project_path: &Path, |
| 167 | + meta: &RecordingMeta, |
| 168 | +) -> Result<PathBuf, String> { |
| 169 | + let sanitized = sanitize_filename::sanitize(&meta.pretty_name.replace(":", ".")); |
| 170 | + |
| 171 | + let filename = if sanitized.ends_with(".cap") { |
| 172 | + sanitized |
| 173 | + } else { |
| 174 | + format!("{}.cap", sanitized) |
| 175 | + }; |
| 176 | + |
| 177 | + let parent_dir = project_path |
| 178 | + .parent() |
| 179 | + .ok_or("Project path has no parent directory")?; |
| 180 | + |
| 181 | + let unique_filename = cap_utils::ensure_unique_filename(&filename, parent_dir) |
| 182 | + .map_err(|e| format!("Failed to ensure unique filename: {}", e))?; |
| 183 | + |
| 184 | + let final_path = parent_dir.join(&unique_filename); |
| 185 | + |
| 186 | + fs::rename(project_path, &final_path) |
| 187 | + .await |
| 188 | + .map_err(|e| format!("Failed to rename project directory: {}", e))?; |
| 189 | + |
| 190 | + Ok(final_path) |
| 191 | +} |
| 192 | + |
| 193 | +pub fn fast_is_project_filename_uuid(filename: &str) -> bool { |
| 194 | + if filename.len() != 40 || !filename.ends_with(".cap") { |
| 195 | + return false; |
| 196 | + } |
| 197 | + |
| 198 | + let uuid_part = &filename[..36]; |
| 199 | + |
| 200 | + if uuid_part.as_bytes()[8] != b'-' |
| 201 | + || uuid_part.as_bytes()[13] != b'-' |
| 202 | + || uuid_part.as_bytes()[18] != b'-' |
| 203 | + || uuid_part.as_bytes()[23] != b'-' |
| 204 | + { |
| 205 | + return false; |
| 206 | + } |
| 207 | + |
| 208 | + uuid_part.chars().all(|c| c.is_ascii_hexdigit() || c == '-') |
| 209 | +} |
| 210 | + |
| 211 | +#[cfg(test)] |
| 212 | +mod tests { |
| 213 | + use super::*; |
| 214 | + |
| 215 | + #[test] |
| 216 | + fn test_is_project_filename_uuid() { |
| 217 | + // Valid UUID |
| 218 | + assert!(fast_is_project_filename_uuid( |
| 219 | + "a1b2c3d4-e5f6-7890-abcd-ef1234567890.cap" |
| 220 | + )); |
| 221 | + assert!(fast_is_project_filename_uuid( |
| 222 | + "00000000-0000-0000-0000-000000000000.cap" |
| 223 | + )); |
| 224 | + |
| 225 | + // Invalid cases |
| 226 | + assert!(!fast_is_project_filename_uuid("my-project-name.cap")); |
| 227 | + assert!(!fast_is_project_filename_uuid( |
| 228 | + "a1b2c3d4-e5f6-7890-abcd-ef1234567890" |
| 229 | + )); |
| 230 | + assert!(!fast_is_project_filename_uuid( |
| 231 | + "a1b2c3d4-e5f6-7890-abcd-ef1234567890.txt" |
| 232 | + )); |
| 233 | + assert!(!fast_is_project_filename_uuid( |
| 234 | + "g1b2c3d4-e5f6-7890-abcd-ef1234567890.cap" |
| 235 | + )); |
| 236 | + } |
| 237 | +} |
0 commit comments