private void AddTestsToProblem(Problem problem, HttpPostedFileBase testArchive)
        {
            using (var memory = new MemoryStream())
            {
                testArchive.InputStream.CopyTo(memory);
                memory.Position = 0;

                var parsedTests = ZippedTestsParser.Parse(memory);

                if (!ZippedTestsParser.AreTestsParsedCorrectly(parsedTests))
                {
                    throw new ArgumentException(GlobalResource.Invalid_tests);
                }

                ZippedTestsParser.AddTestsToProblem(problem, parsedTests);
            }
        }
示例#2
0
        public ActionResult Import(string problemId, HttpPostedFileBase file, bool retestTask, bool deleteOldFiles)
        {
            if (!int.TryParse(problemId, out var id))
            {
                this.TempData.AddDangerMessage(Resource.Invalid_problem);
                return(this.RedirectToAction(c => c.Index()));
            }

            var problem = this.problemsData.GetById(id);

            if (problem == null)
            {
                this.TempData.AddDangerMessage(Resource.Invalid_problem);
                return(this.RedirectToAction(c => c.Index()));
            }

            if (!this.CheckIfUserHasProblemPermissions(id))
            {
                this.TempData.AddDangerMessage(GeneralResource.No_privileges_message);
                return(this.Json("No premissions"));
            }

            if (file == null || file.ContentLength == 0)
            {
                this.TempData.AddDangerMessage(Resource.No_empty_file);
                return(this.RedirectToAction(c => c.Problem(id)));
            }

            var extension = Path.GetExtension(file.FileName);

            if (extension != ".zip")
            {
                this.TempData.AddDangerMessage(Resource.Must_be_zip);
                return(this.RedirectToAction(c => c.Problem(id)));
            }

            TestsParseResult parsedTests;

            using (var memory = new MemoryStream())
            {
                file.InputStream.CopyTo(memory);
                memory.Position = 0;

                try
                {
                    parsedTests = ZippedTestsParser.Parse(memory);
                }
                catch
                {
                    this.TempData.AddDangerMessage(Resource.Zip_damaged);
                    return(this.RedirectToAction(c => c.Problem(id)));
                }
            }

            if (!ZippedTestsParser.AreTestsParsedCorrectly(parsedTests))
            {
                this.TempData.AddDangerMessage(Resource.Invalid_tests);
                return(this.RedirectToAction(c => c.Problem(id)));
            }

            int addedTestsCount;

            using (var scope = TransactionsHelper.CreateTransactionScope(IsolationLevel.RepeatableRead))
            {
                this.submissionsData.RemoveTestRunsCacheByProblem(problem.Id);

                if (deleteOldFiles)
                {
                    this.testRunsData.DeleteByProblem(problem.Id);
                    this.testsData.DeleteByProblem(problem.Id);
                }

                addedTestsCount = ZippedTestsParser.AddTestsToProblem(problem, parsedTests);

                this.problemsData.Update(problem);

                if (retestTask)
                {
                    this.problemsBusiness.RetestById(problem.Id);
                }

                scope.Complete();
            }

            this.TempData.AddInfoMessage(string.Format(Resource.Tests_added_to_problem, addedTestsCount));
            return(this.RedirectToAction(c => c.Problem(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 }));
            }

            TestsParseResult parsedTests;

            using (var memory = new MemoryStream())
            {
                file.InputStream.CopyTo(memory);
                memory.Position = 0;

                try
                {
                    parsedTests = ZippedTestsParser.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 }));
                }
            }

            using (var scope = new TransactionScope())
            {
                this.Data.Submissions.Update(
                    x => x.ProblemId == id && !x.IsDeleted,
                    x => new Submission {
                    TestRunsCache = null
                });
                this.Data.SaveChanges();

                if (deleteOldFiles)
                {
                    this.Data.TestRuns.Delete(tr => tr.Test.ProblemId == problem.Id);
                    this.Data.Tests.Delete(t => t.ProblemId == problem.Id);
                }

                ZippedTestsParser.AddTestsToProblem(problem, parsedTests);

                if (retestTask)
                {
                    this.RetestSubmissions(problem.Id);
                }

                this.Data.SaveChanges();
                scope.Complete();
            }

            this.TempData.AddInfoMessage(Resource.Tests_addted_to_problem);

            return(this.RedirectToAction("Problem", new { id }));
        }