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
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,6 +38,7 @@ async fn rocket() -> _ {
|
|||
route::manifest_redir,
|
||||
route::artist::artists_route,
|
||||
route::artist::artist_route,
|
||||
route::artist::artist_image_route,
|
||||
route::album::albums_route,
|
||||
route::album::album_route,
|
||||
route::track::track_route,
|
||||
|
|
|
@ -2,6 +2,7 @@ use super::api_error;
|
|||
use super::to_api;
|
||||
use super::FallibleApiResponse;
|
||||
use super::ToAPI;
|
||||
use fs::NamedFile;
|
||||
use mongod::Model;
|
||||
use mongodb::bson::doc;
|
||||
use rocket::*;
|
||||
|
@ -16,6 +17,12 @@ pub async fn artists_route(lib: &State<Libary>) -> FallibleApiResponse {
|
|||
Ok(serde_json::to_value(&to_api(&artists).await).unwrap())
|
||||
}
|
||||
|
||||
#[get("/artist/<id>/image")]
|
||||
pub async fn artist_image_route(id: &str) -> Option<NamedFile> {
|
||||
let image = Artist::get_image_of(id).await?;
|
||||
NamedFile::open(image).await.ok()
|
||||
}
|
||||
|
||||
#[get("/artist/<id>")]
|
||||
pub async fn artist_route(id: &str) -> FallibleApiResponse {
|
||||
Ok(Artist::get(id)
|
||||
|
|
Loading…
Reference in a new issue