Ejemplo n.º 1
0
        public async Task <ServiceResponse <string> > DownloadAllSubmissions(GetSubmissionsFileDto dto)

        {
            ServiceResponse <string> response = new ServiceResponse <string>();
            User user = await _context.Users.Include(u => u.ProjectGroups).FirstOrDefaultAsync(u => u.Id == GetUserId());

            Course course = await _context.Courses.Include(u => u.Instructors).FirstOrDefaultAsync(c => c.Id == dto.CourseId);

            if (course == null)
            {
                response.Data    = null;
                response.Message = "There is no course under this Id";
                response.Success = false;
                return(response);
            }
            if (course.Instructors.FirstOrDefault(i => i.UserId == user.Id) == null && user.UserType == UserTypeClass.Student)
            {
                response.Data    = null;
                response.Message = "You are not authorized for this endpoint";
                response.Success = false;
                return(response);
            }
            if (dto.SectionId == -1)
            {
                response.Data = Path.Combine(_hostingEnvironment.ContentRootPath, string.Format("{0}/{1}",
                                                                                                "StaticFiles/Submissions", dto.CourseId));
            }
            else if (dto.AssignmentId == -1)
            {
                response.Data = Path.Combine(_hostingEnvironment.ContentRootPath, string.Format("{0}/{1}/{2}/",
                                                                                                "StaticFiles/Submissions", dto.CourseId, dto.SectionId));
            }
            else
            {
                response.Data = Path.Combine(_hostingEnvironment.ContentRootPath, string.Format("{0}/{1}/{2}/{3}",
                                                                                                "StaticFiles/Submissions", dto.CourseId, dto.SectionId, dto.AssignmentId));
            }
            return(response);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> GetAllSubmissions(int courseId, int sectionId = -1, int assignmentId = -1)
        {
            GetSubmissionsFileDto dto = new GetSubmissionsFileDto {
                CourseId = courseId, SectionId = sectionId, AssignmentId = assignmentId
            };
            ServiceResponse <string> response = await _submissionService.DownloadAllSubmissions(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   = "Submissions.zip";
            var tempOutput = webRoot + "/StaticFiles/Submissions/" + 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));
            }
        }