allow retrieving bot code

This commit is contained in:
Ilion Beyst 2022-09-10 18:56:03 +02:00
parent 78a6032b60
commit 3058028edc
2 changed files with 27 additions and 0 deletions

View file

@ -127,6 +127,7 @@ pub fn api() -> Router {
"/bots/:bot_name/upload", "/bots/:bot_name/upload",
post(routes::bots::upload_code_multipart), post(routes::bots::upload_code_multipart),
) )
.route("/code/:version_id", get(routes::bots::get_code))
.route("/matches", get(routes::matches::list_recent_matches)) .route("/matches", get(routes::matches::list_recent_matches))
.route("/matches/:match_id", get(routes::matches::get_match_data)) .route("/matches/:match_id", get(routes::matches::get_match_data))
.route( .route(

View file

@ -249,3 +249,29 @@ pub async fn upload_code_multipart(
Ok(Json(code_bundle)) Ok(Json(code_bundle))
} }
pub async fn get_code(
conn: DatabaseConnection,
user: User,
Path(bundle_id): Path<i32>,
Extension(config): Extension<Arc<GlobalConfig>>,
) -> Result<Vec<u8>, StatusCode> {
let version =
db::bots::find_bot_version(bundle_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?;
let bot_id = version.bot_id.ok_or(StatusCode::FORBIDDEN)?;
let bot = db::bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
if bot.owner_id != Some(user.id) {
return Err(StatusCode::FORBIDDEN);
}
let bundle_path = version.code_bundle_path.ok_or(StatusCode::NOT_FOUND)?;
// TODO: avoid hardcoding paths
let full_bundle_path = PathBuf::from(&config.bots_directory)
.join(&bundle_path)
.join("bot.py");
let bot_code =
std::fs::read(full_bundle_path).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(bot_code)
}