Beispiel #1
0
        public async Task <ServiceResponse <string> > DownloadAllCommentFiles(GetAllCommentFilesDto dto)
        {
            ServiceResponse <string> response = new ServiceResponse <string>();
            User user = await _context.Users.Include(u => u.ProjectGroups).FirstOrDefaultAsync(u => u.Id == GetUserId());

            Submission submission = await _context.Submissions.Include(s => s.Comments).FirstOrDefaultAsync(s => s.Id == dto.SubmissionId);

            if (submission == null)
            {
                response.Data    = null;
                response.Message = "There is no project group under this Id";
                response.Success = false;
                return(response);
            }
            if (user.ProjectGroups.FirstOrDefault(pgu => pgu.ProjectGroupId == submission.AffiliatedGroupId) == null && user.UserType == UserTypeClass.Student)
            {
                response.Data    = null;
                response.Message = "You are not authorized for this endpoint";
                response.Success = false;
                return(response);
            }
            if (submission.Comments.FirstOrDefault(cm => cm.CommentedSubmissionId == dto.SubmissionId) == null)
            {
                response.Data    = null;
                response.Message = "There is no comment for this group yet";
                response.Success = false;
                return(response);
            }
            response.Data = Path.Combine(_hostingEnvironment.ContentRootPath, string.Format("{0}/{1}/{2}",
                                                                                            "StaticFiles/Feedbacks", submission.CourseId, dto.SubmissionId));
            return(response);
        }
Beispiel #2
0
        public async Task <IActionResult> GetAllCommentFiles(int submissionId)
        {
            GetAllCommentFilesDto dto = new GetAllCommentFilesDto {
                SubmissionId = submissionId
            };
            ServiceResponse <string> response = await _commentService.DownloadAllCommentFiles(dto);

            if (!response.Success)
            {
                return(BadRequest(response));
            }

            List <string>  files = new List <string>();
            Stack <string> stack = new Stack <string>();
            string         s     = response.Data;

            stack.Push(s);
            while (stack.Count != 0)
            {
                s = stack.Peek();
                stack.Pop();
                foreach (string f in Directory.GetFiles(s))
                {
                    files.Add(f);
                }

                foreach (string d in Directory.GetDirectories(s))
                {
                    stack.Push(d);
                }
            }

            var webRoot    = _hostingEnvironment.ContentRootPath;
            var fileName   = "Feedbacks.zip";
            var tempOutput = webRoot + "/StaticFiles/Feedbacks/" + fileName;

            using (ZipOutputStream zipOutputStream = new ZipOutputStream(System.IO.File.Create(tempOutput)))
            {
                zipOutputStream.SetLevel(9);
                byte[] buffer = new byte[4096];
                for (int i = 0; i < files.Count; i++)
                {
                    ZipEntry zipEntry = new ZipEntry(Path.GetFileName(files[i]));
                    zipEntry.DateTime      = DateTime.Now;
                    zipEntry.IsUnicodeText = true;
                    zipOutputStream.PutNextEntry(zipEntry);
                    using (FileStream fileStream = System.IO.File.OpenRead(files[i]))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fileStream.Read(buffer, 0, buffer.Length);
                            zipOutputStream.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);
                    }
                }

                zipOutputStream.Finish();
                zipOutputStream.Flush();
                zipOutputStream.Close();

                byte[] finalResult = System.IO.File.ReadAllBytes(tempOutput);
                if (System.IO.File.Exists(tempOutput))
                {
                    System.IO.File.Delete(tempOutput);
                }
                if (finalResult == null || !finalResult.Any())
                {
                    return(BadRequest(response));
                }
                return(File(finalResult, "application/zip", fileName));
            }
        }