diff options
author | metamuffin <metamuffin@disroot.org> | 2023-10-27 22:10:48 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-10-27 22:10:48 +0200 |
commit | 6b5fbb8c9864f370ef5daf870518f9d3c48f46c5 (patch) | |
tree | 39748593f078402ae96d1e5f30a4bd8fabb8cc86 /server | |
parent | 3e88163603ceefba6672635973fc9658e53f6a4a (diff) | |
download | jellything-6b5fbb8c9864f370ef5daf870518f9d3c48f46c5.tar jellything-6b5fbb8c9864f370ef5daf870518f9d3c48f46c5.tar.bz2 jellything-6b5fbb8c9864f370ef5daf870518f9d3c48f46c5.tar.zst |
error images
Diffstat (limited to 'server')
-rw-r--r-- | server/src/routes/ui/assets.rs | 4 | ||||
-rw-r--r-- | server/src/routes/ui/error.rs | 33 |
2 files changed, 23 insertions, 14 deletions
diff --git a/server/src/routes/ui/assets.rs b/server/src/routes/ui/assets.rs index 5789685..48c569a 100644 --- a/server/src/routes/ui/assets.rs +++ b/server/src/routes/ui/assets.rs @@ -8,7 +8,7 @@ use crate::{ routes::ui::{account::session::Session, error::MyError, CacheControlFile}, }; use anyhow::{anyhow, Context}; -use jellybase::{AssetLocationExt, permission::NodePermissionExt}; +use jellybase::{permission::NodePermissionExt, AssetLocationExt}; use jellycommon::AssetLocation; use log::info; use rocket::{get, http::ContentType, FromFormField, State, UriDisplayQuery}; @@ -50,7 +50,7 @@ pub async fn r_item_assets( } }; let asset = asset.unwrap_or(AssetLocation::Assets( - PathBuf::from_str("fallback.jpeg").unwrap(), + PathBuf::from_str("fallback.avif").unwrap(), )); // fit the resolution into a finite set so the maximum cache is finite too. let width = 2usize.pow(width.unwrap_or(2048).clamp(128, 8196).ilog2()); diff --git a/server/src/routes/ui/error.rs b/server/src/routes/ui/error.rs index a4ef50c..07a6bed 100644 --- a/server/src/routes/ui/error.rs +++ b/server/src/routes/ui/error.rs @@ -5,15 +5,26 @@ */ use super::layout::{DynLayoutPage, LayoutPage}; use crate::{routes::ui::account::rocket_uri_macro_r_account_login, uri}; -use jellybase::database::sled; +use jellybase::{database::sled, AssetLocationExt}; +use jellycommon::AssetLocation; +use log::info; use rocket::{ catch, - http::{MediaType, Status}, + http::{ContentType, Status}, response::{self, Responder}, Request, }; use serde_json::{json, Value}; -use std::fmt::Display; +use std::{fmt::Display, fs::File, io::Read, path::PathBuf, str::FromStr, sync::LazyLock}; + +static ERROR_IMAGE: LazyLock<Vec<u8>> = LazyLock::new(|| { + info!("loading error image"); + let mut f = File::open(AssetLocation::Assets(PathBuf::from_str("error.avif").unwrap()).path()) + .expect("please create error.avif in the asset dir"); + let mut o = Vec::new(); + f.read_to_end(&mut o).unwrap(); + o +}); #[catch(default)] pub fn r_catch<'a>(status: Status, _request: &Request) -> DynLayoutPage<'a> { @@ -41,14 +52,12 @@ pub struct MyError(pub anyhow::Error); impl<'r> Responder<'r, 'static> for MyError { fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> { - if req - .accept() - .map(|a| a.preferred().exact_eq(&MediaType::JSON)) - .unwrap_or(true) - { - json!({ "error": format!("{}", self.0) }).respond_to(req) - } else { - LayoutPage { + match req.accept().map(|a| a.preferred()) { + Some(x) if x.is_json() => json!({ "error": format!("{}", self.0) }).respond_to(req), + Some(x) if x.is_avif() || x.is_png() || x.is_jpeg() => { + (ContentType::AVIF, ERROR_IMAGE.as_slice()).respond_to(req) + } + _ => LayoutPage { title: "Error".to_string(), content: markup::new! { h2 { "An error occured. Nobody is sorry"} @@ -56,7 +65,7 @@ impl<'r> Responder<'r, 'static> for MyError { }, ..Default::default() } - .respond_to(req) + .respond_to(req), } } } |