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); } }
public ActionResult Import(string problemId, HttpPostedFileBase file, bool deleteOldFiles) { int id; if (!int.TryParse(problemId, out id)) { this.TempData.Add("DangerMessage", "Невалидна задача"); return(this.RedirectToAction("Index")); } var problem = this.Data.Problems.All().Where(x => x.Id == id).FirstOrDefault(); if (problem == null) { this.TempData.Add("DangerMessage", "Невалидна задача"); return(this.RedirectToAction("Index")); } if (file == null || file.ContentLength == 0) { this.TempData.Add("DangerMessage", "Файлът не може да бъде празен"); return(this.RedirectToAction("Problem", new { id = id })); } var extension = file.FileName.Substring(file.FileName.Length - 4, 4); if (extension != ".zip") { this.TempData.Add("DangerMessage", "Файлът трябва да бъде .ZIP файл"); return(this.RedirectToAction("Problem", new { id = id })); } if (deleteOldFiles) { var tests = problem.Tests.ToList(); foreach (var test in tests) { var testRuns = test.TestRuns.ToList(); foreach (var testRun in testRuns) { this.Data.TestRuns.Delete(testRun.Id); } this.Data.Tests.Delete(test.Id); } problem.Tests = new HashSet <Test>(); } using (var memory = new MemoryStream()) { file.InputStream.CopyTo(memory); memory.Position = 0; var parsedTests = new TestsParseResult(); try { parsedTests = ZippedTestsManipulator.Parse(memory); } catch { this.TempData.Add("DangerMessage", "Zip файлът е повреден"); return(this.RedirectToAction("Problem", new { id })); } if (parsedTests.ZeroInputs.Count != parsedTests.ZeroOutputs.Count || parsedTests.Inputs.Count != parsedTests.Outputs.Count) { this.TempData.Add("DangerMessage", "Невалидни тестове"); return(this.RedirectToAction("Problem", new { id })); } ZippedTestsManipulator.AddTestsToProblem(problem, parsedTests); this.Data.SaveChanges(); } this.TempData.Add("InfoMessage", "Тестовете са добавени към задачата"); return(this.RedirectToAction("Problem", new { id })); }