perf: use const-str's include_asset!

This commit is contained in:
2026-03-24 22:31:12 +01:00
parent 8a65bb2c92
commit 70f77f3501
4 changed files with 67 additions and 32 deletions

View File

@@ -2,6 +2,8 @@
use std::time::Duration;
use const_macros::{file::FileAsset, include_asset};
/// Max [`moka`] pfp cache capacity
pub const MAX_PFP_CACHE_CAPACITY: u64 = 1024;
@@ -9,8 +11,7 @@ pub const MAX_PFP_CACHE_CAPACITY: u64 = 1024;
pub const MAX_PFP_SIZE: u64 = 8 * 1024 * 1024; // 8 MiB; TODO: might lower, high for prototyping
/// Default user image to use if users have none
pub const DEFAULT_USER_PFP: &[u8] = include_bytes!("../assets/default-pfp.png");
pub const DEFAULT_USER_PFP_MIME: &str = "image/png; charset=binary";
pub const DEFAULT_USER_PFP: FileAsset = include_asset!("assets/default-pfp.png");
/// Basically [`USER_CACHES_HEADER`] but much longer for the [`DEFAULT_USER_PFP`]
pub const DEFAULT_USER_PFP_CACHES_HEADER: &str =
crate::utils::web::make_static_cache_header!(Duration::from_hours(2), Duration::from_days(1));

View File

@@ -1,6 +1,6 @@
//! Scope for the image get backend.
//! - `/`: Gives the default image.
//! - `/{username}`: Gives username's pfp or redirects to the default image's path (for better
//! - `/default`: Gives the default image.
//! - `/user/{username}`: Gives username's pfp or redirects to the default image's path (for better
//! cache control) if there's no image.
//!
//! Must be scoped at [`ws::IMAGES`]
@@ -15,7 +15,7 @@ use actix_web::{
use crate::{
consts::{self, web_scopes as ws},
server::{self, AppData},
server::AppData,
};
#[must_use]
@@ -25,21 +25,18 @@ pub fn make_scope(path: &str) -> actix_web::Scope {
.service(get_image)
}
#[get("/")]
async fn get_default_image(
data: AppData,
_username: web::Path<String>,
) -> HttpResponse {
#[get("/default")]
async fn get_default_image() -> HttpResponse {
HttpResponse::Ok()
.insert_header((
header::CACHE_CONTROL,
consts::DEFAULT_USER_PFP_CACHES_HEADER,
))
.content_type(consts::DEFAULT_USER_PFP_MIME)
.body(web::Bytes::from_static(consts::DEFAULT_USER_PFP))
.content_type(consts::DEFAULT_USER_PFP.mime)
.body(web::Bytes::from_static(consts::DEFAULT_USER_PFP.bytes))
}
#[get("/{username}")]
#[get("/user/{username}")]
async fn get_image(
data: AppData,
username: web::Path<String>,
@@ -47,7 +44,11 @@ async fn get_image(
let cached_pfp = data.cache.get_pfp(username.to_string()).await;
cached_pfp.as_ref().map_or_else(
|| web::Either::Left(web::Redirect::to(ws::IMAGES).temporary()),
|| {
web::Either::Left(
web::Redirect::to(const_str::concat!(ws::IMAGES, "/default")).temporary(),
)
},
|img| {
web::Either::Right(
HttpResponse::Ok()