first
This commit is contained in:
108
src-tauri/src/commands.rs
Normal file
108
src-tauri/src/commands.rs
Normal file
@@ -0,0 +1,108 @@
|
||||
// filepath: src-tauri/src/commands.rs
|
||||
use tauri::AppHandle;
|
||||
use crate::{ytdlp, downloader, storage};
|
||||
use crate::downloader::DownloadOptions;
|
||||
use crate::storage::{Settings, HistoryItem};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn init_ytdlp(app: AppHandle) -> Result<bool, String> {
|
||||
if ytdlp::check_ytdlp(&app) {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
// If not found, try to download
|
||||
match ytdlp::download_ytdlp(&app).await {
|
||||
Ok(_) => Ok(true),
|
||||
Err(e) => Err(format!("Failed to download yt-dlp: {}", e)),
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn update_ytdlp(app: AppHandle) -> Result<String, String> {
|
||||
ytdlp::update_ytdlp(&app).await.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_ytdlp_version(app: AppHandle) -> Result<String, String> {
|
||||
ytdlp::get_version(&app).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn fetch_metadata(app: AppHandle, url: String) -> Result<downloader::MetadataResult, String> {
|
||||
downloader::fetch_metadata(&app, &url).await.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn start_download(app: AppHandle, url: String, options: DownloadOptions, metadata: downloader::VideoMetadata) -> Result<String, String> {
|
||||
// Generate a task ID
|
||||
let id = Uuid::new_v4().to_string();
|
||||
let id_clone = id.clone();
|
||||
|
||||
// Spawn the download task
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let res = downloader::download_video(app.clone(), id_clone.clone(), url.clone(), options.clone()).await;
|
||||
|
||||
let status = if res.is_ok() { "success" } else { "failed" };
|
||||
|
||||
// Add to history
|
||||
let item = HistoryItem {
|
||||
id: id_clone,
|
||||
title: metadata.title,
|
||||
thumbnail: metadata.thumbnail,
|
||||
url: url,
|
||||
output_path: format!("{}/", options.output_path), // Rough path, real path is dynamic
|
||||
timestamp: chrono::Utc::now(),
|
||||
status: status.to_string(),
|
||||
format: options.quality,
|
||||
};
|
||||
|
||||
let _ = storage::add_history_item(&app, item);
|
||||
});
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_settings(app: AppHandle) -> Result<Settings, String> {
|
||||
storage::load_settings(&app).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn save_settings(app: AppHandle, settings: Settings) -> Result<(), String> {
|
||||
storage::save_settings(&app, &settings).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_history(app: AppHandle) -> Result<Vec<HistoryItem>, String> {
|
||||
storage::load_history(&app).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn clear_history(app: AppHandle) -> Result<(), String> {
|
||||
storage::clear_history(&app).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn delete_history_item(app: AppHandle, id: String) -> Result<(), String> {
|
||||
storage::delete_history_item(&app, &id).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn open_in_explorer(path: String) -> Result<(), String> {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
std::process::Command::new("explorer")
|
||||
.arg(path)
|
||||
.spawn()
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
std::process::Command::new("open")
|
||||
.arg(path)
|
||||
.spawn()
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user