public async Task <ActionResult <SubmissionMetadata> > Submit([FromBody] SubmitData data) { if (await _workspace.Problems.Has(data.ProblemId) == false) { return(NotFound()); } if (await _workspace.Users.Has(data.UserId) == false) { return(NotFound()); } SubmissionMetadata meta = new SubmissionMetadata { Id = Guid.NewGuid().ToString(), ProblemId = data.ProblemId, UserId = data.UserId, Language = data.Language, Time = DateTimeOffset.Now, }; ISubmissionProvider sub = await _workspace.Submissions.Create(meta); if (sub == null) { return(Forbid()); } try { if (data.CodeFile != null) { using (System.IO.Stream s = data.CodeFile.OpenReadStream()) { meta.CodeLength = (uint)s.Length; await sub.SetCode(s); } } else { meta.CodeLength = (uint)Encoding.UTF8.GetByteCount(data.Code); using (System.IO.Stream ms = TextIO.ToStream(data.Code ?? "")) await sub.SetCode(ms); } await sub.SetMetadata(meta); SendJudgeRequest(sub.Id); return(Created($"submissions/{meta.Id}", await sub.GetMetadata())); } catch { await _workspace.Submissions.Delete(sub.Id); return(Forbid()); } }
public async Task <ActionResult> GetCode(string id) { ISubmissionProvider res = await _workspace.Submissions.Get(id); if (res == null) { return(NotFound()); } System.IO.Stream code = await res.GetCode(); ProgrammingLanguage lang = (await res.GetMetadata()).Language; return(File(code, "text/plain", $"{id}.{ProgrammingLanguageHelper.Extends[lang]}")); }