From 1d3c86c56c9b0bb542879790259f17ab5a8e7f39 Mon Sep 17 00:00:00 2001 From: Accusys Date: Tue, 17 Nov 2026 22:11:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20upload=5Fpaths=20output=5Fpath=20?= =?UTF-8?q?=E8=87=AA=E5=8B=95=E8=A8=AD=E5=AE=9A=20+=20settings=20save=20?= =?UTF-8?q?=E4=B8=8D=E8=A6=86=E8=93=8B=E6=97=A2=E6=9C=89=20full=5Fpath=20-?= =?UTF-8?q?=20=E6=96=B0=E5=A2=9E=20upload=5Fpath=20=E6=99=82=E8=87=AA?= =?UTF-8?q?=E5=8B=95=E8=A8=AD=E5=AE=9A=20output=5Fpath=20=3D=20/Users/accu?= =?UTF-8?q?sys/momentry/output/{name}=20-=20ON=20CONFLICT=20=E5=8F=AA?= =?UTF-8?q?=E8=A8=AD=20enabled=3Dtrue=EF=BC=8C=E4=B8=8D=E5=86=8D=E8=A6=86?= =?UTF-8?q?=E8=93=8B=20full=5Fpath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/upload_paths.rs | 87 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/src/api/upload_paths.rs b/src/api/upload_paths.rs index 6196fa91..ea2825e2 100644 --- a/src/api/upload_paths.rs +++ b/src/api/upload_paths.rs @@ -66,12 +66,15 @@ pub struct UpdateUploadPathRequest { // ── Routes ────────────────────────────────────────────────────────── pub fn upload_path_routes() -> Router { + use axum::routing::post; Router::new() .route("/api/v1/admin/upload-paths", get(list_upload_paths)) .route("/api/v1/admin/upload-paths", post(create_upload_path)) .route("/api/v1/admin/upload-paths/:id", get(get_upload_path)) .route("/api/v1/admin/upload-paths/:id", put(update_upload_path)) .route("/api/v1/admin/upload-paths/:id", delete(delete_upload_path)) + .route("/api/v2/settings", post(save_legacy_settings)) + .route("/api/v2/settings/validate", post(validate_legacy_settings)) } // ── Handlers ──────────────────────────────────────────────────────── @@ -253,3 +256,87 @@ async fn delete_upload_path( info!("[upload_paths] deleted id={}", id); Ok(Json(serde_json::json!({"success": true}))) } + +// ── Legacy settings compatibility (/api/v2/settings) ───────────────── + +#[derive(Debug, Deserialize)] +struct LegacySettingsRequest { + server: Option, +} + +#[derive(Debug, Deserialize)] +struct LegacyServerSettings { + upload_path: Option, +} + +/// POST /api/v2/settings — legacy compatibility for admin upload path +pub async fn save_legacy_settings( + State(state): State, + Json(req): Json, +) -> Result, StatusCode> { + let upload_path = req.server.and_then(|s| s.upload_path).unwrap_or_default(); + if upload_path.is_empty() { + return Ok(Json(serde_json::json!({ + "success": false, + "error": "upload_path is required" + }))); + } + + let pool = state.db.pool(); + let name = std::path::Path::new(&upload_path) + .file_name() + .map(|n| n.to_string_lossy().to_string()) + .unwrap_or_else(|| upload_path.replace('/', "_")); + + // Check if already exists + let existing: Option = sqlx::query_scalar( + "SELECT id FROM upload_paths WHERE full_path = $1" + ) + .bind(&upload_path) + .fetch_optional(pool) + .await + .map_err(|e| { + error!("[settings] query failed: {}", e); + StatusCode::INTERNAL_SERVER_ERROR + })?; + + if let Some(id) = existing { + let _ = sqlx::query("UPDATE upload_paths SET enabled = true, updated_at = NOW() WHERE id = $1") + .bind(id) + .execute(pool) + .await; + } else { + let output_path = format!("/Users/accusys/momentry/output/{}", name); + let _ = sqlx::query( + "INSERT INTO upload_paths (name, base_path, sub_path, full_path, output_path, enabled) + VALUES ($1, $2, '', $3, $4, true) ON CONFLICT (name) DO UPDATE SET enabled = true, updated_at = NOW()" + ) + .bind(&name) + .bind(&upload_path) + .bind(&upload_path) + .bind(&output_path) + .execute(pool) + .await; + } + + info!("[settings] saved upload_path: {}", upload_path); + Ok(Json(serde_json::json!({ + "success": true, + "message": "Upload path saved successfully" + }))) +} + +/// POST /api/v2/settings/validate — always valid (no restrictions) +pub async fn validate_legacy_settings( + Json(req): Json, +) -> Result, StatusCode> { + let upload_path = req.server.and_then(|s| s.upload_path).unwrap_or_default(); + Ok(Json(serde_json::json!({ + "success": true, + "valid": true, + "message": format!("Path is valid: {}", upload_path), + "details": { + "absolute_path": upload_path + } + }))) +}