示例#1
0
        public async Task UserAvatar(IFormFile avatar)
        {
            var userId = userManager.GetUserId(User);
            var user   = await userManager.FindByIdAsync(userId);

            if (avatar == null)
            {
                throw new BadRequestException("文件格式不正确");
            }

            if (!avatar.ContentType.StartsWith("image/"))
            {
                throw new BadRequestException("文件格式不正确");
            }

            if (avatar.Length > 1048576)
            {
                throw new BadRequestException("文件大小不能超过 1 Mb");
            }

            await using var stream = new System.IO.MemoryStream();

            await avatar.CopyToAsync(stream);

            stream.Seek(0, System.IO.SeekOrigin.Begin);
            var buffer = new byte[stream.Length];
            await stream.ReadAsync(buffer);

            user.Avatar = buffer;
            await userManager.UpdateAsync(user);
        }
示例#2
0
        public async Task <SubmitSuccessModel> SubmitSolution([FromBody] SubmitModel model)
        {
            var user = await userManager.GetUserAsync(User);

            var now = DateTime.Now;
            var allowJumpToResult = true;

            if (user.Privilege == 5)
            {
                throw new ForbiddenException("不允许提交,请与管理员联系");
            }

            if (model.GroupId != 0)
            {
                var inGroup = await groupService.IsInGroupAsync(user.Id, model.GroupId);

                if (!inGroup)
                {
                    throw new ForbiddenException("未参加该小组");
                }
            }

            var problem = await problemService.GetProblemAsync(model.ProblemId);

            if (problem == null)
            {
                throw new NotFoundException("该题目不存在");
            }
            var problemConfig = problem.Config.DeserializeJson <ProblemConfig>(false);

            // For older version compatibility
            if (problemConfig.SourceFiles.Count == 0)
            {
                problemConfig.SourceFiles.Add(string.IsNullOrEmpty(problemConfig.SubmitFileName) ? "${random}${extension}" : $"{problemConfig.SubmitFileName}${{extension}}");
            }

            var sources = new List <Source>();

            foreach (var i in model.Content)
            {
                if (problemConfig.CodeSizeLimit != 0 && problemConfig.CodeSizeLimit < Encoding.UTF8.GetByteCount(i.Content))
                {
                    throw new BadRequestException("提交内容长度超出限制");
                }
                if (problemConfig.SourceFiles.Contains(i.FileName))
                {
                    sources.Add(new Source
                    {
                        FileName = i.FileName,
                        Content  = i.Content
                    });
                }
            }

            var useDefaultDisabledConfig = false;

            var langConfig = (await languageService.GetLanguageConfigAsync()).ToList();
            var langs      = problemConfig.Languages?.Split(';', StringSplitOptions.RemoveEmptyEntries) ?? new string[0];

            if (langs.Length == 0)
            {
                langs = langConfig.Select(i => i.Name).ToArray();
            }
            else
            {
                useDefaultDisabledConfig = true;
            }

            if (model.ContestId != 0)
            {
                var contest = await contestService.GetContestAsync(model.ContestId);

                if (contest != null)
                {
                    if (contest.StartTime > now || now > contest.EndTime)
                    {
                        throw new ForbiddenException("当前不允许提交");
                    }
                    if (contest.Hidden && !Utils.PrivilegeHelper.IsTeacher(user.Privilege))
                    {
                        throw new NotFoundException("该比赛不存在");
                    }

                    var contestConfig = contest.Config.DeserializeJson <ContestConfig>(false);
                    if (contestConfig.SubmissionLimit != 0)
                    {
                        var judges = await judgeService.QueryJudgesAsync(user.Id,
                                                                         model.GroupId == 0?null : (int?)model.GroupId,
                                                                         model.ContestId,
                                                                         model.ProblemId);

                        if (contestConfig.SubmissionLimit <= await judges /*.Cacheable()*/.CountAsync())
                        {
                            throw new ForbiddenException("超出提交次数限制");
                        }
                    }
                    if (contestConfig.ResultMode != ResultDisplayMode.Intime)
                    {
                        allowJumpToResult = false;
                    }
                    var contestLangs = contestConfig.Languages?.Split(';', StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
                    if (contestLangs.Length != 0)
                    {
                        langs = langs.Intersect(contestLangs).ToArray();
                        useDefaultDisabledConfig = true;
                    }
                }
            }
            else if (problem.Hidden && !Utils.PrivilegeHelper.IsTeacher(user.Privilege))
            {
                throw new NotFoundException("该题目不存在");
            }

            if (!useDefaultDisabledConfig)
            {
                langs = langs.Where(i => langConfig.Any(j => j.Name == i && !j.DisabledByDefault)).ToArray();
            }

            if (!langs.Contains(model.Language))
            {
                throw new ForbiddenException("不允许使用该语言提交");
            }

            user.SubmissionCount++;
            await userManager.UpdateAsync(user);

            var id = await judgeService.QueueJudgeAsync(new Judge
            {
                Content        = sources.SerializeJsonAsString(),
                Language       = model.Language,
                ProblemId      = model.ProblemId,
                ContestId      = model.ContestId == 0 ? null : (int?)model.ContestId,
                GroupId        = model.GroupId == 0 ? null : (int?)model.GroupId,
                UserId         = user.Id,
                Description    = "Online Judge",
                AdditionalInfo = "v2"
            });

            return(new SubmitSuccessModel
            {
                Jump = allowJumpToResult,
                ResultId = id
            });
        }