Allow for excluding directories from Memories endpoint

This commit is contained in:
Cameron
2025-08-27 16:02:32 -04:00
parent 5277465f9e
commit e46953194e
2 changed files with 46 additions and 6 deletions

View File

@@ -8,6 +8,7 @@ use opentelemetry::KeyValue;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::path::PathBuf;
use walkdir::WalkDir;
use crate::data::Claims;
@@ -134,10 +135,11 @@ fn extract_date_from_filename(filename: &str) -> Option<DateTime<FixedOffset>> {
};
// 1. Screenshot format: Screenshot_2014-06-01-20-44-50.png
if let Some(captures) = regex::Regex::new(r"Screenshot_(\d{4})-(\d{2})-(\d{2})-(\d{2})-(\d{2})-(\d{2})")
.ok()?
.captures(filename)
.and_then(|c| build_date_from_ymd_capture(&c))
if let Some(captures) =
regex::Regex::new(r"Screenshot_(\d{4})-(\d{2})-(\d{2})-(\d{2})-(\d{2})-(\d{2})")
.ok()?
.captures(filename)
.and_then(|c| build_date_from_ymd_capture(&c))
{
return Some(captures);
}
@@ -232,11 +234,33 @@ pub async fn list_memories(
let base = Path::new(&app_state.base_path);
// Build a list of excluded directories with full paths
let excluded_paths: Vec<PathBuf> = app_state
.excluded_dirs
.iter()
.map(|dir| base.join(dir))
.collect();
debug!("Excluded directories: {:?}", excluded_paths);
let entries: Vec<_> = WalkDir::new(base)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.filter(|e| is_image_or_video(e.path()))
.filter(|e| {
// Skip excluded directories
if !excluded_paths.is_empty() {
let path = e.path();
for excluded in &excluded_paths {
if path.starts_with(excluded) {
debug!("Skipping excluded path: {:?}", path);
return false;
}
}
}
true
})
.filter(|e| e.file_type().is_file() && is_image_or_video(e.path()))
.collect();
let mut memories_with_dates: Vec<(MemoryItem, NaiveDate)> = entries
@@ -310,6 +334,7 @@ pub async fn list_memories(
client_timezone.unwrap_or_else(|| FixedOffset::east_opt(0).unwrap())
),
),
KeyValue::new("excluded_dirs", format!("{:?}", app_state.excluded_dirs)),
],
);
span.set_status(Status::Ok);