diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-09-10 18:56:03 +0200 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-09-10 18:56:03 +0200 |
commit | 3058028edc7ab794e496e12075fa79a4133cd42c (patch) | |
tree | 60882f1238f008670df5b01f0ec5e87a10158a61 /planetwars-server | |
parent | 78a6032b60eea6f39549814c9c38d9078e20284e (diff) | |
download | planetwars.dev-3058028edc7ab794e496e12075fa79a4133cd42c.tar.xz planetwars.dev-3058028edc7ab794e496e12075fa79a4133cd42c.zip |
allow retrieving bot code
Diffstat (limited to 'planetwars-server')
-rw-r--r-- | planetwars-server/src/lib.rs | 1 | ||||
-rw-r--r-- | planetwars-server/src/routes/bots.rs | 26 |
2 files changed, 27 insertions, 0 deletions
diff --git a/planetwars-server/src/lib.rs b/planetwars-server/src/lib.rs index fa1af8d..6018eee 100644 --- a/planetwars-server/src/lib.rs +++ b/planetwars-server/src/lib.rs @@ -127,6 +127,7 @@ pub fn api() -> Router { "/bots/:bot_name/upload", 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/:match_id", get(routes::matches::get_match_data)) .route( diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index f2bf202..4ab1b4e 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -249,3 +249,29 @@ pub async fn upload_code_multipart( 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) +} |