feat: add image fetching

This commit is contained in:
2026-03-20 19:48:18 +01:00
parent fb62111c7f
commit 12388a9908
17 changed files with 2122 additions and 92 deletions

View File

@@ -0,0 +1,30 @@
use actix_web::{HttpResponse, get, web};
use crate::{consts, server};
// TODO: cache control
// TODO: etags
// TODO: canonical redirect for default image and better cache control
#[get("/image/{username}")]
pub async fn get_image(
data: web::Data<&server::AppState>,
username: web::Path<String>,
) -> HttpResponse {
let cached_pfp = data.cache.get_pfp(username.to_string()).await;
let (mime, bytes) = cached_pfp.as_ref().map_or_else(
|| {
(
consts::DEFAULT_USER_PFP_MIME,
web::Bytes::from_static(consts::DEFAULT_USER_PFP),
)
},
|img| {
(
img.mime.as_ref(),
web::Bytes::copy_from_slice(img.bytes.as_ref()),
)
},
);
HttpResponse::Ok().content_type(mime).body(bytes)
}