Verified Commit 37ab97d0 authored by Sebastian Schüpbach's avatar Sebastian Schüpbach
Browse files

add collection folders blacklist

parent e523d28e
.idea/
/target
config.toml
[package]
name = "media-file-distributor"
version = "0.1.0"
version = "1.0.1"
authors = ["Sebastian Schüpbach <post@annotat.net>"]
edition = "2018"
description = "Web service providing access to the media files uploaded by the Memoriav's partner institutions"
......
host = "0.0.0.0:3000"
base_path = "/swissbib_index/mb_sftp"
ignore_collection_paths = ["stage", "test"]
cache_timeout = 3600
partial_cache_timeout = 5
......
......@@ -61,7 +61,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
"Reading in media files starting with path {}",
&config.base_path
);
let media_cache = MediaFileCache::new(config.base_path.clone(), config.mappings.clone());
let media_cache = MediaFileCache::new(
config.base_path.clone(),
config.ignore_collection_paths.clone(),
config.mappings.clone(),
);
info!(
"Done reading in media files. Found {} metadata files, {} media files and {} thumbnails",
media_cache.metadata_file_size(),
......@@ -112,6 +116,8 @@ pub struct Config {
pub host: String,
/// Path to root directory of media file tree
pub base_path: String,
/// Ignored paths in root directory
pub ignore_collection_paths: Option<Vec<String>>,
/// Timeout (in seconds) after which a full cache update is enforced
pub cache_timeout: Option<u64>,
/// Timeout (in seconds) after which a collection cache is updated if a query for a certain ID failed
......
......@@ -229,6 +229,8 @@ impl Collection {
pub struct MediaFileCache {
/// Collection index
collections: HashMap<String, Collection>,
/// Ignored collection folders
ignored_folders: Vec<String>,
/// Mappings for special file names
custom_mappings: Option<CustomMappings>,
/// Path to root directory
......@@ -239,10 +241,21 @@ pub struct MediaFileCache {
impl MediaFileCache {
/// Creates a new instance
pub fn new(base_path: String, custom_mappings: Option<CustomMappings>) -> Self {
let collections = if let Ok(index) =
MediaFileCache::scan_collection_folders(Path::new(&base_path), &custom_mappings)
{
pub fn new(
base_path: String,
ignore_collection_paths: Option<Vec<String>>,
custom_mappings: Option<CustomMappings>,
) -> Self {
let ignored_folders = if ignore_collection_paths.is_some() {
ignore_collection_paths.unwrap()
} else {
vec![]
};
let collections = if let Ok(index) = MediaFileCache::scan_collection_folders(
Path::new(&base_path),
&ignored_folders,
&custom_mappings,
) {
index
} else {
warn!("Collection indexing failed!");
......@@ -250,6 +263,7 @@ impl MediaFileCache {
};
MediaFileCache {
collections,
ignored_folders,
custom_mappings,
base_path,
created_on: Instant::now(),
......@@ -259,13 +273,20 @@ impl MediaFileCache {
/// Scans the root path for collections and returns an index of collection IDs and their files
fn scan_collection_folders(
dir: &Path,
ignored_folders: &Vec<String>,
custom_mappings: &Option<CustomMappings>,
) -> Result<HashMap<String, Collection>> {
let mut file_index: HashMap<String, Collection> = HashMap::new();
for entry in fs::read_dir(&dir).context("Can't read dir")? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
if path.is_dir()
&& entry
.file_name()
.into_string()
.and_then(|s| Ok(!ignored_folders.contains(&s)))
.unwrap_or(false)
{
let path_as_string = path.to_str().unwrap().to_owned();
let dir_name = path
.file_name()
......@@ -373,6 +394,7 @@ impl MediaFileCache {
pub fn refresh(&mut self) -> Result<()> {
self.collections = MediaFileCache::scan_collection_folders(
Path::new(&self.base_path),
&self.ignored_folders,
&self.custom_mappings,
)
.context("Cache refreshing failed")?;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment