this repo has no description
1use crate::state::AppState;
2use axum::{
3 body::Body,
4 extract::State,
5 http::StatusCode,
6 http::header,
7 response::{IntoResponse, Response},
8};
9use tracing::error;
10
11pub async fn get_logo(State(state): State<AppState>) -> Response {
12 let logo_cid: Option<String> =
13 match sqlx::query_scalar("SELECT value FROM server_config WHERE key = 'logo_cid'")
14 .fetch_optional(&state.db)
15 .await
16 {
17 Ok(cid) => cid,
18 Err(e) => {
19 error!("DB error fetching logo_cid: {:?}", e);
20 return StatusCode::INTERNAL_SERVER_ERROR.into_response();
21 }
22 };
23
24 let cid = match logo_cid {
25 Some(c) if !c.is_empty() => c,
26 _ => return StatusCode::NOT_FOUND.into_response(),
27 };
28
29 let blob = match sqlx::query!(
30 "SELECT storage_key, mime_type FROM blobs WHERE cid = $1",
31 cid
32 )
33 .fetch_optional(&state.db)
34 .await
35 {
36 Ok(Some(row)) => row,
37 Ok(None) => return StatusCode::NOT_FOUND.into_response(),
38 Err(e) => {
39 error!("DB error fetching blob: {:?}", e);
40 return StatusCode::INTERNAL_SERVER_ERROR.into_response();
41 }
42 };
43
44 match state.blob_store.get(&blob.storage_key).await {
45 Ok(data) => Response::builder()
46 .status(StatusCode::OK)
47 .header(header::CONTENT_TYPE, &blob.mime_type)
48 .header(header::CACHE_CONTROL, "public, max-age=3600")
49 .body(Body::from(data))
50 .unwrap(),
51 Err(e) => {
52 error!("Failed to fetch logo from storage: {:?}", e);
53 StatusCode::NOT_FOUND.into_response()
54 }
55 }
56}