This commit is contained in:
Julian Freeman
2026-04-19 11:08:28 -04:00
parent bcadf36b71
commit 634c349ebb
5 changed files with 250 additions and 11 deletions

View File

@@ -179,7 +179,90 @@ fn open_database(app: &AppHandle) -> Result<Connection> {
}
pub fn initialize_storage(app: &AppHandle) -> Result<()> {
let _ = open_database(app)?;
let connection = open_database(app)?;
migrate_legacy_files(app, &connection)?;
Ok(())
}
fn get_legacy_settings_path(app: &AppHandle) -> Result<PathBuf> {
Ok(get_app_data_dir(app)?.join("settings.json"))
}
fn get_legacy_history_path(app: &AppHandle) -> Result<PathBuf> {
Ok(get_app_data_dir(app)?.join("history.json"))
}
fn migrate_legacy_files(app: &AppHandle, connection: &Connection) -> Result<()> {
let task_count: i64 = connection.query_row("SELECT COUNT(*) FROM tasks", [], |row| row.get(0))?;
let settings_path = get_legacy_settings_path(app)?;
if settings_path.exists() {
let content = fs::read_to_string(&settings_path)?;
let legacy_settings: Settings = serde_json::from_str(&content)?;
save_settings(app, &legacy_settings)?;
}
if task_count == 0 {
let history_path = get_legacy_history_path(app)?;
if history_path.exists() {
let content = fs::read_to_string(&history_path)?;
let legacy_history: Vec<HistoryItem> = serde_json::from_str(&content)?;
for item in legacy_history {
let metadata = VideoMetadata {
id: item.id.clone(),
title: item.title.clone(),
thumbnail: item.thumbnail.clone(),
duration: None,
uploader: None,
url: Some(item.url.clone()),
extractor: None,
site_name: None,
};
let options = DownloadOptions {
is_audio_only: false,
quality: "best".to_string(),
output_path: item.output_path.clone(),
output_format: item.format.clone(),
cookies_path: None,
};
let metadata_json = serde_json::to_string(&metadata)?;
let options_json = serde_json::to_string(&options)?;
let status = match item.status.as_str() {
"success" => "completed",
other => other,
};
let timestamp = to_rfc3339(item.timestamp);
connection.execute(
"INSERT OR IGNORE INTO tasks (
id, source_url, normalized_url, extractor, site_name, title, thumbnail, output_path,
file_path, status, progress, speed, eta, format, is_audio_only, quality, output_format,
cookies_path, error_message, created_at, started_at, finished_at, metadata_json, options_json
) VALUES (
?1, ?2, ?3, NULL, NULL, ?4, ?5, ?6,
?7, ?8, 100, '-', NULL, ?9, 0, 'best', ?10,
'', NULL, ?11, ?11, ?11, ?12, ?13
)",
params![
item.id,
item.url,
item.url,
item.title,
item.thumbnail,
item.output_path,
item.file_path,
status,
item.format,
item.format,
timestamp,
metadata_json,
options_json
],
)?;
}
}
}
Ok(())
}