/// <summary> /// Writes the contents of a submission to an archive. /// </summary> private async Task WriteSubmissionToArchiveAsync( ZipArchive archive, Project project, ClassroomMembership student, IArchive templateContents, IArchive submissionContents, ProjectSubmissionDownloadFormat format) { bool includeEclipseProjects = ( format == ProjectSubmissionDownloadFormat.Eclipse || format == ProjectSubmissionDownloadFormat.All ); bool includeFlatFiles = ( format == ProjectSubmissionDownloadFormat.Flat || format == ProjectSubmissionDownloadFormat.All ); // Submissions are grouped by section. If a student is in multiple // sections, just grab the first one string sectionName = student.SectionMemberships.First().Section.Name; var studentFolder = $"EclipseProjects\\{sectionName}\\{student.GitHubTeam}"; // The project will contain all non-immutable submission files, // plus all immutable and private files from the template project. var projectContents = submissionContents.Files .Where ( entry => project.GetFileType(entry) == FileType.Public ) .Concat ( templateContents.Files.Where ( entry => project.GetFileType(entry) != FileType.Public ) ) .ToList(); foreach (var entry in projectContents) { if (ExcludeEntry(project, entry)) { continue; } var contents = _transformer.GetFileContents(project, student, entry); var archiveFilePath = entry.FullPath; var fileName = archiveFilePath.Substring(archiveFilePath.LastIndexOf("/") + 1); if (includeEclipseProjects) { var archiveFileFolder = archiveFilePath.Contains("/") ? archiveFilePath.Substring(0, archiveFilePath.LastIndexOf("/")) : archiveFilePath; var localFileFolder = $"{studentFolder}\\{archiveFileFolder}"; var localFilePath = $"{localFileFolder}\\{fileName}"; // Add the file to the student project folder. var projectFolderEntry = archive.CreateEntry(localFilePath); using (Stream stream = projectFolderEntry.Open()) { await stream.WriteAsync(contents, offset : 0, count : contents.Length); } } // Add the file to the folder containing all files, if applicable. if (includeFlatFiles && fileName.EndsWith(".java") && project.GetFileType(entry) == FileType.Public) { // Files are grouped by base name, and then by section var baseFileName = fileName.Substring(0, fileName.LastIndexOf(".")); var allFilesEntry = archive.CreateEntry($"AllFiles\\{baseFileName}\\{sectionName}\\{student.GitHubTeam}-{fileName}"); using (Stream stream = allFilesEntry.Open()) { await stream.WriteAsync(contents, offset : 0, count : contents.Length); } } } }
/// <summary> /// Writes the contents of a submission to an archive. /// </summary> private async Task WriteSubmissionToArchiveAsync( ZipArchive archive, Project project, ClassroomMembership student, IArchive templateContents, IArchive submissionContents) { var studentFolder = $"EclipseProjects\\{student.GitHubTeam}"; // The project will contain all non-immutable submission files, // plus all immutable and private files from the template project. var projectContents = submissionContents.Files .Where ( entry => project.GetFileType(entry) == FileType.Public ) .Concat ( templateContents.Files.Where ( entry => project.GetFileType(entry) != FileType.Public ) ) .ToList(); foreach (var entry in projectContents) { if (ExcludeEntry(project, entry)) { continue; } var contents = _transformer.GetFileContents(project, student, entry); var archiveFilePath = entry.FullPath; var archiveFileFolder = archiveFilePath.Contains("/") ? archiveFilePath.Substring(0, archiveFilePath.LastIndexOf("/")) : archiveFilePath; var localFileFolder = $"{studentFolder}\\{archiveFileFolder}"; var fileName = archiveFilePath.Substring(archiveFilePath.LastIndexOf("/") + 1); var localFilePath = $"{localFileFolder}\\{fileName}"; // Add the file to the student project folder. var projectFolderEntry = archive.CreateEntry(localFilePath); using (Stream stream = projectFolderEntry.Open()) { await stream.WriteAsync(contents, offset : 0, count : contents.Length); } // Add the file to the folder containing all files, if applicable. if (fileName.EndsWith(".java") && project.GetFileType(entry) == FileType.Public) { var allFilesEntry = archive.CreateEntry($"AllFiles\\{student.GitHubTeam}-{fileName}"); using (Stream stream = allFilesEntry.Open()) { await stream.WriteAsync(contents, offset : 0, count : contents.Length); } } } }