fix: upload_paths output_path 自動設定 + settings save 不覆蓋既有 full_path
- 新增 upload_path 時自動設定 output_path = /Users/accusys/momentry/output/{name}
- ON CONFLICT 只設 enabled=true,不再覆蓋 full_path
This commit is contained in:
@@ -66,12 +66,15 @@ pub struct UpdateUploadPathRequest {
|
||||
// ── Routes ──────────────────────────────────────────────────────────
|
||||
|
||||
pub fn upload_path_routes() -> Router<AppState> {
|
||||
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<LegacyServerSettings>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct LegacyServerSettings {
|
||||
upload_path: Option<String>,
|
||||
}
|
||||
|
||||
/// POST /api/v2/settings — legacy compatibility for admin upload path
|
||||
pub async fn save_legacy_settings(
|
||||
State(state): State<AppState>,
|
||||
Json(req): Json<LegacySettingsRequest>,
|
||||
) -> Result<Json<serde_json::Value>, 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<i32> = 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<LegacySettingsRequest>,
|
||||
) -> Result<Json<serde_json::Value>, 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
|
||||
}
|
||||
})))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user