All checks were successful
Build and Test / cache-mii (push) Successful in 2m30s
132 lines
4.2 KiB
Rust
132 lines
4.2 KiB
Rust
use dotenvy::dotenv;
|
|
use rocket::fs::NamedFile;
|
|
use rocket::State;
|
|
use reqwest::Client;
|
|
use bytes::Bytes;
|
|
|
|
static MII_RENDERER_URL: &str = "https://mii-fwd.spfn.net/";
|
|
|
|
async fn request_mii_render(client: &State<Client>, pid: i32, path: &str) -> Option<Bytes> {
|
|
let req_str = format!("{MII_RENDERER_URL}/{pid}/{path}");
|
|
|
|
let response = client.get(req_str)
|
|
.send()
|
|
.await;
|
|
|
|
match response {
|
|
Ok(res) => {
|
|
let bytes = res.bytes().await;
|
|
match bytes {
|
|
Ok(bytes) => {
|
|
Some(bytes)
|
|
},
|
|
Err(_) => {
|
|
println!("Error Getting Response Bytes");
|
|
None
|
|
}
|
|
}
|
|
}
|
|
Err(err) => {
|
|
let status = match err.status() {
|
|
Some(err_status) => {
|
|
err_status.to_string()
|
|
}
|
|
None => String::from("No Response")
|
|
};
|
|
println!("Error Requesting New Render - {}", status);
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
#[rocket::get("/<pid>/<path>")]
|
|
pub async fn mii_render(pid: i32, path: &str, client: &State<Client>) -> Option<NamedFile> {
|
|
let mii_data = {
|
|
let route = format!("https://account.spfn.net/api/v2/users/{pid}/mii");
|
|
let response = client.get(route)
|
|
.send()
|
|
.await;
|
|
|
|
let response = match response {
|
|
Ok(res) => res,
|
|
Err(e) => {
|
|
println!("Error Fetching Mii Data: {e}");
|
|
return None;
|
|
}
|
|
};
|
|
|
|
let data: String = match response.json().await {
|
|
Ok(data) => data,
|
|
Err(_) => return None,
|
|
};
|
|
|
|
data
|
|
};
|
|
|
|
let cache_path = std::path::Path::new("miis/").join(pid.to_string());
|
|
let img_path = cache_path.clone().join(path);
|
|
let data_path = cache_path.clone().join("data");
|
|
|
|
// Update Cache
|
|
if cache_path.exists() && cache_path.is_dir() { // Mii data was previously cached - Verify it is correct
|
|
if data_path.exists() && data_path.is_file() { // Data exists - Check if it's the same
|
|
let cache_data = std::fs::read_to_string(&data_path);
|
|
|
|
match cache_data {
|
|
Ok(hash) => {
|
|
if hash == mii_data { // Cached images are correct
|
|
if !img_path.exists() || !img_path.is_file() { // File type was not found - Request data
|
|
let img_data = request_mii_render(client, pid, path).await?;
|
|
let _ = std::fs::write(&img_path, &img_data);
|
|
}
|
|
} else { // Cached images are outdated - Update data
|
|
let img_data = request_mii_render(client, pid, path).await?;
|
|
let _ = std::fs::write(&img_path, &img_data);
|
|
};
|
|
}
|
|
|
|
Err(_) => { // Data cannot be read - Request data
|
|
let _ = std::fs::write(data_path, &mii_data);
|
|
|
|
let img_data = request_mii_render(client, pid, path).await?;
|
|
let _ = std::fs::write(&img_path, &img_data);
|
|
}
|
|
}
|
|
} else { // Data isn't saved - Request data
|
|
let _ = std::fs::write(data_path, &mii_data);
|
|
|
|
let img_file = request_mii_render(client, pid, path).await?;
|
|
let _ = std::fs::write(&img_path, &img_file);
|
|
}
|
|
} else { // Mii data was never cached - Request data
|
|
let _ = std::fs::create_dir(format!("miis/{}", pid.to_string()));
|
|
|
|
let _ = std::fs::write(data_path, &mii_data);
|
|
|
|
let img_data = request_mii_render(client, pid, path).await?;
|
|
let _ = std::fs::write(&img_path, &img_data);
|
|
};
|
|
|
|
let img = match NamedFile::open(img_path).await {
|
|
Ok(img) => img,
|
|
Err(_) => return None
|
|
};
|
|
|
|
Some(img)
|
|
}
|
|
|
|
#[rocket::launch]
|
|
async fn launch() -> _ {
|
|
dotenv().ok();
|
|
|
|
let client = Client::new();
|
|
|
|
let mii_path = std::path::Path::new("miis");
|
|
if !mii_path.exists() || !mii_path.is_dir() {
|
|
let _ = std::fs::create_dir("miis");
|
|
}
|
|
|
|
rocket::build()
|
|
.manage(client)
|
|
.mount("/", rocket::routes![mii_render])
|
|
}
|