Add update-compact-metadata verb.
This commit is contained in:
parent
8b6223627e
commit
185c3cde2d
@ -345,7 +345,7 @@ impl MovieLibrary {
|
|||||||
Ok(serde_json::ser::to_writer_pretty(f, &metadata)?)
|
Ok(serde_json::ser::to_writer_pretty(f, &metadata)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_metadata(&self) -> Result<(), Error> {
|
pub fn update_metadata(&self) -> Result<Vec<String>, Error> {
|
||||||
let path = Path::new(&self.root).join("metadata.json");
|
let path = Path::new(&self.root).join("metadata.json");
|
||||||
// Open the file in read-only mode with buffer.
|
// Open the file in read-only mode with buffer.
|
||||||
let f = File::open(&path).context(format!("open {}", path.display()))?;
|
let f = File::open(&path).context(format!("open {}", path.display()))?;
|
||||||
@ -389,13 +389,14 @@ impl MovieLibrary {
|
|||||||
})
|
})
|
||||||
.map(|(path, json)| (path, serde_json::from_str::<Value>(&json).unwrap()))
|
.map(|(path, json)| (path, serde_json::from_str::<Value>(&json).unwrap()))
|
||||||
.collect();
|
.collect();
|
||||||
|
let new_movies = metadata.keys().cloned().collect();
|
||||||
info!("Adding {} new videos", metadata.len());
|
info!("Adding {} new videos", metadata.len());
|
||||||
metadata.extend(old_metadata);
|
metadata.extend(old_metadata);
|
||||||
|
|
||||||
let f = File::create(Path::new(&self.root).join("metadata.json"))?;
|
let f = File::create(Path::new(&self.root).join("metadata.json"))?;
|
||||||
let f = BufWriter::new(f);
|
let f = BufWriter::new(f);
|
||||||
serde_json::ser::to_writer_pretty(f, &metadata)?;
|
serde_json::ser::to_writer_pretty(f, &metadata)?;
|
||||||
Ok(())
|
Ok(new_movies)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iter_video_files(&self) -> impl Send + Iterator<Item = Result<PathBuf, glob::GlobError>> {
|
fn iter_video_files(&self) -> impl Send + Iterator<Item = Result<PathBuf, glob::GlobError>> {
|
||||||
|
|||||||
18
src/main.rs
18
src/main.rs
@ -87,8 +87,13 @@ enum Command {
|
|||||||
about = "Read full metadata file and write compact file."
|
about = "Read full metadata file and write compact file."
|
||||||
)]
|
)]
|
||||||
CompactMetadata,
|
CompactMetadata,
|
||||||
#[structopt(name = "update-metadata", about = "Write metadata files")]
|
#[structopt(name = "update-metadata", about = "Write full metadata files")]
|
||||||
UpdateMetadata,
|
UpdateMetadata,
|
||||||
|
#[structopt(
|
||||||
|
name = "update-compact-metadata",
|
||||||
|
about = "Write full metadata files and update compact file on changes"
|
||||||
|
)]
|
||||||
|
UpdateAndCompactMetadata,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(StructOpt)]
|
#[derive(StructOpt)]
|
||||||
@ -151,6 +156,17 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let lib = MovieLibrary::new(MOVIE_DIR);
|
let lib = MovieLibrary::new(MOVIE_DIR);
|
||||||
lib.update_metadata()?;
|
lib.update_metadata()?;
|
||||||
}
|
}
|
||||||
|
Command::UpdateAndCompactMetadata => {
|
||||||
|
let lib = MovieLibrary::new(MOVIE_DIR);
|
||||||
|
let new_movies = lib.update_metadata()?;
|
||||||
|
if !new_movies.is_empty() {
|
||||||
|
info!(
|
||||||
|
"{} new movies added, recompacting metadata",
|
||||||
|
new_movies.len()
|
||||||
|
);
|
||||||
|
lib.compact_metadata()?;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user