about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--crates/yt/src/storage/db/insert/maintenance.rs28
-rw-r--r--crates/yt/src/storage/db/insert/mod.rs1
2 files changed, 29 insertions, 0 deletions
diff --git a/crates/yt/src/storage/db/insert/maintenance.rs b/crates/yt/src/storage/db/insert/maintenance.rs
new file mode 100644
index 0000000..6442c48
--- /dev/null
+++ b/crates/yt/src/storage/db/insert/maintenance.rs
@@ -0,0 +1,28 @@
+use crate::{
+    app::App,
+    storage::db::{
+        insert::Operations,
+        video::{Video, VideoStatus, VideoStatusMarker},
+    },
+};
+
+use anyhow::Result;
+
+/// Remove the downloaded paths from videos in the db, that no longer exist on the file system.
+pub(crate) async fn clear_stale_downloaded_paths(app: &App) -> Result<()> {
+    let mut cached_videos = Video::in_states(app, &[VideoStatusMarker::Cached]).await?;
+
+    let mut ops = Operations::new("DbMaintain: init");
+    for vid in &mut cached_videos {
+        if let VideoStatus::Cached { cache_path, .. } = &vid.status {
+            if !cache_path.exists() {
+                vid.remove_download_path(&mut ops);
+            }
+        } else {
+            unreachable!("We only asked for cached videos.")
+        }
+    }
+    ops.commit(app).await?;
+
+    Ok(())
+}
diff --git a/crates/yt/src/storage/db/insert/mod.rs b/crates/yt/src/storage/db/insert/mod.rs
index f1d464f..e83287a 100644
--- a/crates/yt/src/storage/db/insert/mod.rs
+++ b/crates/yt/src/storage/db/insert/mod.rs
@@ -6,6 +6,7 @@ use anyhow::Result;
 use log::trace;
 use sqlx::SqliteConnection;
 
+pub(crate) mod maintenance;
 pub(crate) mod playlist;
 pub(crate) mod subscription;
 pub(crate) mod video;