1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
// yt - A fully featured command line YouTube client
//
// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of Yt.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
use anyhow::{Context, Result};
use log::{debug, info};
use tokio::fs;
use crate::{
app::App,
storage::video_database::{
Video, VideoStatus, VideoStatusMarker, downloader::set_video_cache_path, get,
},
};
async fn invalidate_video(app: &App, video: &Video, hard: bool) -> Result<()> {
info!("Invalidating cache of video: '{}'", video.title);
if hard {
if let VideoStatus::Cached {
cache_path: path, ..
} = &video.status
{
info!("Removing cached video at: '{}'", path.display());
if let Err(err) = fs::remove_file(path).await.map_err(|err| err.kind()) {
match err {
std::io::ErrorKind::NotFound => {
// The path is already gone
debug!(
"Not actually removing path: '{}'. It is already gone.",
path.display()
);
}
err => Err(std::io::Error::from(err)).with_context(|| {
format!(
"Failed to delete video ('{}') cache path: '{}'.",
video.title,
path.display()
)
})?,
}
}
}
}
set_video_cache_path(app, &video.extractor_hash, None).await?;
Ok(())
}
pub async fn invalidate(app: &App, hard: bool) -> Result<()> {
let all_cached_things = get::videos(app, &[VideoStatusMarker::Cached]).await?;
info!("Got videos to invalidate: '{}'", all_cached_things.len());
for video in all_cached_things {
invalidate_video(app, &video, hard).await?;
}
Ok(())
}
/// # Panics
/// Only if internal assertions fail.
pub async fn maintain(app: &App, all: bool) -> Result<()> {
let domain = if all {
VideoStatusMarker::ALL.as_slice()
} else {
&[VideoStatusMarker::Watch, VideoStatusMarker::Cached]
};
let cached_videos = get::videos(app, domain).await?;
let mut found_focused = 0;
for vid in cached_videos {
if let VideoStatus::Cached {
cache_path: path,
is_focused,
} = &vid.status
{
info!("Checking if path ('{}') exists", path.display());
if !path.exists() {
invalidate_video(app, &vid, false).await?;
}
if *is_focused {
found_focused += 1;
}
}
}
assert!(
found_focused <= 1,
"Only one video can be focused at a time"
);
Ok(())
}
|