Filter out stale metadata entries.
This commit is contained in:
parent
a2f17ed511
commit
7f00c90003
26
src/lib.rs
26
src/lib.rs
@ -438,7 +438,6 @@ impl MovieLibrary {
|
||||
let f = File::open(&path).context(format!("open {}", path.display()))?;
|
||||
let r = BufReader::new(f);
|
||||
|
||||
// Read the JSON contents of the file as an instance of `User`.
|
||||
let mdf: MetadataFile = serde_json::from_reader(r)
|
||||
.context(format!("serde_json::from_reader {}", path.display()))?;
|
||||
|
||||
@ -548,7 +547,7 @@ impl MovieLibrary {
|
||||
pub fn update_metadata(&self) -> Result<Vec<String>, Error> {
|
||||
let path = Path::new(&self.root).join(FULL_METADATA_FILENAME);
|
||||
|
||||
let old_metadata: HashMap<String, Value> = match File::open(&path) {
|
||||
let mut old_metadata: HashMap<String, Value> = match File::open(&path) {
|
||||
Ok(f) => {
|
||||
let r = BufReader::new(f);
|
||||
serde_json::from_reader(r)?
|
||||
@ -560,6 +559,29 @@ impl MovieLibrary {
|
||||
};
|
||||
|
||||
info!("Read metadata, {} videos found", old_metadata.len());
|
||||
// Filter out stale metadata (where the file no longer exists).
|
||||
let old_metadata: HashMap<String, Value> = self
|
||||
.iter_video_files()
|
||||
.filter(|r| r.is_ok())
|
||||
.filter_map(|r| {
|
||||
let path = r
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.strip_prefix(&self.root)
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.to_owned();
|
||||
match old_metadata.remove(&path) {
|
||||
Some(v) => Some((path, v)),
|
||||
None => None,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
info!(
|
||||
"After removing stale metadata, {} videos found",
|
||||
old_metadata.len()
|
||||
);
|
||||
|
||||
let mut metadata: HashMap<_, _> = self
|
||||
.iter_video_files()
|
||||
|
||||
@ -245,15 +245,9 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
Command::UpdateAndCompactMetadata => {
|
||||
let lib = MovieLibrary::new(MOVIE_DIR);
|
||||
let new_videos = lib.update_metadata()?;
|
||||
if !new_videos.is_empty() {
|
||||
info!(
|
||||
"{} new videos added, recompacting metadata",
|
||||
new_videos.len()
|
||||
);
|
||||
lib.update_metadata()?;
|
||||
lib.compact_metadata()?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user