Use TagDao for improved filtering
This commit is contained in:
68
src/files.rs
68
src/files.rs
@@ -24,7 +24,7 @@ use crate::tags::TagDao;
|
||||
use crate::video::StreamActor;
|
||||
use path_absolutize::*;
|
||||
use rand::prelude::SliceRandom;
|
||||
use rand::{thread_rng};
|
||||
use rand::thread_rng;
|
||||
use serde::Deserialize;
|
||||
|
||||
pub async fn list_photos<TagD: TagDao, FS: FileSystemAccess>(
|
||||
@@ -51,54 +51,38 @@ pub async fn list_photos<TagD: TagDao, FS: FileSystemAccess>(
|
||||
.filter_map(|t| t.parse().ok())
|
||||
.collect::<Vec<i32>>();
|
||||
|
||||
let exclude_tag_ids = req.exclude_tag_ids.clone()
|
||||
let exclude_tag_ids = req
|
||||
.exclude_tag_ids
|
||||
.clone()
|
||||
.unwrap_or(String::new())
|
||||
.split(',')
|
||||
.filter_map(|t| t.parse().ok())
|
||||
.collect::<Vec<i32>>();
|
||||
|
||||
return dao
|
||||
.get_files_with_any_tag_ids(tag_ids.clone(), exclude_tag_ids.clone())
|
||||
.context(format!("Failed to get files with tag_ids: {:?}", tag_ids))
|
||||
.map(|tagged_files| {
|
||||
return if let Some(sort_type) = req.sort {
|
||||
debug!("Sorting files: {:?}", sort_type);
|
||||
sort(tagged_files, sort_type)
|
||||
} else {
|
||||
tagged_files
|
||||
};
|
||||
})
|
||||
.map(|tagged_files| match filter_mode {
|
||||
FilterMode::Any => tagged_files
|
||||
.iter()
|
||||
.filter(|file| file.starts_with(search_path))
|
||||
.cloned()
|
||||
.collect(),
|
||||
FilterMode::All => tagged_files
|
||||
.iter()
|
||||
.filter(|&file_path| {
|
||||
if !file_path.starts_with(search_path) {
|
||||
return false;
|
||||
}
|
||||
match filter_mode {
|
||||
FilterMode::Any => dao.get_files_with_any_tag_ids(tag_ids.clone(), exclude_tag_ids),
|
||||
FilterMode::All => dao.get_files_with_all_tag_ids(tag_ids.clone(), exclude_tag_ids),
|
||||
}
|
||||
.context(format!(
|
||||
"Failed to get files with tag_ids: {:?} with filter_mode: {:?}",
|
||||
tag_ids, filter_mode
|
||||
))
|
||||
.map(|files| {
|
||||
files
|
||||
.into_iter()
|
||||
.filter(|file_path| file_path.starts_with(search_path))
|
||||
.collect()
|
||||
})
|
||||
.map(|tagged_files| {
|
||||
trace!("Found tagged files: {:?}", tagged_files);
|
||||
|
||||
let file_tags = dao.get_tags_for_path(file_path).unwrap_or_default();
|
||||
tag_ids
|
||||
.iter()
|
||||
.all(|id| file_tags.iter().any(|tag| &tag.id == id))
|
||||
})
|
||||
.cloned()
|
||||
.collect::<Vec<String>>(),
|
||||
HttpResponse::Ok().json(PhotosResponse {
|
||||
photos: tagged_files,
|
||||
dirs: vec![],
|
||||
})
|
||||
.map(|tagged_files| {
|
||||
trace!("Found tagged files: {:?}", tagged_files);
|
||||
|
||||
HttpResponse::Ok().json(PhotosResponse {
|
||||
photos: tagged_files,
|
||||
dirs: vec![],
|
||||
})
|
||||
})
|
||||
.into_http_internal_err()
|
||||
.unwrap_or_else(|e| e.error_response());
|
||||
})
|
||||
.into_http_internal_err()
|
||||
.unwrap_or_else(|e| e.error_response());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user