From 67b54449a1bbde257e9454419e7bb70ebc515c0f Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Thu, 14 Nov 2024 21:43:54 +0000 Subject: implement artwork upload --- src/routes/admin.rs | 2 +- src/routes/artworks.rs | 45 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 4 deletions(-) (limited to 'src/routes') diff --git a/src/routes/admin.rs b/src/routes/admin.rs index ccca2de..7ba63ac 100644 --- a/src/routes/admin.rs +++ b/src/routes/admin.rs @@ -19,7 +19,7 @@ pub async fn get_dashboard(session: &Session, critch: Data<&Critch>) -> Result, +) -> Result { + if let Some(true) = session.get("is_admin") { + let (mut title, mut handle, mut url_source, mut description, mut files) = + (None, None, None, None, Vec::new()); + while let Some(field) = multipart.next_field().await? { + match field.name() { + Some("title") => title = Some(field.text().await?), + Some("artist") => handle = Some(field.text().await?), + Some("url") => url_source = Some(field.text().await?), + Some("description") => description = Some(field.text().await?), + Some("file") => { + let content_type = field.content_type().ok_or(Error::BadRequest)?.to_owned(); + let file_data = field.bytes().await?; + let file = File::new(&content_type)?; + let file_name = format!("{}.{}", file.file_id(), file.extension()); + critch.save_file(&file_name, file_data).await?; + files.push(file); + } + _ => return Err(Error::BadRequest), + } + } + let artist = Artist::new(handle.ok_or(Error::BadRequest)?); + let artwork = Artwork::new(title, description, url_source, artist, files); + critch.db.artworks().create(artwork).await?; + println!("saved file"); + return Ok(Redirect::see_other("/admin").into_response()); + } else { + return Err(Error::Unauthorized); + } } #[handler] -- cgit