artist image
This commit is contained in:
parent
92df85a4bb
commit
96cd6ed0ef
3 changed files with 37 additions and 2 deletions
|
@ -1,12 +1,15 @@
|
|||
use mongod::{
|
||||
derive::{Model, Referencable},
|
||||
Model, Validate,
|
||||
reference_of, Model, Referencable, Validate,
|
||||
};
|
||||
use mongodb::bson::doc;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::route::ToAPI;
|
||||
|
||||
use super::track::Track;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)]
|
||||
pub struct Artist {
|
||||
pub _id: String,
|
||||
|
@ -22,13 +25,37 @@ impl Artist {
|
|||
a.insert().await.unwrap();
|
||||
a
|
||||
}
|
||||
|
||||
pub async fn get_image_of(id: &str) -> Option<String> {
|
||||
let track_path = Track::find_one(doc! { "artist_id": reference_of!(Artist, id)})
|
||||
.await?
|
||||
.path;
|
||||
let track_path = std::path::Path::new(&track_path);
|
||||
|
||||
let artist_path = track_path.parent()?.parent()?;
|
||||
|
||||
for ext in ["png", "jpg", "jpeg", "avif"] {
|
||||
let cover_file = artist_path.join(format!("artist.{ext}"));
|
||||
|
||||
if cover_file.exists() {
|
||||
return Some(cover_file.to_str().unwrap().to_string());
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl ToAPI for Artist {
|
||||
async fn api(&self) -> serde_json::Value {
|
||||
json!({
|
||||
"id": &self._id,
|
||||
"name": &self.name
|
||||
"name": &self.name,
|
||||
"image": if Artist::get_image_of(self.id()).await.is_some() {
|
||||
Some(format!("/artist/{}/image", self.id()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue