feat: Implement CachedOption for get_game_thumbnail response, trim game titles, and update toolchain to nightly.

This commit is contained in:
2025-12-04 16:12:47 +01:00
parent 70e1a555c0
commit 1ff7e1dd83
4 changed files with 84 additions and 11 deletions
+13 -7
View File
@@ -1,3 +1,4 @@
#![feature(try_trait_v2)]
use rocket::fs::FileServer;
use rocket::futures::lock::Mutex;
@@ -62,12 +63,14 @@ async fn add_game(
game: proto_utils::Proto<items::Game>,
) -> Option<items::Game> {
let mut games = game_list.lock().await;
let game = game.into_inner();
let mut game = game.into_inner();
if games.iter().any(|g| g.title == game.title) {
return None;
}
game.title = game.title.trim().to_string();
games.push(game.clone());
let users = user_list.lock().await;
@@ -118,11 +121,14 @@ async fn add_opinion(
result
}
mod cached_option;
use cached_option::CachedOption;
#[get("/game_thumbnail/<title>")]
async fn get_game_thumbnail(
title: &str,
game_list: &rocket::State<Mutex<Vec<Game>>>,
) -> Option<(rocket::http::ContentType, Vec<u8>)> {
) -> CachedOption {
// 1. Sanitize title for filename
let safe_title: String = title
.chars()
@@ -140,7 +146,7 @@ async fn get_game_thumbnail(
std::fs::read_to_string(&cache_path_type),
) && let Some(ct) = rocket::http::ContentType::parse_flexible(&type_str)
{
return Some((ct, bytes));
return Some((ct, bytes)).into();
}
let games = game_list.lock().await;
@@ -177,10 +183,10 @@ async fn get_game_thumbnail(
if let Ok(json) = resp.json::<serde_json::Value>().await {
json["data"][0]["imageUrl"].as_str()?.to_string()
} else {
return None;
return None.into();
}
}
Err(_) => return None,
Err(_) => return None.into(),
}
}
};
@@ -199,9 +205,9 @@ async fn get_game_thumbnail(
let _ = std::fs::write(&cache_path_bin, &bytes);
let _ = std::fs::write(&cache_path_type, content_type.to_string());
Some((content_type, bytes))
Some((content_type, bytes)).into()
}
Err(_) => None,
Err(_) => None.into(),
}
}