start at 3am
This commit is contained in:
@@ -53,33 +53,60 @@ pub fn get_image_base64(path: String) -> Result<String, String> {
|
||||
#[tauri::command]
|
||||
pub fn get_timeline(date: String, base_dir: String) -> Vec<serde_json::Value> {
|
||||
let mut results = Vec::new();
|
||||
let dir_path = PathBuf::from(base_dir).join(date);
|
||||
let base_path = PathBuf::from(base_dir);
|
||||
|
||||
if !dir_path.exists() || !dir_path.is_dir() {
|
||||
return results;
|
||||
}
|
||||
// Parse current date
|
||||
let current_date = match chrono::NaiveDate::parse_from_str(&date, "%Y-%m-%d") {
|
||||
Ok(d) => d,
|
||||
Err(_) => return results,
|
||||
};
|
||||
let next_date = current_date + chrono::Duration::days(1);
|
||||
let next_date_str = next_date.format("%Y-%m-%d").to_string();
|
||||
|
||||
if let Ok(entries) = fs::read_dir(dir_path) {
|
||||
let mut paths: Vec<_> = entries
|
||||
.filter_map(|e| e.ok())
|
||||
.map(|e| e.path())
|
||||
.filter(|p| p.is_file() && p.extension().and_then(|s| s.to_str()) == Some("jpg"))
|
||||
.collect();
|
||||
|
||||
paths.sort();
|
||||
|
||||
for path in paths {
|
||||
if let Some(file_name) = path.file_stem().and_then(|s| s.to_str()) {
|
||||
// file_name format: "14-30-00" or "14-30-00_0"
|
||||
let time_str = file_name.replace("-", ":").replace("_", " ");
|
||||
results.push(serde_json::json!({
|
||||
"time": time_str,
|
||||
"path": path.to_string_lossy().to_string()
|
||||
}));
|
||||
// Helper to process a directory with a time filter
|
||||
let mut process_dir = |dir_date: &str, is_next_day: bool| {
|
||||
let dir_path = base_path.join(dir_date);
|
||||
if let Ok(entries) = fs::read_dir(dir_path) {
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
if path.is_file() && path.extension().and_then(|s| s.to_str()) == Some("jpg") {
|
||||
if let Some(file_name) = path.file_stem().and_then(|s| s.to_str()) {
|
||||
// file_name format: "14-30-00"
|
||||
let parts: Vec<&str> = file_name.split('-').collect();
|
||||
if parts.len() >= 1 {
|
||||
if let Ok(hour) = parts[0].parse::<u32>() {
|
||||
// Logic: If current day, must be >= 3. If next day, must be < 3.
|
||||
let keep = if !is_next_day { hour >= 3 } else { hour < 3 };
|
||||
if keep {
|
||||
let time_str = file_name.replace("-", ":").replace("_", " ");
|
||||
results.push(serde_json::json!({
|
||||
"time": time_str,
|
||||
"path": path.to_string_lossy().to_string(),
|
||||
"isNextDay": is_next_day
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
process_dir(&date, false);
|
||||
process_dir(&next_date_str, true);
|
||||
|
||||
// Sort results by isNextDay (false first) then by time
|
||||
results.sort_by(|a, b| {
|
||||
let a_next = a["isNextDay"].as_bool().unwrap_or(false);
|
||||
let b_next = b["isNextDay"].as_bool().unwrap_or(false);
|
||||
if a_next != b_next {
|
||||
a_next.cmp(&b_next)
|
||||
} else {
|
||||
a["time"].as_str().unwrap_or("").cmp(b["time"].as_str().unwrap_or(""))
|
||||
}
|
||||
});
|
||||
|
||||
results
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user