From 22a8f3d619e8ef89eeb9a60ab0a27aed01aa93f7 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Sun, 27 Feb 2022 20:35:22 +0100 Subject: save all uploaded code bundles in database --- planetwars-server/src/modules/bots.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 planetwars-server/src/modules/bots.rs (limited to 'planetwars-server/src/modules/bots.rs') diff --git a/planetwars-server/src/modules/bots.rs b/planetwars-server/src/modules/bots.rs new file mode 100644 index 0000000..843e48d --- /dev/null +++ b/planetwars-server/src/modules/bots.rs @@ -0,0 +1,23 @@ +use std::path::PathBuf; + +use diesel::{PgConnection, QueryResult}; + +use crate::{db, util::gen_alphanumeric, BOTS_DIR}; + +pub fn save_code_bundle( + bot_code: &str, + bot_id: Option, + conn: &PgConnection, +) -> QueryResult { + let bundle_name = gen_alphanumeric(16); + + let code_bundle_dir = PathBuf::from(BOTS_DIR).join(&bundle_name); + std::fs::create_dir(&code_bundle_dir).unwrap(); + std::fs::write(code_bundle_dir.join("bot.py"), bot_code).unwrap(); + + let new_code_bundle = db::bots::NewCodeBundle { + bot_id, + path: &bundle_name, + }; + db::bots::create_code_bundle(&new_code_bundle, conn) +} -- cgit v1.2.3