Пример #1
0
        public async Task <IActionResult> GetSubmissionFile(int submissionId)
        {
            GetSubmissionFileDto dto = new GetSubmissionFileDto {
                SubmissionId = submissionId
            };
            ServiceResponse <string> response = await _submissionService.DownloadSubmission(dto);

            if (!response.Success)
            {
                return(BadRequest(response));
            }
            string path = response.Data;
            string type = GetContentType(path);

            if (type == null)
            {
                return(BadRequest("this file type is not supported"));
            }
            var memory = new MemoryStream();

            using (var stream = new FileStream(path, FileMode.Open))
            {
                await stream.CopyToAsync(memory);
            }
            memory.Position = 0;
            return(File(memory, type, Path.GetFileName(path)));
        }
Пример #2
0
        public async Task <ServiceResponse <string> > DownloadSubmission(GetSubmissionFileDto dto)
        {
            ServiceResponse <string> response = new ServiceResponse <string>();
            Submission submission             = await _context.Submissions
                                                .Include(s => s.AffiliatedGroup).ThenInclude(pg => pg.GroupMembers)
                                                .Include(s => s.AffiliatedAssignment)
                                                .FirstOrDefaultAsync(s => s.Id == dto.SubmissionId);

            if (submission == null)
            {
                response.Data    = null;
                response.Message = "There is no such submission";
                response.Success = false;
                return(response);
            }
            if (!submission.HasFile)
            {
                response.Data    = null;
                response.Message = "This group has not yet submitted their file for this assigment";
                response.Success = false;
                return(response);
            }
            Course course = await _context.Courses.Include(c => c.Instructors).FirstOrDefaultAsync(c => c.Id == submission.AffiliatedAssignment.AfilliatedCourseId);

            if (!submission.AffiliatedAssignment.VisibilityOfSubmission &&
                !course.Instructors.Any(cu => cu.UserId == GetUserId()) &&
                !submission.AffiliatedGroup.GroupMembers.Any(u => u.UserId == GetUserId()))
            {
                response.Data    = null;
                response.Message = "You are not authorized to see this submission";
                response.Success = false;
                return(response);
            }
            response.Data    = submission.FilePath;
            response.Success = true;
            return(response);
        }