Filter out stale metadata entries.

This commit is contained in:
Bill Thiede 2019-11-25 09:26:04 -08:00
parent a2f17ed511
commit 7f00c90003
2 changed files with 26 additions and 10 deletions

View File

@ -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()

View File

@ -245,14 +245,8 @@ 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.compact_metadata()?;
}
lib.update_metadata()?;
lib.compact_metadata()?;
}
}
Ok(())