Add name parameter

This commit is contained in:
2026-03-26 09:57:02 -07:00
parent 5b0953d250
commit f9a39d5ff9
3 changed files with 43 additions and 9 deletions

View File

@@ -20,6 +20,7 @@ pub struct ExtractQuery {
#[serde(default)]
download: bool,
name: Option<String>,
}
/// Extract a specific field from an item's metadata.
@@ -31,6 +32,7 @@ pub struct ExtractQuery {
("source" = String, Query, description = "Source label"),
("key" = String, Query, description = "Item key"),
("path" = String, Query, description = "Object path (e.g. $.flac.title); repeat for fallbacks"),
("name" = Option<String>, Query, description = "Downloaded filename; defaults to the last segment of the key"),
),
responses(
(status = 200, description = "Field value as JSON"),
@@ -112,18 +114,27 @@ pub async fn get_extract(
time_ms = start.elapsed().as_millis()
);
let disposition = if params.download {
let disposition_type = if params.download {
"attachment"
} else {
"inline"
};
let file_name = params.name.unwrap_or_else(|| {
params
.key
.rsplit('/')
.next()
.unwrap_or(&params.key)
.to_owned()
});
let disposition = format!("{disposition_type}; filename=\"{file_name}\"");
match value {
PileValue::String(s) => (
StatusCode::OK,
[
(header::CONTENT_TYPE, "text/plain".to_owned()),
(header::CONTENT_DISPOSITION, disposition.to_owned()),
(header::CONTENT_DISPOSITION, disposition),
],
s.to_string(),
)
@@ -132,7 +143,7 @@ pub async fn get_extract(
StatusCode::OK,
[
(header::CONTENT_TYPE, mime.to_string()),
(header::CONTENT_DISPOSITION, disposition.to_owned()),
(header::CONTENT_DISPOSITION, disposition),
],
bytes.as_ref().clone(),
)
@@ -140,7 +151,7 @@ pub async fn get_extract(
_ => match value.to_json(&extract_state).await {
Ok(json) => (
StatusCode::OK,
[(header::CONTENT_DISPOSITION, disposition.to_owned())],
[(header::CONTENT_DISPOSITION, disposition)],
Json(json),
)
.into_response(),