// TODO: Extract common logic between SubmitBinaryFile() and Submit() public ActionResult SubmitBinaryFile(BinarySubmissionModel participantSubmission, bool official, int?returnProblem) { if (participantSubmission?.File == null) { throw new HttpException((int)HttpStatusCode.BadRequest, Resource.ContestsGeneral.Upload_file); } var problem = this.Data.Problems.All().FirstOrDefault(x => x.Id == participantSubmission.ProblemId); if (problem == null) { throw new HttpException((int)HttpStatusCode.Unauthorized, Resource.ContestsGeneral.Problem_not_found); } var participant = this.Data.Participants.GetWithContest(problem.ContestId, this.UserProfile.Id, official); if (participant == null) { throw new HttpException((int)HttpStatusCode.Unauthorized, Resource.ContestsGeneral.User_is_not_registered_for_exam); } ValidateContest(participant.Contest, official); ValidateSubmissionType(participantSubmission.SubmissionTypeId, participant.Contest); if (this.Data.Submissions.HasSubmissionTimeLimitPassedForParticipant(participant.Id, participant.Contest.LimitBetweenSubmissions)) { throw new HttpException((int)HttpStatusCode.ServiceUnavailable, Resource.ContestsGeneral.Submission_was_sent_too_soon); } if (problem.SourceCodeSizeLimit < participantSubmission.File.ContentLength) { throw new HttpException((int)HttpStatusCode.BadRequest, Resource.ContestsGeneral.Submission_too_long); } // Validate submission type existence var submissionType = this.Data.SubmissionTypes.GetById(participantSubmission.SubmissionTypeId); if (submissionType == null) { throw new HttpException((int)HttpStatusCode.BadRequest, Resource.ContestsGeneral.Invalid_request); } // Validate if binary files are allowed if (!submissionType.AllowBinaryFilesUpload) { throw new HttpException((int)HttpStatusCode.BadRequest, Resource.ContestsGeneral.Binary_files_not_allowed); } // Validate file extension if (!submissionType.AllowedFileExtensionsList.Contains( participantSubmission.File.FileName.GetFileExtension())) { throw new HttpException((int)HttpStatusCode.BadRequest, Resource.ContestsGeneral.Invalid_extention); } if (!this.ModelState.IsValid) { throw new HttpException((int)HttpStatusCode.BadRequest, Resource.ContestsGeneral.Invalid_request); } this.Data.Submissions.Add(new Submission { Content = participantSubmission.File.InputStream.ToByteArray(), FileExtension = participantSubmission.File.FileName.GetFileExtension(), ProblemId = participantSubmission.ProblemId, SubmissionTypeId = participantSubmission.SubmissionTypeId, ParticipantId = participant.Id }); this.Data.SaveChanges(); this.TempData.Add(GlobalConstants.InfoMessage, Resource.ContestsGeneral.Solution_uploaded); return(this.Redirect(string.Format("/Contests/{2}/Index/{0}#{1}", problem.ContestId, returnProblem ?? 0, official ? CompeteUrl : PracticeUrl))); }
// TODO: Extract common logic between SubmitBinaryFile and Submit methods public ActionResult SubmitBinaryFile(BinarySubmissionModel participantSubmission, bool official, int?returnProblem) { if (participantSubmission?.File == null) { throw new HttpException((int)HttpStatusCode.BadRequest, Resource.ContestsGeneral.Upload_file); } var problem = this.problemsData.GetWithProblemGroupById(participantSubmission.ProblemId); if (problem == null) { throw new HttpException((int)HttpStatusCode.Unauthorized, Resource.ContestsGeneral.Problem_not_found); } var participant = this.participantsData .GetWithContestByContestByUserAndIsOfficial(problem.ProblemGroup.ContestId, this.UserProfile.Id, official); if (participant == null) { throw new HttpException((int)HttpStatusCode.Unauthorized, Resource.ContestsGeneral.User_is_not_registered_for_exam); } this.ValidateContest(participant.Contest, official); this.ValidateProblemForParticipant( participant, participant.Contest, participantSubmission.ProblemId, official); if (official && !this.contestsBusiness.IsContestIpValidByContestAndIp(problem.ProblemGroup.ContestId, this.Request.UserHostAddress)) { return(this.RedirectToAction("NewContestIp", new { id = problem.ProblemGroup.ContestId })); } if (this.Data.Submissions.HasSubmissionTimeLimitPassedForParticipant(participant.Id, participant.Contest.LimitBetweenSubmissions)) { throw new HttpException((int)HttpStatusCode.ServiceUnavailable, Resource.ContestsGeneral.Submission_was_sent_too_soon); } if (problem.SourceCodeSizeLimit < participantSubmission.File.ContentLength) { throw new HttpException((int)HttpStatusCode.BadRequest, Resource.ContestsGeneral.Submission_too_long); } var submissionType = ValidateSubmissionType( participantSubmission.SubmissionTypeId, problem, shouldAllowBinaryFiles: true); // Validate file extension if (!submissionType.AllowedFileExtensionsList.Contains( participantSubmission.File.FileName.GetFileExtension())) { throw new HttpException((int)HttpStatusCode.BadRequest, Resource.ContestsGeneral.Invalid_extention); } if (!this.ModelState.IsValid) { throw new HttpException((int)HttpStatusCode.BadRequest, Resource.ContestsGeneral.Invalid_request); } var newSubmission = new Submission { Content = participantSubmission.File.InputStream.ToByteArray(), FileExtension = participantSubmission.File.FileName.GetFileExtension(), ProblemId = participantSubmission.ProblemId, SubmissionTypeId = participantSubmission.SubmissionTypeId, ParticipantId = participant.Id }; this.Data.Submissions.Add(newSubmission); this.Data.SaveChanges(); this.submissionsForProcessingData.AddOrUpdateBySubmission(newSubmission.Id); this.TempData.Add(GlobalConstants.InfoMessage, Resource.ContestsGeneral.Solution_uploaded); return(this.Redirect(string.Format("/Contests/{2}/Index/{0}#{1}", problem.ProblemGroup.ContestId, returnProblem ?? 0, official ? CompeteActionName : PracticeActionName))); }