Skip to content
Snippets Groups Projects
Commit 64a81fcc authored by srosse's avatar srosse
Browse files

OO-5238: give a name ot the downloaded ZIP of participants folders

parent 0c47adcd
No related branches found
No related tags found
No related merge requests found
...@@ -22,12 +22,14 @@ package org.olat.course.nodes.pf.manager; ...@@ -22,12 +22,14 @@ package org.olat.course.nodes.pf.manager;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.FileVisitResult; import java.nio.file.FileVisitResult;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor; import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
...@@ -44,12 +46,15 @@ import org.olat.core.gui.media.ServletUtil; ...@@ -44,12 +46,15 @@ import org.olat.core.gui.media.ServletUtil;
import org.olat.core.gui.translator.Translator; import org.olat.core.gui.translator.Translator;
import org.olat.core.id.Identity; import org.olat.core.id.Identity;
import org.olat.core.logging.Tracing; import org.olat.core.logging.Tracing;
import org.olat.core.util.Formatter;
import org.olat.core.util.StringHelper; import org.olat.core.util.StringHelper;
import org.olat.core.util.Util; import org.olat.core.util.Util;
import org.olat.core.util.io.ShieldOutputStream;
import org.olat.core.util.io.SystemFileFilter; import org.olat.core.util.io.SystemFileFilter;
import org.olat.course.nodes.PFCourseNode; import org.olat.course.nodes.PFCourseNode;
import org.olat.course.nodes.pf.ui.PFParticipantController; import org.olat.course.nodes.pf.ui.PFParticipantController;
import org.olat.course.run.environment.CourseEnvironment; import org.olat.course.run.environment.CourseEnvironment;
import org.olat.repository.RepositoryEntry;
import org.olat.user.UserManager; import org.olat.user.UserManager;
/** /**
* *
...@@ -107,6 +112,14 @@ public class FileSystemExport implements MediaResource { ...@@ -107,6 +112,14 @@ public class FileSystemExport implements MediaResource {
@Override @Override
public void prepare(HttpServletResponse hres) { public void prepare(HttpServletResponse hres) {
RepositoryEntry entry = courseEnv.getCourseGroupManager().getCourseEntry();
String label = StringHelper.transformDisplayNameToFileSystemName(pfNode.getShortName() + "_" + entry.getDisplayname())
+ "_" + Formatter.formatDatetimeWithMinutes(new Date())
+ ".zip";
String urlEncodedLabel = StringHelper.urlEncodeUTF8(label);
hres.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + urlEncodedLabel);
hres.setHeader("Content-Description", urlEncodedLabel);
try (ZipOutputStream zout = new ZipOutputStream(hres.getOutputStream())) { try (ZipOutputStream zout = new ZipOutputStream(hres.getOutputStream())) {
zout.setLevel(9); zout.setLevel(9);
Path relPath = Paths.get(courseEnv.getCourseBaseContainer().getBasefile().getAbsolutePath(), Path relPath = Paths.get(courseEnv.getCourseBaseContainer().getBasefile().getAbsolutePath(),
...@@ -132,8 +145,8 @@ public class FileSystemExport implements MediaResource { ...@@ -132,8 +145,8 @@ public class FileSystemExport implements MediaResource {
* @param translator * @param translator
* @throws IOException Signals that an I/O exception has occurred. * @throws IOException Signals that an I/O exception has occurred.
*/ */
public static boolean fsToZip(ZipOutputStream zout, String zipPath, final Path sourceFolder, PFCourseNode pfNode, public static boolean fsToZip(final ZipOutputStream zout, String zipPath, final Path sourceFolder, PFCourseNode pfNode,
List<Identity> identities, Translator translator) { List<Identity> identities, final Translator translator) {
if(StringHelper.containsNonWhitespace(zipPath)) { if(StringHelper.containsNonWhitespace(zipPath)) {
if(!zipPath.endsWith("/")) { if(!zipPath.endsWith("/")) {
...@@ -143,7 +156,7 @@ public class FileSystemExport implements MediaResource { ...@@ -143,7 +156,7 @@ public class FileSystemExport implements MediaResource {
zipPath = ""; zipPath = "";
} }
final String targetPath = zipPath + translator.translate("participant.folder") + "/"; final String targetPath = zipPath;
final UserManager userManager = CoreSpringFactory.getImpl(UserManager.class); final UserManager userManager = CoreSpringFactory.getImpl(UserManager.class);
Set<String> idKeys = new HashSet<>(); Set<String> idKeys = new HashSet<>();
if (identities != null) { if (identities != null) {
...@@ -188,7 +201,7 @@ public class FileSystemExport implements MediaResource { ...@@ -188,7 +201,7 @@ public class FileSystemExport implements MediaResource {
String relPath = sourceFolder.relativize(file).toString(); String relPath = sourceFolder.relativize(file).toString();
if ((relPath = containsID(relPath)) != null && (relPath = boxesEnabled(relPath)) != null) { if ((relPath = containsID(relPath)) != null && (relPath = boxesEnabled(relPath)) != null) {
zout.putNextEntry(new ZipEntry(targetPath + relPath)); zout.putNextEntry(new ZipEntry(targetPath + relPath));
Files.copy(file, zout); copyFile(file, zout);
zout.closeEntry(); zout.closeEntry();
} }
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
...@@ -204,7 +217,6 @@ public class FileSystemExport implements MediaResource { ...@@ -204,7 +217,6 @@ public class FileSystemExport implements MediaResource {
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
}); });
zout.close();
return true; return true;
} catch (IOException e) { } catch (IOException e) {
log.error("Unable to export zip",e); log.error("Unable to export zip",e);
...@@ -212,4 +224,12 @@ public class FileSystemExport implements MediaResource { ...@@ -212,4 +224,12 @@ public class FileSystemExport implements MediaResource {
} }
} }
private static void copyFile(Path file, ZipOutputStream zout) {
try(OutputStream out= new ShieldOutputStream(zout)) {
Files.copy(file, zout);
} catch(Exception e) {
log.error("Cannot zip {}", file, e);
}
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment