update
This commit is contained in:
parent
a5d3c14f0c
commit
11a862f6c7
6 changed files with 158 additions and 1 deletions
|
@ -166,6 +166,127 @@ impl Libary {
|
|||
Track::get(track_id).await
|
||||
}
|
||||
|
||||
pub async fn reload_metadata(&self, track_id: &str) -> Result<(), ()> {
|
||||
let mut track = Track::get(track_id).await.ok_or_else(|| ())?;
|
||||
let path = &track.path;
|
||||
log::info!("Rescanning metadata for {path}");
|
||||
|
||||
let metadata = metadata::get_metadata(path);
|
||||
|
||||
let mut update = json!({});
|
||||
|
||||
if let Some(meta) = &metadata {
|
||||
if let Some(artist) = meta.artist() {
|
||||
let artist_id = self.find_or_create_artist(artist).await;
|
||||
update
|
||||
.as_object_mut()
|
||||
.unwrap()
|
||||
.insert("artist_id".into(), artist_id.into());
|
||||
} else {
|
||||
log::warn!("{path} has no artist");
|
||||
}
|
||||
|
||||
if let Some(album) = meta.album() {
|
||||
let album_id = self
|
||||
.find_or_create_album(
|
||||
album,
|
||||
update
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.get("artist_id")
|
||||
.map(|x| x.as_str().unwrap()),
|
||||
)
|
||||
.await;
|
||||
update
|
||||
.as_object_mut()
|
||||
.unwrap()
|
||||
.insert("album_id".into(), album_id.into());
|
||||
} else {
|
||||
log::warn!("{path} has no album and will be treated as single");
|
||||
}
|
||||
|
||||
if let Some(title) = meta.title() {
|
||||
update
|
||||
.as_object_mut()
|
||||
.unwrap()
|
||||
.insert("title".into(), title.into());
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(metadata) = metadata {
|
||||
update
|
||||
.as_object_mut()
|
||||
.unwrap()
|
||||
.insert("meta".into(), metadata.0);
|
||||
}
|
||||
|
||||
// if no title in metadata use file name
|
||||
if update.as_object().unwrap().get("title").is_none() {
|
||||
update.as_object_mut().unwrap().insert(
|
||||
"title".into(),
|
||||
std::path::Path::new(&path)
|
||||
.file_stem()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
|
||||
track.update(&update).await.unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn clean_lost_files(&self) {
|
||||
// todo : clean
|
||||
// tracks
|
||||
for track in Track::find(doc! {}, None).await.unwrap() {
|
||||
if !std::path::Path::new(&track.path).exists() {
|
||||
log::info!("Cleaning lost {}", track.path);
|
||||
track.delete().await.unwrap();
|
||||
}
|
||||
}
|
||||
// albums
|
||||
for album in Album::find_partial(doc! {}, json!({"title": 1}), None)
|
||||
.await
|
||||
.unwrap()
|
||||
{
|
||||
if Track::find_partial(doc! { "album_id": album.reference() }, json!({}), None)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
{
|
||||
log::info!(
|
||||
"Cleaning album {} with no tracks",
|
||||
album.title.as_ref().unwrap()
|
||||
);
|
||||
Album::remove(album.id()).await.unwrap();
|
||||
}
|
||||
}
|
||||
// artists
|
||||
for artist in Artist::find_partial(doc! {}, json!({"name": 1}), None)
|
||||
.await
|
||||
.unwrap()
|
||||
{
|
||||
if Track::find_partial(doc! { "artist_id": artist.reference()}, json!({}), None)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
&& Album::find_partial(doc! { "artist_id": artist.reference()}, json!({}), None)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_empty()
|
||||
{
|
||||
log::info!(
|
||||
"Cleaning artist {} with no tracks or albums",
|
||||
artist.name.as_ref().unwrap()
|
||||
);
|
||||
Artist::remove(artist.id()).await.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn rescan(&self, cache: &RouteCache) {
|
||||
cache.invalidate("albums", "latest").await;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue