bcae4e5466
Introduce an `AppState` struct to hold shared application state, including configuration and the transcription sender. Modify `create_router` and `create_router_with_state` to use `AppState`. Update `main.rs` to initialize the transcription worker and pass the `AppState` to the router. Implement the transcription worker logic in `server/src/transcribe/worker.rs`, handling job queues, FFmpeg remuxing, and Whisper transcription. Update `server/src/auth.rs` to use `Arc<Config>` from the shared state. Modify `server/src/routes/upload.rs` to enqueue transcription jobs and handle potential errors. Add `TranscribeJob` and channel types in `server/src/transcribe/mod.rs`.
55 lines
1.4 KiB
Rust
55 lines
1.4 KiB
Rust
use std::path::PathBuf;
|
|
use std::sync::Arc;
|
|
|
|
use axum::extract::{FromRef, FromRequestParts};
|
|
use axum::http::request::Parts;
|
|
|
|
use crate::config::Config;
|
|
use crate::error::AppError;
|
|
|
|
/// Extracted from the X-API-Key header on every authenticated request.
|
|
pub struct AuthenticatedUser {
|
|
pub slug: String,
|
|
pub role: String,
|
|
pub data_dir: PathBuf,
|
|
}
|
|
|
|
impl<S> FromRequestParts<S> for AuthenticatedUser
|
|
where
|
|
S: Send + Sync,
|
|
Arc<Config>: FromRef<S>,
|
|
{
|
|
type Rejection = AppError;
|
|
|
|
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
|
let config = Arc::<Config>::from_ref(state);
|
|
|
|
let api_key = parts
|
|
.headers
|
|
.get("X-API-Key")
|
|
.and_then(|v| v.to_str().ok())
|
|
.ok_or(AppError::Unauthorized)?;
|
|
|
|
let slug = config
|
|
.api_keys
|
|
.get(api_key)
|
|
.ok_or(AppError::Unauthorized)?;
|
|
|
|
let user = config
|
|
.users
|
|
.iter()
|
|
.find(|u| u.slug == *slug)
|
|
.ok_or(AppError::Unauthorized)?;
|
|
|
|
let data_dir = config.data_path.join(slug);
|
|
tokio::fs::create_dir_all(data_dir.join("open")).await?;
|
|
tokio::fs::create_dir_all(data_dir.join("done")).await?;
|
|
|
|
Ok(Self {
|
|
slug: slug.clone(),
|
|
role: user.role.clone(),
|
|
data_dir,
|
|
})
|
|
}
|
|
}
|