|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | +use std::collections::HashMap; |
| 3 | +use std::fs; |
| 4 | + |
| 5 | +use crate::playlists::{self, Playlist}; |
| 6 | +use crate::scan::scan_dir; |
| 7 | + |
| 8 | +#[derive(Serialize, Deserialize, Clone)] |
| 9 | +pub struct ExportTrack { |
| 10 | + pub title: String, |
| 11 | + pub artist: String, |
| 12 | + pub album: String, |
| 13 | + pub duration: f64, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Serialize, Deserialize, Clone)] |
| 17 | +pub struct ExportPlaylist { |
| 18 | + pub name: String, |
| 19 | + pub tracks: Vec<ExportTrack>, |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Serialize, Deserialize)] |
| 23 | +pub struct AccountExport { |
| 24 | + pub version: u32, |
| 25 | + pub tracks: Vec<ExportTrack>, |
| 26 | + pub playlists: Vec<ExportPlaylist>, |
| 27 | + pub favorites: Vec<ExportTrack>, |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Serialize)] |
| 31 | +pub struct ImportResult { |
| 32 | + pub playlists_created: usize, |
| 33 | + pub favorites_restored: usize, |
| 34 | + pub tracks_already_present: usize, |
| 35 | + pub tracks_to_download: Vec<ExportTrack>, |
| 36 | +} |
| 37 | + |
| 38 | +fn norm_key(title: &str, artist: &str) -> String { |
| 39 | + format!( |
| 40 | + "{}::{}", |
| 41 | + title.trim().to_lowercase(), |
| 42 | + artist.trim().to_lowercase() |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +#[tauri::command] |
| 47 | +pub async fn export_account(dir: String) -> Result<String, String> { |
| 48 | + let lib = scan_dir(dir); |
| 49 | + let path_to_meta: HashMap<String, ExportTrack> = lib |
| 50 | + .iter() |
| 51 | + .map(|t| { |
| 52 | + ( |
| 53 | + t.path.clone(), |
| 54 | + ExportTrack { |
| 55 | + title: t.title.clone(), |
| 56 | + artist: t.artist.clone(), |
| 57 | + album: t.album.clone(), |
| 58 | + duration: t.duration, |
| 59 | + }, |
| 60 | + ) |
| 61 | + }) |
| 62 | + .collect(); |
| 63 | + |
| 64 | + let tracks: Vec<ExportTrack> = lib |
| 65 | + .iter() |
| 66 | + .map(|t| ExportTrack { |
| 67 | + title: t.title.clone(), |
| 68 | + artist: t.artist.clone(), |
| 69 | + album: t.album.clone(), |
| 70 | + duration: t.duration, |
| 71 | + }) |
| 72 | + .collect(); |
| 73 | + |
| 74 | + let pl_dir = playlists::pl_dir(); |
| 75 | + let mut pls = Vec::new(); |
| 76 | + let entries = fs::read_dir(&pl_dir).map_err(|e| format!("readdir: {e}"))?; |
| 77 | + for entry in entries.flatten() { |
| 78 | + let path = entry.path(); |
| 79 | + if path.extension().map_or(false, |e| e == "json") { |
| 80 | + if let Ok(data) = fs::read_to_string(&path) { |
| 81 | + if let Ok(pl) = serde_json::from_str::<Playlist>(&data) { |
| 82 | + if pl.id != playlists::FAVS_ID { |
| 83 | + let pl_tracks: Vec<ExportTrack> = pl |
| 84 | + .tracks |
| 85 | + .iter() |
| 86 | + .filter_map(|p| path_to_meta.get(p).cloned()) |
| 87 | + .collect(); |
| 88 | + pls.push(ExportPlaylist { |
| 89 | + name: pl.name, |
| 90 | + tracks: pl_tracks, |
| 91 | + }); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + playlists::ensure_favs(); |
| 99 | + let favs_pl = playlists::read_pl(playlists::FAVS_ID)?; |
| 100 | + let favorites: Vec<ExportTrack> = favs_pl |
| 101 | + .tracks |
| 102 | + .iter() |
| 103 | + .filter_map(|p| path_to_meta.get(p).cloned()) |
| 104 | + .collect(); |
| 105 | + |
| 106 | + let export = AccountExport { |
| 107 | + version: 1, |
| 108 | + tracks, |
| 109 | + playlists: pls, |
| 110 | + favorites, |
| 111 | + }; |
| 112 | + |
| 113 | + serde_json::to_string_pretty(&export).map_err(|e| format!("json: {e}")) |
| 114 | +} |
| 115 | + |
| 116 | +#[tauri::command] |
| 117 | +pub async fn import_account(data: String, dir: String) -> Result<ImportResult, String> { |
| 118 | + let export: AccountExport = |
| 119 | + serde_json::from_str(&data).map_err(|e| format!("parse: {e}"))?; |
| 120 | + |
| 121 | + let lib = scan_dir(dir.clone()); |
| 122 | + let lib_keys: HashMap<String, String> = lib |
| 123 | + .iter() |
| 124 | + .map(|t| (norm_key(&t.title, &t.artist), t.path.clone())) |
| 125 | + .collect(); |
| 126 | + |
| 127 | + let mut present = 0usize; |
| 128 | + let mut missing: Vec<ExportTrack> = Vec::new(); |
| 129 | + |
| 130 | + for t in &export.tracks { |
| 131 | + let key = norm_key(&t.title, &t.artist); |
| 132 | + if lib_keys.contains_key(&key) { |
| 133 | + present += 1; |
| 134 | + } else { |
| 135 | + missing.push(t.clone()); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + let mut pl_count = 0usize; |
| 140 | + for ep in &export.playlists { |
| 141 | + let id = format!( |
| 142 | + "pl_{}", |
| 143 | + std::time::SystemTime::now() |
| 144 | + .duration_since(std::time::UNIX_EPOCH) |
| 145 | + .unwrap_or_default() |
| 146 | + .as_millis() |
| 147 | + ); |
| 148 | + std::thread::sleep(std::time::Duration::from_millis(1)); |
| 149 | + |
| 150 | + let track_paths: Vec<String> = ep |
| 151 | + .tracks |
| 152 | + .iter() |
| 153 | + .filter_map(|t| lib_keys.get(&norm_key(&t.title, &t.artist)).cloned()) |
| 154 | + .collect(); |
| 155 | + |
| 156 | + let pl = Playlist { |
| 157 | + id, |
| 158 | + name: ep.name.clone(), |
| 159 | + description: String::new(), |
| 160 | + cover: None, |
| 161 | + tracks: track_paths, |
| 162 | + }; |
| 163 | + playlists::write_pl(&pl)?; |
| 164 | + pl_count += 1; |
| 165 | + } |
| 166 | + |
| 167 | + playlists::ensure_favs(); |
| 168 | + let mut favs = playlists::read_pl(playlists::FAVS_ID)?; |
| 169 | + let mut fav_count = 0usize; |
| 170 | + for ft in &export.favorites { |
| 171 | + let key = norm_key(&ft.title, &ft.artist); |
| 172 | + if let Some(path) = lib_keys.get(&key) { |
| 173 | + if !favs.tracks.contains(path) { |
| 174 | + favs.tracks.push(path.clone()); |
| 175 | + fav_count += 1; |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + playlists::write_pl(&favs)?; |
| 180 | + |
| 181 | + Ok(ImportResult { |
| 182 | + playlists_created: pl_count, |
| 183 | + favorites_restored: fav_count, |
| 184 | + tracks_already_present: present, |
| 185 | + tracks_to_download: missing, |
| 186 | + }) |
| 187 | +} |
| 188 | + |
| 189 | +#[tauri::command] |
| 190 | +pub async fn import_fill_playlists(data: String, dir: String) -> Result<(), String> { |
| 191 | + let export: AccountExport = |
| 192 | + serde_json::from_str(&data).map_err(|e| format!("parse: {e}"))?; |
| 193 | + |
| 194 | + let lib = scan_dir(dir); |
| 195 | + let lib_keys: HashMap<String, String> = lib |
| 196 | + .iter() |
| 197 | + .map(|t| (norm_key(&t.title, &t.artist), t.path.clone())) |
| 198 | + .collect(); |
| 199 | + |
| 200 | + let pl_dir = playlists::pl_dir(); |
| 201 | + let entries = fs::read_dir(&pl_dir).map_err(|e| format!("readdir: {e}"))?; |
| 202 | + for entry in entries.flatten() { |
| 203 | + let path = entry.path(); |
| 204 | + if path.extension().map_or(false, |e| e == "json") { |
| 205 | + if let Ok(file_data) = fs::read_to_string(&path) { |
| 206 | + if let Ok(mut pl) = serde_json::from_str::<Playlist>(&file_data) { |
| 207 | + if pl.id == playlists::FAVS_ID { |
| 208 | + continue; |
| 209 | + } |
| 210 | + if let Some(ep) = export.playlists.iter().find(|e| e.name == pl.name) { |
| 211 | + for et in &ep.tracks { |
| 212 | + let key = norm_key(&et.title, &et.artist); |
| 213 | + if let Some(track_path) = lib_keys.get(&key) { |
| 214 | + if !pl.tracks.contains(track_path) { |
| 215 | + pl.tracks.push(track_path.clone()); |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + playlists::write_pl(&pl)?; |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + playlists::ensure_favs(); |
| 227 | + let mut favs = playlists::read_pl(playlists::FAVS_ID)?; |
| 228 | + for ft in &export.favorites { |
| 229 | + let key = norm_key(&ft.title, &ft.artist); |
| 230 | + if let Some(path) = lib_keys.get(&key) { |
| 231 | + if !favs.tracks.contains(path) { |
| 232 | + favs.tracks.push(path.clone()); |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + playlists::write_pl(&favs)?; |
| 237 | + |
| 238 | + Ok(()) |
| 239 | +} |
0 commit comments