Skip to main content

smugmug_cli/cache/
mod.rs

1use anyhow::{Context, Result};
2use directories::ProjectDirs;
3use std::path::PathBuf;
4
5pub mod hash_store;
6
7pub use hash_store::HashStore;
8
9/// The cache directory `upload` and `backup` use: the hash store and file
10/// index (one sled database), and `last_run.json`.
11pub fn get_cache_path() -> Result<PathBuf> {
12    let cache_dir = match ProjectDirs::from("com", "smugmug-cli", "smugmug-cli") {
13        Some(dirs) => dirs.cache_dir().to_path_buf(),
14        None => PathBuf::from(".cache"),
15    };
16
17    // Ensure the cache directory exists
18    std::fs::create_dir_all(&cache_dir).context("Failed to create cache directory")?;
19
20    Ok(cache_dir)
21}
22
23/// Clear the deduplication cache
24pub fn clear_cache() -> Result<()> {
25    let cache_path = get_cache_path()?;
26    let store = HashStore::new(cache_path.to_str().unwrap())?;
27
28    let stats = store.stats()?;
29    println!("Clearing {} entries from cache...", stats.total_entries);
30
31    store.clear()?;
32    println!("Cache cleared successfully!");
33
34    Ok(())
35}