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/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 166cae4..bf6ec5d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,9 @@ +#![feature(async_closure)] + use config::Config; use db::Database; +use error::Error; +use tokio::{fs::File, io::AsyncWriteExt}; mod artist; mod artwork; @@ -25,6 +29,20 @@ impl Critch { Self { db, config } } + + pub async fn save_file(&self, file_name: &str, file_data: Vec) -> Result<()> { + let upload_dir = self.config.files_dir(); + if upload_dir.is_dir() { + let file_handle = upload_dir.join(file_name); + let mut file = File::create(file_handle).await?; + file.write_all(&file_data).await?; + return Ok(()); + } else { + return Err(Error::UploadDirectory( + self.config.files_dir().to_string_lossy().to_string(), + )); + } + } } include!(concat!(env!("OUT_DIR"), "/templates.rs")); -- cgit