diff options
| author | 2026-01-15 16:53:51 +0800 | |
|---|---|---|
| committer | 2026-01-15 17:36:39 +0800 | |
| commit | 0477829f3f3e069bb746e711b0acad3494a484e2 (patch) | |
| tree | 06c1729f15709e87f5b0a54614bb3ae23ec317a5 /src-tauri/src/main.rs | |
| parent | 6ef9f5e01e6d7a55861803cb400720a81c64685e (diff) | |
| download | DropOut-0477829f3f3e069bb746e711b0acad3494a484e2.tar.gz DropOut-0477829f3f3e069bb746e711b0acad3494a484e2.zip | |
feat: Implement upload_to_pastebin command for log file uploads with content length validation and support for Pastebin and paste.rs services
Diffstat (limited to 'src-tauri/src/main.rs')
| -rw-r--r-- | src-tauri/src/main.rs | 80 |
1 files changed, 79 insertions, 1 deletions
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 7e6ae80..b69912e 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -6,6 +6,7 @@ use std::sync::Mutex; use tauri::{Emitter, Manager, State, Window}; // Added Emitter use tokio::io::{AsyncBufReadExt, BufReader}; use tokio::process::Command; +use serde::Serialize; // Added Serialize #[cfg(target_os = "windows")] use std::os::windows::process::CommandExt; @@ -1124,8 +1125,84 @@ async fn get_github_releases() -> Result<Vec<GithubRelease>, String> { Ok(result) } +#[derive(Serialize)] +struct PastebinResponse { + url: String, +} + +#[tauri::command] +async fn upload_to_pastebin( + state: State<'_, core::config::ConfigState>, + content: String, +) -> Result<PastebinResponse, String> { + // Check content length limit + if content.len() > 500 * 1024 { + return Err("Log file too large (max 500KB)".to_string()); + } + + // Extract config values before any async calls to avoid holding MutexGuard across await + let (service, api_key) = { + let config = state.config.lock().unwrap(); + ( + config.log_upload_service.clone(), + config.pastebin_api_key.clone(), + ) + }; + + let client = reqwest::Client::new(); + + match service.as_str() { + "pastebin.com" => { + let api_key = api_key + .ok_or("Pastebin API Key not configured in settings")?; + + let res = client + .post("https://pastebin.com/api/api_post.php") + .form(&[ + ("api_dev_key", api_key.as_str()), + ("api_option", "paste"), + ("api_paste_code", content.as_str()), + ("api_paste_private", "1"), // Unlisted + ("api_paste_name", "DropOut Launcher Log"), + ("api_paste_expire_date", "1W"), + ]) + .send() + .await + .map_err(|e| e.to_string())?; + + if !res.status().is_success() { + return Err(format!("Pastebin upload failed: {}", res.status())); + } + + let url = res.text().await.map_err(|e| e.to_string())?; + if url.starts_with("Bad API Request") { + return Err(format!("Pastebin API error: {}", url)); + } + Ok(PastebinResponse { url }) + } + // Default to paste.rs + _ => { + let res = client + .post("https://paste.rs/") + .body(content) + .send() + .await + .map_err(|e| e.to_string())?; + + if !res.status().is_success() { + return Err(format!("paste.rs upload failed: {}", res.status())); + } + + let url = res.text().await.map_err(|e| e.to_string())?; + let url = url.trim().to_string(); + Ok(PastebinResponse { url }) + } + } +} + fn main() { tauri::Builder::default() + .plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_shell::init()) .manage(core::auth::AccountState::new()) @@ -1194,7 +1271,8 @@ fn main() { get_forge_game_versions, get_forge_versions_for_game, install_forge, - get_github_releases + get_github_releases, + upload_to_pastebin ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); |