Many field paths
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
Json,
|
Json,
|
||||||
extract::{Query, State},
|
extract::{Query, RawQuery, State},
|
||||||
http::{StatusCode, header},
|
http::{StatusCode, header},
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
};
|
};
|
||||||
@@ -17,19 +17,19 @@ use crate::Datasets;
|
|||||||
pub struct FieldQuery {
|
pub struct FieldQuery {
|
||||||
source: String,
|
source: String,
|
||||||
key: String,
|
key: String,
|
||||||
path: String,
|
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
download: bool,
|
download: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extract a specific field from an item's metadata
|
/// Extract a specific field from an item's metadata.
|
||||||
|
/// Multiple `path` parameters may be provided; the first non-null result is returned.
|
||||||
#[utoipa::path(
|
#[utoipa::path(
|
||||||
get,
|
get,
|
||||||
path = "/field",
|
path = "/field",
|
||||||
params(
|
params(
|
||||||
("source" = String, Query, description = "Source label"),
|
("source" = String, Query, description = "Source label"),
|
||||||
("key" = String, Query, description = "Item key"),
|
("key" = String, Query, description = "Item key"),
|
||||||
("path" = String, Query, description = "Object path (e.g. $.flac.title)"),
|
("path" = String, Query, description = "Object path (e.g. $.flac.title); repeat for fallbacks"),
|
||||||
),
|
),
|
||||||
responses(
|
responses(
|
||||||
(status = 200, description = "Field value as JSON"),
|
(status = 200, description = "Field value as JSON"),
|
||||||
@@ -41,43 +41,73 @@ pub struct FieldQuery {
|
|||||||
pub async fn get_field(
|
pub async fn get_field(
|
||||||
State(state): State<Arc<Datasets>>,
|
State(state): State<Arc<Datasets>>,
|
||||||
Query(params): Query<FieldQuery>,
|
Query(params): Query<FieldQuery>,
|
||||||
|
RawQuery(raw_query): RawQuery,
|
||||||
) -> Response {
|
) -> Response {
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
debug!(
|
|
||||||
message = "Serving /field",
|
|
||||||
source = params.source,
|
|
||||||
key = params.key,
|
|
||||||
path = params.path,
|
|
||||||
);
|
|
||||||
|
|
||||||
let label = match Label::try_from(params.source.clone()) {
|
let label = match Label::try_from(params.source.clone()) {
|
||||||
Ok(l) => l,
|
Ok(l) => l,
|
||||||
Err(e) => return (StatusCode::BAD_REQUEST, format!("{e:?}")).into_response(),
|
Err(e) => return (StatusCode::BAD_REQUEST, format!("{e:?}")).into_response(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let path: ObjectPath = match params.path.parse() {
|
// Collect all `path` query params in order (supports repeated ?path=...&path=...)
|
||||||
Ok(p) => p,
|
let raw = raw_query.as_deref().unwrap_or("");
|
||||||
Err(e) => return (StatusCode::BAD_REQUEST, format!("{e:?}")).into_response(),
|
let paths: Vec<ObjectPath> = {
|
||||||
|
let mut result = Vec::new();
|
||||||
|
for part in raw.split('&') {
|
||||||
|
if let Some((k, v)) = part.split_once('=')
|
||||||
|
&& k == "path"
|
||||||
|
{
|
||||||
|
match v.parse::<ObjectPath>() {
|
||||||
|
Ok(p) => result.push(p),
|
||||||
|
Err(e) => {
|
||||||
|
return (StatusCode::BAD_REQUEST, format!("{e:?}")).into_response();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if paths.is_empty() {
|
||||||
|
return (StatusCode::BAD_REQUEST, "Missing `path` query parameter").into_response();
|
||||||
|
}
|
||||||
|
|
||||||
|
debug!(
|
||||||
|
message = "Serving /field",
|
||||||
|
source = params.source,
|
||||||
|
key = params.key,
|
||||||
|
paths = paths.len(),
|
||||||
|
);
|
||||||
|
|
||||||
let Some(item) = state.get(&label, ¶ms.key).await else {
|
let Some(item) = state.get(&label, ¶ms.key).await else {
|
||||||
return StatusCode::NOT_FOUND.into_response();
|
return StatusCode::NOT_FOUND.into_response();
|
||||||
};
|
};
|
||||||
|
|
||||||
let state = ExtractState { ignore_mime: false };
|
let extract_state = ExtractState { ignore_mime: false };
|
||||||
|
|
||||||
let item = PileValue::Item(item);
|
let item = PileValue::Item(item);
|
||||||
let value = match item.query(&state, &path).await {
|
|
||||||
Ok(Some(v)) => v,
|
// Try each path in order, returning the first non-null result
|
||||||
Ok(None) => return StatusCode::NOT_FOUND.into_response(),
|
let mut value = None;
|
||||||
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("{e:?}")).into_response(),
|
for path in &paths {
|
||||||
|
match item.query(&extract_state, path).await {
|
||||||
|
Ok(Some(PileValue::Null)) | Ok(None) => continue,
|
||||||
|
Ok(Some(v)) => {
|
||||||
|
value = Some(v);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, format!("{e:?}")).into_response(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(value) = value else {
|
||||||
|
return StatusCode::NOT_FOUND.into_response();
|
||||||
};
|
};
|
||||||
|
|
||||||
debug!(
|
debug!(
|
||||||
message = "Served /field",
|
message = "Served /field",
|
||||||
source = params.source,
|
source = params.source,
|
||||||
key = params.key,
|
key = params.key,
|
||||||
path = params.path,
|
|
||||||
time_ms = start.elapsed().as_millis()
|
time_ms = start.elapsed().as_millis()
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -106,7 +136,7 @@ pub async fn get_field(
|
|||||||
bytes.as_ref().clone(),
|
bytes.as_ref().clone(),
|
||||||
)
|
)
|
||||||
.into_response(),
|
.into_response(),
|
||||||
_ => match value.to_json(&state).await {
|
_ => match value.to_json(&extract_state).await {
|
||||||
Ok(json) => (
|
Ok(json) => (
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
[(header::CONTENT_DISPOSITION, disposition.to_owned())],
|
[(header::CONTENT_DISPOSITION, disposition.to_owned())],
|
||||||
|
|||||||
Reference in New Issue
Block a user