(fix) delete the file after its run

This commit is contained in:
b1ek 2024-02-18 02:51:27 +10:00
parent b390f74431
commit 1691684c78
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 6 additions and 3 deletions

View File

@ -1,4 +1,4 @@
use std::{fs, io::Read, process::{Command, Stdio}};
use std::{fs::{self, remove_file}, io::Read, process::{Command, Stdio}};
use rand::{distributions::Alphanumeric, Rng};
use serde::{Serialize, Deserialize};
@ -31,10 +31,11 @@ async fn executor(_state: SharedState, data: ExecutorData) -> Result<Box<dyn Rep
.take(64)
.map(char::from)
.collect::<String>();
let path = format!("/tmp/sandy-tmp/{name}");
fs::write(format!("/tmp/sandy-tmp/{name}"), &data.code).unwrap();
fs::write(path.clone(), &data.code).unwrap();
let mut out = Command::new("python")
.arg(format!("/tmp/sandy-tmp/{name}"))
.arg(path.clone())
.stdout(Stdio::piped())
.spawn()
.unwrap();
@ -53,6 +54,8 @@ async fn executor(_state: SharedState, data: ExecutorData) -> Result<Box<dyn Rep
}
let stdout = stdout.replace('\n', "<br>");
remove_file(path).unwrap();
Ok(Box::new(warp::reply::json(&stdout)))
}