private async Task <SavedFile> AddSampleFile(HttpRequestMessage req) { var bytes = Convert.FromBase64String(SampleSavedFile.fileContents); Stream stream = new MemoryStream(bytes); return(await _fileService.AddFileAsync(stream, SampleSavedFile.documentId, SampleSavedFile.fileName, "", req)); }
public async Task <ApiResult> AddProblemAsync(long contestId, long userId, AddProblemViewModel addProblem) { var contest = await _context.Contests.Include(c => c.Owner).SingleOrDefaultAsync(c => c.Id == contestId && c.Id == addProblem.ContestId); if (contest == null) { return new ApiResult { Error = AddProblemResultType.ContestNotFound } } ; if (contest.Owner.UserId != userId) { return new ApiResult { Error = AddProblemResultType.Unauthorized } } ; if (contest.ProgressState != ContestProgressState.NoStarted && contest.ProgressState != ContestProgressState.UnPublished) { return(new ApiResult { Error = AddProblemResultType.Forbiddance }); } var problem = _mapper.Map <AddProblemViewModel, ProblemModel>(addProblem); var fileResult = await _fileService.AddFileAsync(addProblem.File, userId); switch (fileResult.Error) { case AddFileResultType.Error: return(new ApiResult { Error = AddProblemResultType.Error }); case AddFileResultType.FileTooBig: return(new ApiResult { Error = AddProblemResultType.FileTooBig }); case AddFileResultType.Ok: problem.Content = fileResult.Data; break; default: throw new ArgumentOutOfRangeException(); } await _context.Problems.AddAsync(problem); await _context.SaveChangesAsync(); return(new ApiResult { Error = AddProblemResultType.Ok }); }
public async Task <HttpResponseMessage> Post(HttpRequestMessage req, TraceWriter log) { log.Info("FileApi.Post method called."); if (req.Content.IsMimeMultipartContent() == false) { return(req.CreateResponse(HttpStatusCode.BadRequest)); } Stream fileStream = null; var multipartStreamProvider = await req.Content.ReadAsMultipartAsync(new MultipartMemoryStreamProvider()); string documentId = string.Empty, fileName = string.Empty, name = string.Empty; foreach (HttpContent content in multipartStreamProvider.Contents) { Stream stream = content.ReadAsStreamAsync().Result; if (!string.IsNullOrEmpty(content.Headers.ContentDisposition.FileName)) { fileName = content.Headers.ContentDisposition.FileName.Trim('"'); log.Info($"Filename: {content.Headers.ContentDisposition.FileName}"); fileStream = stream; } else { var contents = new StreamReader(stream).ReadToEnd(); if (content.Headers.ContentDisposition.Name == "\"documentId\"") { documentId = contents; } if (content.Headers.ContentDisposition.Name == "\"name\"") { name = contents; } } } var savedFile = await _fileService.AddFileAsync(fileStream, documentId, fileName, name, req); return(req.CreateResponse(HttpStatusCode.Created, savedFile)); }
public async Task <ApiResult> AddSubmissionAsync(long contestId, long problemId, long userId, AddSubmissionViewModel addSubmission) { var contest = await _context.Contests.Include(c => c.Problems).ThenInclude(p => p.Submissions).Include(c => c.Enrollments).ThenInclude(e => e.Student).SingleOrDefaultAsync(c => c.Id == contestId); if (contest == null) { return new ApiResult { Error = AddSubmissionResultType.ContestNotFound } } ; if (contest.ProgressState != ContestProgressState.InProgress) { return(new ApiResult { Error = AddSubmissionResultType.Forbiddance }); } var enrollment = contest.Enrollments.SingleOrDefault(e => e.StudentId == addSubmission.StudentId && e.Student.UserId == userId); if (enrollment == null) { return new ApiResult { Error = AddSubmissionResultType.Unauthorized } } ; var problem = contest.Problems.SingleOrDefault(p => p.Id == problemId && p.Id == addSubmission.ProblemId); if (problem == null) { return new ApiResult { Error = AddSubmissionResultType.ProblemNotFound } } ; var submissions = problem.Submissions.Where(p => p.StudentId == addSubmission.StudentId).ToList(); if (submissions.Count > 0) { foreach (var s in submissions) { if (s.Type != SubmissionType.Binary) { continue; } var fileResult = await _fileService.DeleteFileAsync(s.Content); switch (fileResult) { case DeleteFileResultType.Error: return(new ApiResult { Error = AddSubmissionResultType.Error }); case DeleteFileResultType.Ok: break; default: throw new ArgumentOutOfRangeException(); } } _context.RemoveRange(submissions); } var submission = _mapper.Map <AddSubmissionViewModel, SubmissionModel>(addSubmission); submission.CreateTime = DateTime.Now; if (submission.Type == SubmissionType.Binary) { var fileResult = await _fileService.AddFileAsync(addSubmission.File, userId); switch (fileResult.Error) { case AddFileResultType.Error: return(new ApiResult { Error = AddSubmissionResultType.Error }); case AddFileResultType.FileTooBig: return(new ApiResult { Error = AddSubmissionResultType.FileTooBig }); case AddFileResultType.Ok: submission.Content = fileResult.Data; break; default: throw new ArgumentOutOfRangeException(); } } await _context.Submissions.AddAsync(submission); await _context.SaveChangesAsync(); return(new ApiResult { Error = AddSubmissionResultType.Ok }); }