public async Task <string> Execute(ProgramSpecification program) { var zipFileName = Guid.NewGuid().ToString() + ".zip"; var zipFilePath = Path.Combine("/requests", zipFileName); var expectedOutputFile = Path.Combine("/outputs", zipFileName + ".output"); try { DropToZip(program, zipFilePath); await WaitForOutputFile(expectedOutputFile); Thread.Sleep(10); return(File.ReadAllText(expectedOutputFile)); } finally { if (File.Exists(zipFilePath)) { File.Delete(zipFilePath); } if (File.Exists(expectedOutputFile)) { File.Delete(expectedOutputFile); } } }
public async Task <IActionResult> Index([FromBody] ProgramSpecification model) { if (!ModelState.IsValid) { return(StatusCode(400)); } return(Json(new { result = await executorService.Execute(model) })); }
private void DropToZip(ProgramSpecification program, string zipFilePath) { using (var zipFile = File.Create(zipFilePath)) using (var zipStream = new System.IO.Compression.ZipArchive(zipFile, System.IO.Compression.ZipArchiveMode.Create)) { using (var writer = new StreamWriter(zipStream.CreateEntry("Program.cs").Open())) { writer.Write(program.Program); } if (!String.IsNullOrWhiteSpace(program.ProjectJSON)) { using (var writer = new StreamWriter(zipStream.CreateEntry("project.json").Open())) { writer.Write(program.ProjectJSON); } } } }