2022-02-27 20:35:22 +01:00
|
|
|
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<i32>,
|
|
|
|
conn: &PgConnection,
|
2022-07-06 22:41:27 +02:00
|
|
|
) -> QueryResult<db::bots::BotVersion> {
|
2022-02-27 20:35:22 +01:00
|
|
|
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,
|
2022-07-05 20:34:20 +02:00
|
|
|
code_bundle_path: &bundle_name,
|
2022-02-27 20:35:22 +01:00
|
|
|
};
|
|
|
|
db::bots::create_code_bundle(&new_code_bundle, conn)
|
|
|
|
}
|