praying again

This commit is contained in:
Andrea Toska 2025-04-27 11:46:07 +02:00
commit f85085b445
No known key found for this signature in database
GPG key ID: 5B3C83807CCBE9A2

View file

@ -1,5 +1,6 @@
use std::env; use std::env;
use std::fs; use std::fs;
use std::fs::File;
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
use chrono::{NaiveDate, NaiveDateTime}; use chrono::{NaiveDate, NaiveDateTime};
@ -396,7 +397,6 @@ fn build_own_profile(user: User) -> Ds<Xml<GetOwnProfileData>> {
)) ))
} }
#[put("/v1/api/people/@me/miis/@primary", data = "<data>")] #[put("/v1/api/people/@me/miis/@primary", data = "<data>")]
pub async fn change_mii( pub async fn change_mii(
database: &State<Pool>, database: &State<Pool>,
@ -406,9 +406,10 @@ pub async fn change_mii(
) -> Result<(), Option<Errors<'static>>> { ) -> Result<(), Option<Errors<'static>>> {
let db = database.inner(); let db = database.inner();
let pid = auth.pid; let pid = auth.pid;
let mii_data = data.data.as_ref(); let mii_data = data.data.as_ref();
println!("Received new Mii data update for PID {}", pid);
let result = sqlx::query!( let result = sqlx::query!(
"UPDATE users SET mii_data = $1 WHERE pid = $2", "UPDATE users SET mii_data = $1 WHERE pid = $2",
mii_data, mii_data,
@ -417,10 +418,13 @@ pub async fn change_mii(
.execute(db) .execute(db)
.await; .await;
if result.is_err() { if let Err(e) = result {
println!("Failed to update Mii data for PID {}: {:?}", pid, e);
return Err(Some(DATABASE_ERROR)); return Err(Some(DATABASE_ERROR));
} }
println!("Successfully updated Mii data for PID {}", pid);
generate_mii_images(s3.client.clone(), "pn-cdn", pid, mii_data).await; generate_mii_images(s3.client.clone(), "pn-cdn", pid, mii_data).await;
Ok(()) Ok(())
@ -430,36 +434,39 @@ pub async fn generate_mii_images(client: Arc<Client>, bucket: &str, pid: i32, mi
let user_mii_key = format!("mii/{}", pid); let user_mii_key = format!("mii/{}", pid);
println!("Starting Mii image generation for PID {}", pid); println!("Starting Mii image generation for PID {}", pid);
async fn save_upload_delete( async fn save_and_upload(
client: &Client, client: &Client,
bucket: &str, bucket: &str,
key: &str, key: &str,
data: &[u8], data: &[u8],
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
let temp_path = format!("/tmp/{}", key.replace("/", "_")); let temp_path = format!("/tmp/{}", key.replace("/", "_"));
println!("Saving temporary file at {}", temp_path);
{ {
use std::fs::File;
let mut file = File::create(&temp_path)?; let mut file = File::create(&temp_path)?;
file.write_all(data)?; file.write_all(data)?;
file.flush()?; file.flush()?; // Make sure file is actually written
} }
println!("File saved, starting upload to S3...");
let content = ObjectContent::from(Path::new(&temp_path)); let content = ObjectContent::from(Path::new(&temp_path));
client.put_object_content(bucket, key, content).send().await?; client.put_object_content(bucket, key, content).send().await?;
fs::remove_file(&temp_path)?; println!("Uploaded {} to bucket {}", key, bucket);
// NOTE: DO NOT delete temp file for now
println!("(Skipping delete for {})", temp_path);
Ok(()) Ok(())
} }
// Upload normal face image // Upload normal face image
if let Some(png_data) = get_image_png(mii_data).await { if let Some(png_data) = get_image_png(mii_data).await {
println!("Fetched PNG for PID {}, uploading...", pid); println!("Fetched PNG for PID {}, preparing upload...", pid);
if let Err(e) = save_upload_delete(&client, bucket, &format!("{}/normal_face.png", user_mii_key), &png_data).await { if let Err(e) = save_and_upload(&client, bucket, &format!("{}/normal_face.png", user_mii_key), &png_data).await {
println!("Failed to upload normal_face.png for PID {}: {:?}", pid, e); println!("Failed to upload normal_face.png for PID {}: {:?}", pid, e);
} else {
println!("Uploaded normal_face.png for PID {}", pid);
} }
} else { } else {
println!("Failed to fetch PNG for PID {}", pid); println!("Failed to fetch PNG for PID {}", pid);
@ -467,11 +474,9 @@ pub async fn generate_mii_images(client: Arc<Client>, bucket: &str, pid: i32, mi
// Upload standard TGA // Upload standard TGA
if let Some(tga_data) = get_image_tga(mii_data).await { if let Some(tga_data) = get_image_tga(mii_data).await {
println!("Fetched TGA for PID {}, uploading...", pid); println!("Fetched TGA for PID {}, preparing upload...", pid);
if let Err(e) = save_upload_delete(&client, bucket, &format!("{}/standard.tga", user_mii_key), &tga_data).await { if let Err(e) = save_and_upload(&client, bucket, &format!("{}/standard.tga", user_mii_key), &tga_data).await {
println!("Failed to upload standard.tga for PID {}: {:?}", pid, e); println!("Failed to upload standard.tga for PID {}: {:?}", pid, e);
} else {
println!("Uploaded standard.tga for PID {}", pid);
} }
} else { } else {
println!("Failed to fetch TGA for PID {}", pid); println!("Failed to fetch TGA for PID {}", pid);
@ -495,11 +500,9 @@ pub async fn generate_mii_images(client: Arc<Client>, bucket: &str, pid: i32, mi
if let Ok(resp) = reqwest::get(&url).await { if let Ok(resp) = reqwest::get(&url).await {
if let Ok(bytes) = resp.bytes().await { if let Ok(bytes) = resp.bytes().await {
println!("Fetched expression '{}', uploading...", expression); println!("Fetched expression '{}', preparing upload...", expression);
if let Err(e) = save_upload_delete(&client, bucket, &format!("{}/{}.png", user_mii_key, expression), &bytes).await { if let Err(e) = save_and_upload(&client, bucket, &format!("{}/{}.png", user_mii_key, expression), &bytes).await {
println!("Failed to upload {}.png for PID {}: {:?}", expression, pid, e); println!("Failed to upload {}.png for PID {}: {:?}", expression, pid, e);
} else {
println!("Uploaded {}.png for PID {}", expression, pid);
} }
} else { } else {
println!("Failed to read bytes for expression '{}' for PID {}", expression, pid); println!("Failed to read bytes for expression '{}' for PID {}", expression, pid);
@ -518,11 +521,9 @@ pub async fn generate_mii_images(client: Arc<Client>, bucket: &str, pid: i32, mi
if let Ok(resp) = reqwest::get(&body_url).await { if let Ok(resp) = reqwest::get(&body_url).await {
if let Ok(bytes) = resp.bytes().await { if let Ok(bytes) = resp.bytes().await {
println!("Fetched body image, uploading..."); println!("Fetched body image, preparing upload...");
if let Err(e) = save_upload_delete(&client, bucket, &format!("{}/body.png", user_mii_key), &bytes).await { if let Err(e) = save_and_upload(&client, bucket, &format!("{}/body.png", user_mii_key), &bytes).await {
println!("Failed to upload body.png for PID {}: {:?}", pid, e); println!("Failed to upload body.png for PID {}: {:?}", pid, e);
} else {
println!("Uploaded body.png for PID {}", pid);
} }
} else { } else {
println!("Failed to read body image bytes for PID {}", pid); println!("Failed to read body image bytes for PID {}", pid);
@ -538,3 +539,4 @@ pub async fn generate_mii_images(client: Arc<Client>, bucket: &str, pid: i32, mi