private void AddTestsToProblem(Problem problem, HttpPostedFileBase testArchive) { var extension = testArchive.FileName.Substring(testArchive.FileName.Length - 4, 4); if (extension != ".zip") { throw new ArgumentException("Тестовете трябва да бъдат в .ZIP файл"); } using (var memory = new MemoryStream()) { testArchive.InputStream.CopyTo(memory); memory.Position = 0; var parsedTests = new TestsParseResult(); parsedTests = ZippedTestsManipulator.Parse(memory); if (parsedTests.ZeroInputs.Count != parsedTests.ZeroOutputs.Count || parsedTests.Inputs.Count != parsedTests.Outputs.Count) { throw new ArgumentException("Невалидни тестове"); } ZippedTestsManipulator.AddTestsToProblem(problem, parsedTests); } }
private void AddTestsToProblem(Problem problem, HttpPostedFileBase testArchive) { var extension = testArchive.FileName.Substring(testArchive.FileName.Length - 4, 4); if (extension != GlobalConstants.ZipFileExtension) { throw new ArgumentException(GlobalResource.Must_be_zip_file); } using (var memory = new MemoryStream()) { testArchive.InputStream.CopyTo(memory); memory.Position = 0; var parsedTests = ZippedTestsManipulator.Parse(memory); if (parsedTests.ZeroInputs.Count != parsedTests.ZeroOutputs.Count || parsedTests.Inputs.Count != parsedTests.Outputs.Count) { throw new ArgumentException(GlobalResource.Invalid_tests); } ZippedTestsManipulator.AddTestsToProblem(problem, parsedTests); } }
public ActionResult Import(string problemId, HttpPostedFileBase file, bool retestTask, bool deleteOldFiles) { int id; if (!int.TryParse(problemId, out id)) { this.TempData.Add(GlobalConstants.DangerMessage, "Невалидна задача"); return(this.RedirectToAction(GlobalConstants.Index)); } var problem = this.Data.Problems.All().FirstOrDefault(x => x.Id == id); if (problem == null) { this.TempData.Add(GlobalConstants.DangerMessage, "Невалидна задача"); return(this.RedirectToAction(GlobalConstants.Index)); } if (file == null || file.ContentLength == 0) { this.TempData.Add(GlobalConstants.DangerMessage, "Файлът не може да бъде празен"); return(this.RedirectToAction("Problem", new { id = id })); } var extension = file.FileName.Substring(file.FileName.Length - 4, 4); if (extension != ".zip") { this.TempData.Add(GlobalConstants.DangerMessage, "Файлът трябва да бъде .ZIP файл"); return(this.RedirectToAction("Problem", new { id = id })); } if (deleteOldFiles) { var tests = problem.Tests.Select(t => new { Id = t.Id, TestRuns = t.TestRuns.Select(tr => tr.Id) }).ToList(); foreach (var test in tests) { var testRuns = test.TestRuns.ToList(); foreach (var testRun in testRuns) { this.Data.TestRuns.Delete(testRun); } this.Data.Tests.Delete(test.Id); } problem.Tests = new HashSet <Test>(); } using (var memory = new MemoryStream()) { file.InputStream.CopyTo(memory); memory.Position = 0; TestsParseResult parsedTests; try { parsedTests = ZippedTestsManipulator.Parse(memory); } catch { this.TempData.Add(GlobalConstants.DangerMessage, "Zip файлът е повреден"); return(this.RedirectToAction("Problem", new { id })); } if (parsedTests.ZeroInputs.Count != parsedTests.ZeroOutputs.Count || parsedTests.Inputs.Count != parsedTests.Outputs.Count) { this.TempData.Add(GlobalConstants.DangerMessage, "Невалидни тестове"); return(this.RedirectToAction("Problem", new { id })); } ZippedTestsManipulator.AddTestsToProblem(problem, parsedTests); this.Data.SaveChanges(); } if (retestTask) { this.RetestSubmissions(problem); } this.TempData.Add(GlobalConstants.InfoMessage, "Тестовете са добавени към задачата"); return(this.RedirectToAction("Problem", new { id })); }
public ActionResult Import(string problemId, HttpPostedFileBase file, bool retestTask, bool deleteOldFiles) { int id; if (!int.TryParse(problemId, out id)) { this.TempData.AddDangerMessage(Resource.Invalid_problem); return(this.RedirectToAction(GlobalConstants.Index)); } var problem = this.Data.Problems.All().FirstOrDefault(x => x.Id == id); if (problem == null) { this.TempData.AddDangerMessage(Resource.Invalid_problem); return(this.RedirectToAction(GlobalConstants.Index)); } if (!this.CheckIfUserHasProblemPermissions(id)) { this.TempData[GlobalConstants.DangerMessage] = "Нямате привилегиите за това действие"; return(this.Json("No premissions")); } if (file == null || file.ContentLength == 0) { this.TempData.AddDangerMessage(Resource.No_empty_file); return(this.RedirectToAction("Problem", new { id })); } var extension = file.FileName.Substring(file.FileName.Length - 4, 4); if (extension != ".zip") { this.TempData.AddDangerMessage(Resource.Must_be_zip); return(this.RedirectToAction("Problem", new { id })); } if (deleteOldFiles) { var tests = problem.Tests.Select(t => new { t.Id, TestRuns = t.TestRuns.Select(tr => tr.Id) }).ToList(); foreach (var test in tests) { var testRuns = test.TestRuns.ToList(); foreach (var testRun in testRuns) { this.Data.TestRuns.Delete(testRun); } this.Data.Tests.Delete(test.Id); } problem.Tests = new HashSet <Test>(); } using (var memory = new MemoryStream()) { file.InputStream.CopyTo(memory); memory.Position = 0; TestsParseResult parsedTests; try { parsedTests = ZippedTestsManipulator.Parse(memory); } catch { this.TempData.AddDangerMessage(Resource.Zip_damaged); return(this.RedirectToAction("Problem", new { id })); } if (parsedTests.ZeroInputs.Count != parsedTests.ZeroOutputs.Count || parsedTests.Inputs.Count != parsedTests.Outputs.Count) { this.TempData.AddDangerMessage(Resource.Invalid_tests); return(this.RedirectToAction("Problem", new { id })); } ZippedTestsManipulator.AddTestsToProblem(problem, parsedTests); this.Data.SaveChanges(); } if (retestTask) { this.RetestSubmissions(problem.Id); } this.TempData.AddInfoMessage(Resource.Tests_addted_to_problem); return(this.RedirectToAction("Problem", new { id })); }