004 Multi-library Support #54
@@ -192,6 +192,10 @@ pub struct ThumbnailRequest {
|
||||
pub(crate) format: Option<ThumbnailFormat>,
|
||||
#[serde(default)]
|
||||
pub(crate) shape: Option<ThumbnailShape>,
|
||||
/// Optional library filter. Accepts a library id (e.g. "1") or name
|
||||
/// (e.g. "main"). When omitted, defaults to the primary library.
|
||||
#[serde(default)]
|
||||
pub(crate) library: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, PartialEq)]
|
||||
|
||||
40
src/files.rs
40
src/files.rs
@@ -422,7 +422,7 @@ pub async fn list_photos<TagD: TagDao, FS: FileSystemAccess>(
|
||||
sort_type,
|
||||
&mut exif_dao_guard,
|
||||
&span_context,
|
||||
app_state.base_path.as_ref(),
|
||||
scoped_library.root_path.as_ref(),
|
||||
limit,
|
||||
offset,
|
||||
);
|
||||
@@ -473,11 +473,14 @@ pub async fn list_photos<TagD: TagDao, FS: FileSystemAccess>(
|
||||
.unwrap_or_else(|e| e.error_response());
|
||||
}
|
||||
|
||||
// Use recursive or non-recursive file listing based on flag
|
||||
// Use recursive or non-recursive file listing based on flag. Both
|
||||
// paths must walk the *scoped* library's root; the generic
|
||||
// FileSystemAccess trait (file_system.get_files_for_path) is pinned
|
||||
// to AppState's base_path at construction time and doesn't know
|
||||
// which library the request targets.
|
||||
let files_result = if search_recursively {
|
||||
// For recursive search without tags, manually list files recursively
|
||||
is_valid_full_path(
|
||||
&PathBuf::from(&app_state.base_path),
|
||||
&PathBuf::from(&scoped_library.root_path),
|
||||
&PathBuf::from(search_path),
|
||||
false,
|
||||
)
|
||||
@@ -486,8 +489,21 @@ pub async fn list_photos<TagD: TagDao, FS: FileSystemAccess>(
|
||||
list_files_recursive(&path).unwrap_or_default()
|
||||
})
|
||||
.context("Invalid path")
|
||||
} else {
|
||||
} else if scoped_library.id == app_state.primary_library().id {
|
||||
// Primary library: preserve the original FileSystemAccess path so
|
||||
// the test-mock path (MockFileSystem) continues to work.
|
||||
file_system.get_files_for_path(search_path)
|
||||
} else {
|
||||
is_valid_full_path(
|
||||
&PathBuf::from(&scoped_library.root_path),
|
||||
&PathBuf::from(search_path),
|
||||
false,
|
||||
)
|
||||
.map(|path| {
|
||||
debug!("Valid path for non-recursive search: {:?}", path);
|
||||
list_files(&path).unwrap_or_default()
|
||||
})
|
||||
.context("Invalid path")
|
||||
};
|
||||
|
||||
match files_result {
|
||||
@@ -510,10 +526,10 @@ pub async fn list_photos<TagD: TagDao, FS: FileSystemAccess>(
|
||||
match path.metadata() {
|
||||
Ok(md) => {
|
||||
let relative =
|
||||
path.strip_prefix(&app_state.base_path).unwrap_or_else(|_| {
|
||||
path.strip_prefix(&scoped_library.root_path).unwrap_or_else(|_| {
|
||||
panic!(
|
||||
"Unable to strip base path {} from file path {}",
|
||||
&app_state.base_path.path(),
|
||||
"Unable to strip library root {} from file path {}",
|
||||
&scoped_library.root_path,
|
||||
path.display()
|
||||
)
|
||||
});
|
||||
@@ -530,11 +546,11 @@ pub async fn list_photos<TagD: TagDao, FS: FileSystemAccess>(
|
||||
// Include files without metadata if they have extensions
|
||||
if path.extension().is_some() {
|
||||
let relative = path
|
||||
.strip_prefix(&app_state.base_path)
|
||||
.strip_prefix(&scoped_library.root_path)
|
||||
.unwrap_or_else(|_| {
|
||||
panic!(
|
||||
"Unable to strip base path {} from file path {}",
|
||||
&app_state.base_path.path(),
|
||||
"Unable to strip library root {} from file path {}",
|
||||
&scoped_library.root_path,
|
||||
path.display()
|
||||
)
|
||||
});
|
||||
@@ -668,7 +684,7 @@ pub async fn list_photos<TagD: TagDao, FS: FileSystemAccess>(
|
||||
sort_type,
|
||||
&mut exif_dao_guard,
|
||||
&span_context,
|
||||
app_state.base_path.as_ref(),
|
||||
scoped_library.root_path.as_ref(),
|
||||
limit,
|
||||
offset,
|
||||
);
|
||||
|
||||
20
src/main.rs
20
src/main.rs
@@ -104,12 +104,26 @@ async fn get_image(
|
||||
|
||||
let mut span = tracer.start_with_context("get_image", &context);
|
||||
|
||||
if let Some(path) = is_valid_full_path(&app_state.base_path, &req.path, false) {
|
||||
// Resolve library from query param; default to primary so clients that
|
||||
// don't yet send `library=` continue to work.
|
||||
let library = match libraries::resolve_library_param(
|
||||
&app_state,
|
||||
req.library.as_deref(),
|
||||
) {
|
||||
Ok(Some(lib)) => lib,
|
||||
Ok(None) => app_state.primary_library(),
|
||||
Err(msg) => {
|
||||
span.set_status(Status::error(msg.clone()));
|
||||
return HttpResponse::BadRequest().body(msg);
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(path) = is_valid_full_path(&library.root_path, &req.path, false) {
|
||||
let image_size = req.size.unwrap_or(PhotoSize::Full);
|
||||
if image_size == PhotoSize::Thumb {
|
||||
let relative_path = path
|
||||
.strip_prefix(&app_state.base_path)
|
||||
.expect("Error stripping base path prefix from thumbnail");
|
||||
.strip_prefix(&library.root_path)
|
||||
.expect("Error stripping library root prefix from thumbnail");
|
||||
let relative_path_str = relative_path.to_string_lossy().replace('\\', "/");
|
||||
|
||||
let thumbs = &app_state.thumbnail_path;
|
||||
|
||||
@@ -369,6 +369,7 @@ fn collect_exif_memories(
|
||||
exif_dao: &Data<Mutex<Box<dyn ExifDao>>>,
|
||||
context: &opentelemetry::Context,
|
||||
base_path: &str,
|
||||
library_id: i32,
|
||||
now: NaiveDate,
|
||||
span_mode: MemoriesSpan,
|
||||
years_back: u32,
|
||||
@@ -423,7 +424,7 @@ fn collect_exif_memories(
|
||||
path: file_path.clone(),
|
||||
created,
|
||||
modified,
|
||||
library_id: crate::libraries::PRIMARY_LIBRARY_ID,
|
||||
library_id,
|
||||
},
|
||||
file_date,
|
||||
))
|
||||
@@ -434,6 +435,7 @@ fn collect_exif_memories(
|
||||
/// Collect memories from file system scan (for files not in EXIF DB)
|
||||
fn collect_filesystem_memories(
|
||||
base_path: &str,
|
||||
library_id: i32,
|
||||
path_excluder: &PathExcluder,
|
||||
skip_paths: &HashSet<PathBuf>,
|
||||
now: NaiveDate,
|
||||
@@ -485,7 +487,7 @@ fn collect_filesystem_memories(
|
||||
path: path_relative,
|
||||
created,
|
||||
modified,
|
||||
library_id: crate::libraries::PRIMARY_LIBRARY_ID,
|
||||
library_id,
|
||||
},
|
||||
file_date,
|
||||
))
|
||||
@@ -560,6 +562,7 @@ pub async fn list_memories(
|
||||
&exif_dao,
|
||||
&span_context,
|
||||
&scoped_library.root_path,
|
||||
scoped_library.id,
|
||||
now,
|
||||
span_mode,
|
||||
years_back,
|
||||
@@ -576,6 +579,7 @@ pub async fn list_memories(
|
||||
// Phase 2: File system scan (skip EXIF files)
|
||||
let fs_memories = collect_filesystem_memories(
|
||||
&scoped_library.root_path,
|
||||
scoped_library.id,
|
||||
&path_excluder,
|
||||
&exif_paths,
|
||||
now,
|
||||
@@ -1122,7 +1126,7 @@ mod tests {
|
||||
path: "photo1.jpg".to_string(),
|
||||
created: Some(jan_15_2024_9am),
|
||||
modified: Some(jan_15_2024_9am),
|
||||
library_id: crate::libraries::PRIMARY_LIBRARY_ID,
|
||||
library_id: 1,
|
||||
},
|
||||
NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
|
||||
),
|
||||
@@ -1131,7 +1135,7 @@ mod tests {
|
||||
path: "photo2.jpg".to_string(),
|
||||
created: Some(jan_15_2020_10am),
|
||||
modified: Some(jan_15_2020_10am),
|
||||
library_id: crate::libraries::PRIMARY_LIBRARY_ID,
|
||||
library_id: 1,
|
||||
},
|
||||
NaiveDate::from_ymd_opt(2020, 1, 15).unwrap(),
|
||||
),
|
||||
@@ -1140,7 +1144,7 @@ mod tests {
|
||||
path: "photo3.jpg".to_string(),
|
||||
created: Some(jan_16_2021_8am),
|
||||
modified: Some(jan_16_2021_8am),
|
||||
library_id: crate::libraries::PRIMARY_LIBRARY_ID,
|
||||
library_id: 1,
|
||||
},
|
||||
NaiveDate::from_ymd_opt(2021, 1, 16).unwrap(),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user