From 96cd6ed0ef2175e911f64ebde3bca3a1b3ea998b Mon Sep 17 00:00:00 2001 From: JMARyA Date: Wed, 7 Aug 2024 23:07:08 +0200 Subject: [PATCH] artist image --- src/library/artist.rs | 31 +++++++++++++++++++++++++++++-- src/main.rs | 1 + src/route/artist.rs | 7 +++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/library/artist.rs b/src/library/artist.rs index 63a21fd..ad8273f 100644 --- a/src/library/artist.rs +++ b/src/library/artist.rs @@ -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 { + 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 + } }) } } diff --git a/src/main.rs b/src/main.rs index d5effd1..fab5cb7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, diff --git a/src/route/artist.rs b/src/route/artist.rs index b50a567..9144fad 100644 --- a/src/route/artist.rs +++ b/src/route/artist.rs @@ -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) -> FallibleApiResponse { Ok(serde_json::to_value(&to_api(&artists).await).unwrap()) } +#[get("/artist//image")] +pub async fn artist_image_route(id: &str) -> Option { + let image = Artist::get_image_of(id).await?; + NamedFile::open(image).await.ok() +} + #[get("/artist/")] pub async fn artist_route(id: &str) -> FallibleApiResponse { Ok(Artist::get(id)