pinakes-server: relativize media paths against configured root directories
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I9f113e6402030c46ad97f636985b5d6c6a6a6964
This commit is contained in:
parent
5077e9f117
commit
9c67c81a79
11 changed files with 212 additions and 42 deletions
|
|
@ -120,7 +120,13 @@ pub async fn list_media(
|
|||
params.sort,
|
||||
);
|
||||
let items = state.storage.list_media(&pagination).await?;
|
||||
Ok(Json(items.into_iter().map(MediaResponse::from).collect()))
|
||||
let roots = state.config.read().await.directories.roots.clone();
|
||||
Ok(Json(
|
||||
items
|
||||
.into_iter()
|
||||
.map(|item| MediaResponse::new(item, &roots))
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn get_media(
|
||||
|
|
@ -128,7 +134,8 @@ pub async fn get_media(
|
|||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<MediaResponse>, ApiError> {
|
||||
let item = state.storage.get_media(MediaId(id)).await?;
|
||||
Ok(Json(MediaResponse::from(item)))
|
||||
let roots = state.config.read().await.directories.roots.clone();
|
||||
Ok(Json(MediaResponse::new(item, &roots)))
|
||||
}
|
||||
|
||||
/// Maximum length for short text fields (title, artist, album, genre).
|
||||
|
|
@ -206,7 +213,8 @@ pub async fn update_media(
|
|||
&serde_json::json!({"media_id": item.id.to_string()}),
|
||||
);
|
||||
|
||||
Ok(Json(MediaResponse::from(item)))
|
||||
let roots = state.config.read().await.directories.roots.clone();
|
||||
Ok(Json(MediaResponse::new(item, &roots)))
|
||||
}
|
||||
|
||||
pub async fn delete_media(
|
||||
|
|
@ -574,12 +582,14 @@ pub async fn preview_directory(
|
|||
}
|
||||
}
|
||||
|
||||
let roots_for_walk = roots.clone();
|
||||
let files: Vec<DirectoryPreviewFile> =
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let mut result = Vec::new();
|
||||
fn walk_dir(
|
||||
dir: &std::path::Path,
|
||||
recursive: bool,
|
||||
roots: &[std::path::PathBuf],
|
||||
result: &mut Vec<DirectoryPreviewFile>,
|
||||
) {
|
||||
let Ok(entries) = std::fs::read_dir(dir) else {
|
||||
|
|
@ -596,7 +606,7 @@ pub async fn preview_directory(
|
|||
}
|
||||
if path.is_dir() {
|
||||
if recursive {
|
||||
walk_dir(&path, recursive, result);
|
||||
walk_dir(&path, recursive, roots, result);
|
||||
}
|
||||
} else if path.is_file()
|
||||
&& let Some(mt) =
|
||||
|
|
@ -612,7 +622,7 @@ pub async fn preview_directory(
|
|||
.and_then(|v| v.as_str().map(String::from))
|
||||
.unwrap_or_default();
|
||||
result.push(DirectoryPreviewFile {
|
||||
path: path.to_string_lossy().to_string(),
|
||||
path: crate::dto::relativize_path(&path, roots),
|
||||
file_name,
|
||||
media_type,
|
||||
file_size: size,
|
||||
|
|
@ -620,7 +630,7 @@ pub async fn preview_directory(
|
|||
}
|
||||
}
|
||||
}
|
||||
walk_dir(&dir, recursive, &mut result);
|
||||
walk_dir(&dir, recursive, &roots_for_walk, &mut result);
|
||||
result
|
||||
})
|
||||
.await
|
||||
|
|
@ -948,7 +958,8 @@ pub async fn rename_media(
|
|||
)
|
||||
.await?;
|
||||
|
||||
Ok(Json(MediaResponse::from(item)))
|
||||
let roots = state.config.read().await.directories.roots.clone();
|
||||
Ok(Json(MediaResponse::new(item, &roots)))
|
||||
}
|
||||
|
||||
pub async fn move_media_endpoint(
|
||||
|
|
@ -994,7 +1005,8 @@ pub async fn move_media_endpoint(
|
|||
)
|
||||
.await?;
|
||||
|
||||
Ok(Json(MediaResponse::from(item)))
|
||||
let roots = state.config.read().await.directories.roots.clone();
|
||||
Ok(Json(MediaResponse::new(item, &roots)))
|
||||
}
|
||||
|
||||
pub async fn batch_move_media(
|
||||
|
|
@ -1144,7 +1156,8 @@ pub async fn restore_media(
|
|||
&serde_json::json!({"media_id": media_id.to_string(), "restored": true}),
|
||||
);
|
||||
|
||||
Ok(Json(MediaResponse::from(item)))
|
||||
let roots = state.config.read().await.directories.roots.clone();
|
||||
Ok(Json(MediaResponse::new(item, &roots)))
|
||||
}
|
||||
|
||||
pub async fn list_trash(
|
||||
|
|
@ -1159,9 +1172,13 @@ pub async fn list_trash(
|
|||
|
||||
let items = state.storage.list_trash(&pagination).await?;
|
||||
let count = state.storage.count_trash().await?;
|
||||
let roots = state.config.read().await.directories.roots.clone();
|
||||
|
||||
Ok(Json(TrashResponse {
|
||||
items: items.into_iter().map(MediaResponse::from).collect(),
|
||||
items: items
|
||||
.into_iter()
|
||||
.map(|item| MediaResponse::new(item, &roots))
|
||||
.collect(),
|
||||
total_count: count,
|
||||
}))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue