音乐模块调整
This commit is contained in:
1 parent
4f574cb5fa
commit
8f853f7ef3
31 files changed
+3819
-936
No files matched your search
@@ -15,6 +15,8 @@ use axum::{
|
||||
Router,
|
||||
};
|
||||
|
||||
use lofty::file::TaggedFileExt;
|
||||
|
||||
use super::conn::normalize_base_url;
|
||||
|
||||
/// 由 Feiniu 运行期与代理 handler 共享的连接配置(登录更新、登出置空)。
|
||||
@@ -23,6 +25,8 @@ pub struct ProxyCfg {
|
||||
pub base_url: String,
|
||||
pub token: String,
|
||||
pub access_code: String,
|
||||
/// FnConnect 中继链路:所有请求需携带 `Cookie: mode=relay`
|
||||
pub relay: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -44,6 +48,7 @@ pub async fn start(shared: ProxyShared) -> Result<u16, String> {
|
||||
let app = Router::new()
|
||||
.route("/feiniu/stream", get(proxy_stream))
|
||||
.route("/feiniu/cover", get(proxy_cover))
|
||||
.route("/feiniu/local-cover", get(proxy_local_cover))
|
||||
.with_state(shared);
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = axum::serve(listener, app).await {
|
||||
@@ -53,6 +58,48 @@ pub async fn start(shared: ProxyShared) -> Result<u16, String> {
|
||||
Ok(port)
|
||||
}
|
||||
|
||||
/// 本地音频文件的内嵌封面:`GET /feiniu/local-cover?path=<绝对路径>`。
|
||||
///
|
||||
/// 供「本地曲库」列表显示封面(无需 NAS 登录)。只接受音频扩展名,且必须
|
||||
/// 能被 lofty 解析出内嵌图片——非音频文件在这里必然 404,不构成任意文件读取。
|
||||
/// 响应带 immutable 缓存头:path 不变时 WebView 直接复用。
|
||||
async fn proxy_local_cover(Query(q): Query<HashMap<String, String>>) -> Response {
|
||||
const AUDIO_EXTS: [&str; 7] = ["mp3", "flac", "wav", "m4a", "aac", "ogg", "ape"];
|
||||
let Some(path) = q.get("path") else {
|
||||
return (StatusCode::BAD_REQUEST, "missing path").into_response();
|
||||
};
|
||||
let p = std::path::Path::new(path);
|
||||
let ext_ok = p
|
||||
.extension()
|
||||
.and_then(|e| e.to_str())
|
||||
.map(|e| AUDIO_EXTS.contains(&e.to_lowercase().as_str()))
|
||||
.unwrap_or(false);
|
||||
if !ext_ok || !p.is_file() {
|
||||
return StatusCode::NOT_FOUND.into_response();
|
||||
}
|
||||
let picture = lofty::read_from_path(p).ok().and_then(|tagged| {
|
||||
tagged
|
||||
.primary_tag()
|
||||
.or_else(|| tagged.first_tag())
|
||||
.and_then(|t| t.pictures().first())
|
||||
.cloned()
|
||||
});
|
||||
let Some(pic) = picture else {
|
||||
return StatusCode::NOT_FOUND.into_response();
|
||||
};
|
||||
// lofty 0.22 的 mime_type 返回 Option<MimeType>
|
||||
let mime = pic
|
||||
.mime_type()
|
||||
.map(|m| m.to_string())
|
||||
.unwrap_or_else(|| "image/jpeg".to_string());
|
||||
let mut resp = ([(header::CONTENT_TYPE, mime)], pic.data().to_vec()).into_response();
|
||||
resp.headers_mut().insert(
|
||||
header::CACHE_CONTROL,
|
||||
header::HeaderValue::from_static("public, max-age=2592000, immutable"),
|
||||
);
|
||||
resp
|
||||
}
|
||||
|
||||
async fn proxy_stream(
|
||||
State(s): State<ProxyShared>,
|
||||
Query(q): Query<HashMap<String, String>>,
|
||||
@@ -118,7 +165,15 @@ async fn forward(
|
||||
let qrefs: Vec<(&str, &str)> = query.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect();
|
||||
|
||||
let mut rb = s.client.get(&url).query(&qrefs);
|
||||
rb = rb.header("cookie", format!("music-token={}", cfg.token));
|
||||
// 中继链路必须带 mode=relay,网关据此转发到 NAS(与 music-token 合并进同一个 Cookie 头)。
|
||||
rb = rb.header(
|
||||
"cookie",
|
||||
if cfg.relay {
|
||||
format!("music-token={}; mode=relay", cfg.token)
|
||||
} else {
|
||||
format!("music-token={}", cfg.token)
|
||||
},
|
||||
);
|
||||
if !cfg.access_code.is_empty() {
|
||||
use base64::Engine;
|
||||
rb = rb
|
||||
@@ -143,6 +198,12 @@ async fn forward(
|
||||
let cl = resp.headers().get(header::CONTENT_LENGTH).cloned();
|
||||
let cr = resp.headers().get(header::CONTENT_RANGE).cloned();
|
||||
let ar = resp.headers().get(header::ACCEPT_RANGES).cloned();
|
||||
// 缓存头必须透传:NAS 的封面接口带 `public, max-age=2592000, immutable`,
|
||||
// 丢掉它就等于告诉 WebView「这个响应不可缓存」——每次进曲库页都要重新下载全部封面。
|
||||
let cc = resp.headers().get(header::CACHE_CONTROL).cloned();
|
||||
let et = resp.headers().get(header::ETAG).cloned();
|
||||
let lm = resp.headers().get(header::LAST_MODIFIED).cloned();
|
||||
let ex = resp.headers().get(header::EXPIRES).cloned();
|
||||
|
||||
let body = axum::body::Body::from_stream(resp.bytes_stream());
|
||||
let mut out = Response::new(body);
|
||||
@@ -160,5 +221,17 @@ async fn forward(
|
||||
if let Some(v) = ar {
|
||||
h.insert(header::ACCEPT_RANGES, v);
|
||||
}
|
||||
if let Some(v) = cc {
|
||||
h.insert(header::CACHE_CONTROL, v);
|
||||
}
|
||||
if let Some(v) = et {
|
||||
h.insert(header::ETAG, v);
|
||||
}
|
||||
if let Some(v) = lm {
|
||||
h.insert(header::LAST_MODIFIED, v);
|
||||
}
|
||||
if let Some(v) = ex {
|
||||
h.insert(header::EXPIRES, v);
|
||||
}
|
||||
out
|
||||
}
|
||||
Reference in new issue
Block a user