video2slides: 修缩略图/大图 400 — img 标签带不了头,改用 ?c= query 传 client
deploy video2slides / build-and-deploy (push) Successful in 1m40s

This commit is contained in:
Fam Zheng
2026-06-14 21:42:44 +01:00
parent bb65c4c415
commit 25a28ea092
2 changed files with 20 additions and 4 deletions
+19 -3
View File
@@ -5,7 +5,7 @@ use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use axum::extract::{Multipart, Path as AxPath, State};
use axum::extract::{Multipart, Path as AxPath, Query, State};
use axum::http::{header, HeaderMap, StatusCode};
use axum::response::{IntoResponse, Response};
use axum::Json;
@@ -48,6 +48,20 @@ fn client_id(headers: &HeaderMap) -> Result<String, ApiErr> {
.ok_or_else(|| err(StatusCode::BAD_REQUEST, "缺少 X-Client-Id"))
}
/// `<img src>` 没法带自定义头,图片接口允许用 `?c=<clientId>` query 传 client
/// 兜底再看 header。
#[derive(Deserialize)]
pub struct ClientQuery {
c: Option<String>,
}
fn client_from(q: &ClientQuery, headers: &HeaderMap) -> Result<String, ApiErr> {
if let Some(c) = q.c.as_ref().filter(|s| !s.is_empty()) {
return Ok(c.clone());
}
client_id(headers)
}
// ---------------------------------------------------------------------------
// 上传
// ---------------------------------------------------------------------------
@@ -272,17 +286,19 @@ async fn serve_img(
pub async fn thumb(
State(st): State<AppState>,
AxPath((vid, idx)): AxPath<(String, u32)>,
Query(q): Query<ClientQuery>,
headers: HeaderMap,
) -> Result<Response, ApiErr> {
let client = client_id(&headers)?;
let client = client_from(&q, &headers)?;
serve_img(&st, &client, &vid, "thumbs", idx).await
}
pub async fn frame(
State(st): State<AppState>,
AxPath((vid, idx)): AxPath<(String, u32)>,
Query(q): Query<ClientQuery>,
headers: HeaderMap,
) -> Result<Response, ApiErr> {
let client = client_id(&headers)?;
let client = client_from(&q, &headers)?;
serve_img(&st, &client, &vid, "frames", idx).await
}