From 08d3682164e2737fd60dfe0dc7731a8fc5cfd977 Mon Sep 17 00:00:00 2001 From: Alexandertp Date: Tue, 10 Sep 2024 11:52:27 +0200 Subject: [PATCH] Implement image uploading in rust Co-authored-by: Reimar --- API/Persistence/Services/.R2Service.cs.swp | Bin 12288 -> 0 bytes rust-backend/.env.example | 7 ++- rust-backend/src/main.rs | 47 ++++++++++++++++++++- rust-backend/src/models.rs | 2 +- 4 files changed, 52 insertions(+), 4 deletions(-) delete mode 100644 API/Persistence/Services/.R2Service.cs.swp diff --git a/API/Persistence/Services/.R2Service.cs.swp b/API/Persistence/Services/.R2Service.cs.swp deleted file mode 100644 index 6f73e76de95da7ffd5dbfcd6e9008396a1c4ad4e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2&u<$=6vw9^fhd%pLgL2M-N8Gp|Bm@WUoH@Y10Cx(4OOL<_gv58ezp@S`XJA(PW`E3^+4ny0%}Q3@p#IA4 z9&K55g5z;QzI)(%_x(H1kT32hM6$pU`uQnGe_de72W$e@>_>i^=!e-cWl=stx9Lh{q|`)Ss?{d(;NrmDthWXm&9!=S ztGU(UY_slev>F?m4bc$wCbyz>OSgKCniG%f*3d!B4}_}CUC$Rc*jOA@+Jt1U(&xg* z?5P?>;UNvgA?=LW+hJfguD->^M0sIQU8!7+qA;TI#1EO{1w*PArYfX%!=l&v{Vuf? zdQv6b2_12X&LbfwVIYNd4N)XRdgQ2bp&a#`WUtp}&!yFS%%tbHsXas2>e#v=)Y7un z&FdG+;c1%LN}$OOnWyM2+1T+t5h$x0tDA#21Xmq73pn2SW-O$FZzXq*%D!q3U-Tp! z_@c{Z8ief)0}W%Fswftvx!U-;m|-YO!Rm?eD~lxQ^uVmLl)K7F``us?D_f!acbu-9 z+>|ZLND~l?@GwVbH;=uSM4h`;Ohn1~9;Zyhp?xL~UXUt6pqJ_Xq5%3~Z@rp2qQgtw zpovu;@v)qg%Pmht>QYv4Np`9{5)QKDF<-7y9tN&ATq>Kt$;?Y(#E};@vegWwd5@H& zP0sthmtm1_F}6{Ee$5#SM4e%yHMh2y>ozwBEw|~kT)wv0Ufd63$MqSiN`(=2u*Jji z@`hVVT0vBogHA;f&2rd_5c53bwgrZC!WAYx-g(tV19%3g$~fA$B}#3TN&#{Tm&=80 zX9HL#H!2r*CtC(4`I3c65lLDbdB1oyO?o~Fek_Wmc+;^uNN1-Og, path: web: } } +#[derive(Deserialize)] +struct CreateImageQuery { + file_name: String, +} + +#[post("/images")] +async fn create_image(auth: AuthorizedUser, data: web::Data, bytes: Bytes, query: web::Query) -> impl Responder { + let db = data.database.lock().unwrap(); + let config = aws_config::load_from_env().await; + let client = aws_sdk_s3::Client::new(&config); + + let bucket_name = std::env::var("R2_BUCKET_NAME").expect("R2_BUCKET_NAME must be provided"); + let bucket_url = std::env::var("R2_BUCKET_URL").expect("R2_BUCKET_URL must be provided"); + + let response = client.put_object() + .bucket(bucket_name.clone()) + .key(query.file_name.clone()) + .body(ByteStream::from(bytes.to_vec())) + .send() + .await; + + if response.is_err() { + return HttpResponse::InternalServerError().finish(); + } + + let image_url = format!("{}/{}/{}", bucket_url, bucket_name, query.file_name); + + match db.execute( + "INSERT INTO images (user_id, image_url)", + &[ + (":user_id", &auth.user_id), + (":image_url", &image_url), + ], + ) { + Ok(_) => HttpResponse::Created().json(Image { + id: db.last_insert_rowid(), + user_id: auth.user_id, + image_url: image_url, + }), + Err(_) => HttpResponse::InternalServerError().finish(), + } +} #[actix_web::main] async fn main() -> std::io::Result<()> { diff --git a/rust-backend/src/models.rs b/rust-backend/src/models.rs index 1c1bf3c..7b3b1e0 100644 --- a/rust-backend/src/models.rs +++ b/rust-backend/src/models.rs @@ -62,7 +62,7 @@ pub struct Image { } impl Image { - pub fn from_row(row: &Row) -> Resul { + pub fn from_row(row: &Row) -> Result { Ok(Image { id: row.get("id")?, user_id: row.get("user_id")?,