public async Task <IActionResult> GetCommentFile(int commentId) { GetCommentFileDto dto = new GetCommentFileDto { CommentId = commentId }; ServiceResponse <string> response = await _commentService.DownloadCommentFile(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))); }
public async Task <ServiceResponse <string> > DownloadCommentFile(GetCommentFileDto dto) { ServiceResponse <string> response = new ServiceResponse <string>(); User user = await _context.Users.Include(u => u.ProjectGroups).FirstOrDefaultAsync(u => u.Id == GetUserId()); Comment comment = await _context.Comments.Include(c => c.CommentedSubmission).FirstOrDefaultAsync(s => s.Id == dto.CommentId); if (comment == null) { response.Data = null; response.Message = "There is no comment with this Id"; response.Success = false; return(response); } Submission submission = comment.CommentedSubmission; Assignment assignment = await _context.Assignments.Include(a => a.AfilliatedCourse).ThenInclude(c => c.Instructors) .FirstOrDefaultAsync(a => a.Id == submission.AffiliatedAssignmentId); if (!assignment.VisibilityOfSubmission) { if (!user.ProjectGroups.Any(pgu => pgu.UserId == submission.AffiliatedGroupId) && !assignment.AfilliatedCourse.Instructors.Any(cu => cu.UserId == GetUserId()) && user.UserType == UserTypeClass.Student) { response.Data = null; response.Message = "You are not authorized for this endpoint"; response.Success = false; return(response); } } if (!comment.FileAttachmentAvailability) { response.Data = null; response.Message = "This user has not yet submitted his comment as a file for this group yet"; response.Success = false; return(response); } response.Data = comment.FilePath; return(response); }