diff options
author | alyx <alyx@aleteoryx.me> | 2024-01-12 01:28:22 -0500 |
---|---|---|
committer | alyx <alyx@aleteoryx.me> | 2024-01-12 01:28:22 -0500 |
commit | 7e4da5f0de16c35ce304620bd37b08d57ff46858 (patch) | |
tree | 849787161f7ad0d8c4ff40c5531896b93e9c8578 /src/ctx.rs | |
parent | b53cb296fc5a0e2c77c6074e6cc5cde45a290cdd (diff) | |
download | lfm_embed-7e4da5f0de16c35ce304620bd37b08d57ff46858.tar.gz lfm_embed-7e4da5f0de16c35ce304620bd37b08d57ff46858.tar.bz2 lfm_embed-7e4da5f0de16c35ce304620bd37b08d57ff46858.zip |
Custom font support complete
Diffstat (limited to 'src/ctx.rs')
-rw-r--r-- | src/ctx.rs | 35 |
1 files changed, 26 insertions, 9 deletions
@@ -3,6 +3,7 @@ use super::deserialize as de; use std::sync::Arc; use std::collections::BTreeMap; use super::font::FontQuery; +use super::config::STATE; pub mod model { use std::sync::Arc; @@ -70,7 +71,7 @@ pub mod model { #[derive(serde::Serialize, Debug)] #[serde(untagged)] pub enum Font { - Url { css: Arc<str> }, + External { css: Arc<str>, name: Arc<str> }, Name { name: Arc<str> }, } @@ -89,10 +90,9 @@ pub mod model { #[derive(Debug)] pub struct ResponseCtx(pub model::Data, pub StatusCode); -impl From<(Result<Arc<(de::User, de::Track)>, (StatusCode, &'static str)>, Option<FontQuery>, BTreeMap<String, String>)> for ResponseCtx { - fn from(v: (Result<Arc<(de::User, de::Track)>, (StatusCode, &'static str)>, Option<FontQuery>, BTreeMap<String, String>)) -> ResponseCtx { - let (v, f, q) = v; - match v { +impl ResponseCtx { + pub async fn create(api_result: Result<Arc<(de::User, de::Track)>, (StatusCode, &'static str)>, font_query: Option<FontQuery>, query: BTreeMap<String, String>) -> ResponseCtx { + match api_result { Ok(a) => { let (user, track) = a.as_ref(); ResponseCtx(model::Data::Data { @@ -123,12 +123,29 @@ impl From<(Result<Arc<(de::User, de::Track)>, (StatusCode, &'static str)>, Optio url: track.url.clone(), loved: track.loved.unwrap_or(false) }, - font: match f { - Some(FontQuery { font: Some(s), include_font: None }) => Some(model::Font::Name { name: s }), - Some(FontQuery { include_font: Some(s), .. }) => Some(model::Font::Url { css: format!("@font-face {{ font-family: 'included_font'; src: url('{}'); }}", s.replace("\\", "\\\\").replace("'", "\\'")).into() }), + font: match font_query { + Some(FontQuery { google_font: Some(f), .. }) if STATE.has_google_api_key() => { + let css = match STATE.get_fontinfo(&f.to_string()).await { + Ok(css) => css, + Err((status, error)) => { return ResponseCtx(model::Data::Error {error}, status); } + }; + Some(model::Font::External { + css, + name: f + }) + }, + Some(FontQuery { include_font: Some(f), .. }) => Some( + model::Font::External { + css: format!( + "@font-face {{ font-family: 'included_font'; src: url('{}'); }}", + f.replace("\\", "\\\\") + .replace("'", "\\'")).into(), + name: "included_font".into() + }), + Some(FontQuery { font: Some(s), .. }) => Some(model::Font::Name { name: s }), _ => None, }, - query: q + query }, StatusCode::OK) }, Err((status, error)) => { |