Skip to content
Snippets Groups Projects
Commit 84fb3480 authored by srosse's avatar srosse
Browse files

OO-1397: zip on the fly scorm and cp content for download

parent bf198c10
No related branches found
No related tags found
No related merge requests found
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.fileresource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.olat.core.gui.media.MediaResource;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.core.util.FileUtils;
import org.olat.core.util.StringHelper;
/**
*
* Initial date: 20.01.2015<br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
public class ZippedDirectoryMediaResource implements MediaResource {
private static final OLog log = Tracing.createLoggerFor(ZippedDirectoryMediaResource.class);
private final String filename;
private final File unzipDir;
public ZippedDirectoryMediaResource(String filename, File unzipDir) {
this.filename = filename;
this.unzipDir = unzipDir;
}
@Override
public String getContentType() {
return "application/zip";
}
@Override
public Long getSize() {
return null;
}
@Override
public InputStream getInputStream() {
return null;
}
@Override
public Long getLastModified() {
return null;
}
@Override
public void prepare(HttpServletResponse hres) {
String label = StringHelper.transformDisplayNameToFileSystemName(filename) + ".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())) {
zout.setLevel(9);
final Path unzipPath = unzipDir.toPath();
Files.walkFileTree(unzipPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if(!attrs.isDirectory()) {
Path relativeFile = unzipPath.relativize(file);
String names = relativeFile.toString();
zout.putNextEntry(new ZipEntry(names));
try(InputStream in=Files.newInputStream(file)) {
FileUtils.copy(in, zout);
} catch (Exception e) {
log.error("", e);
}
zout.closeEntry();
}
return FileVisitResult.CONTINUE;
}
});
} catch (Exception e) {
log.error("", e);
}
}
@Override
public void release() {
//
}
}
......@@ -176,7 +176,6 @@ public class CPManagerImpl extends CPManager {
File cpRoot = FileResourceManager.getInstance().unzipFileResource(ores);
logDebug("createNewCP: cpRoot=" + cpRoot);
logDebug("createNewCP: cpRoot.getAbsolutePath()=" + cpRoot.getAbsolutePath());
System.out.println("createNewCP: cpRoot.getAbsolutePath()=" + cpRoot.getAbsolutePath());
LocalFolderImpl vfsWrapper = new LocalFolderImpl(cpRoot);
ContentPackage cp = load(vfsWrapper, ores);
......
......@@ -36,9 +36,11 @@ import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.control.generic.layout.MainLayoutController;
import org.olat.core.gui.control.generic.wizard.StepsMainRunController;
import org.olat.core.gui.media.MediaResource;
import org.olat.core.id.Identity;
import org.olat.core.id.OLATResourceable;
import org.olat.core.logging.AssertException;
import org.olat.fileresource.FileResourceManager;
import org.olat.fileresource.types.ResourceEvaluation;
import org.olat.ims.qti.QTIRuntimeController;
import org.olat.ims.qti.editor.QTIEditorMainController;
......@@ -102,6 +104,11 @@ public class QTISurveyHandler extends QTIHandler {
return super.importResource(initialAuthor, displayname, description, new SurveyFileResource(), file, filename);
}
@Override
public MediaResource getAsMediaResource(OLATResourceable res, boolean backwardsCompatible) {
return FileResourceManager.getInstance().getAsDownloadeableMediaResource(res);
}
@Override
public String getSupportedType() {
return SurveyFileResource.TYPE_NAME;
......
......@@ -36,9 +36,11 @@ import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.control.generic.layout.MainLayoutController;
import org.olat.core.gui.control.generic.wizard.StepsMainRunController;
import org.olat.core.gui.media.MediaResource;
import org.olat.core.id.Identity;
import org.olat.core.id.OLATResourceable;
import org.olat.core.logging.AssertException;
import org.olat.fileresource.FileResourceManager;
import org.olat.fileresource.types.ResourceEvaluation;
import org.olat.ims.qti.QTIRuntimeController;
import org.olat.ims.qti.editor.QTIEditorMainController;
......@@ -102,6 +104,11 @@ public class QTITestHandler extends QTIHandler {
return super.importResource(initialAuthor, displayname, description, new TestFileResource(), file, filename);
}
@Override
public MediaResource getAsMediaResource(OLATResourceable res, boolean backwardsCompatible) {
return FileResourceManager.getInstance().getAsDownloadeableMediaResource(res);
}
@Override
public String getSupportedType() {
return TestFileResource.TYPE_NAME;
......
......@@ -55,11 +55,6 @@ public abstract class FileHandler implements RepositoryHandler {
//
}
@Override
public MediaResource getAsMediaResource(OLATResourceable res, boolean backwardsCompatible) {
return FileResourceManager.getInstance().getAsDownloadeableMediaResource(res);
}
@Override
public VFSContainer getMediaContainer(RepositoryEntry repoEntry) {
return FileResourceManager.getInstance()
......
......@@ -41,6 +41,7 @@ import org.olat.core.gui.control.generic.iframe.DeliveryOptions;
import org.olat.core.gui.control.generic.layout.MainLayout3ColumnsController;
import org.olat.core.gui.control.generic.layout.MainLayoutController;
import org.olat.core.gui.control.generic.wizard.StepsMainRunController;
import org.olat.core.gui.media.MediaResource;
import org.olat.core.gui.translator.Translator;
import org.olat.core.id.Identity;
import org.olat.core.id.OLATResourceable;
......@@ -54,6 +55,7 @@ import org.olat.core.util.vfs.QuotaManager;
import org.olat.core.util.vfs.callbacks.FullAccessWithQuotaCallback;
import org.olat.core.util.vfs.callbacks.VFSSecurityCallback;
import org.olat.fileresource.FileResourceManager;
import org.olat.fileresource.ZippedDirectoryMediaResource;
import org.olat.fileresource.types.FileResource;
import org.olat.fileresource.types.ImsCPFileResource;
import org.olat.fileresource.types.ResourceEvaluation;
......@@ -65,6 +67,7 @@ import org.olat.ims.cp.ui.CPRuntimeController;
import org.olat.modules.cp.CPDisplayController;
import org.olat.modules.cp.CPOfflineReadableManager;
import org.olat.repository.RepositoryEntry;
import org.olat.repository.RepositoryManager;
import org.olat.repository.RepositoryService;
import org.olat.repository.model.RepositoryEntrySecurity;
import org.olat.repository.ui.RepositoryEntryRuntimeController.RuntimeControllerCreator;
......@@ -155,6 +158,14 @@ public class ImsCPHandler extends FileHandler {
return target;
}
@Override
public MediaResource getAsMediaResource(OLATResourceable res, boolean backwardsCompatible) {
File unzippedDir = FileResourceManager.getInstance().unzipFileResource(res);
String displayName = CoreSpringFactory.getImpl(RepositoryManager.class)
.lookupDisplayNameByOLATResourceableId(res.getResourceableId());
return new ZippedDirectoryMediaResource(displayName, unzippedDir);
}
@Override
public String getSupportedType() {
return ImsCPFileResource.TYPE_NAME;
......
......@@ -36,6 +36,7 @@ import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.control.generic.layout.MainLayoutController;
import org.olat.core.gui.control.generic.wizard.StepsMainRunController;
import org.olat.core.gui.media.MediaResource;
import org.olat.core.id.Identity;
import org.olat.core.id.OLATResourceable;
import org.olat.core.logging.AssertException;
......@@ -43,6 +44,7 @@ import org.olat.core.logging.activity.ThreadLocalUserActivityLogger;
import org.olat.core.util.FileUtils;
import org.olat.core.util.coordinate.LockResult;
import org.olat.fileresource.FileResourceManager;
import org.olat.fileresource.ZippedDirectoryMediaResource;
import org.olat.fileresource.types.FileResource;
import org.olat.fileresource.types.ResourceEvaluation;
import org.olat.fileresource.types.ScormCPFileResource;
......@@ -50,6 +52,7 @@ import org.olat.modules.scorm.ScormMainManager;
import org.olat.modules.scorm.ScormPackageConfig;
import org.olat.modules.scorm.ScormRuntimeController;
import org.olat.repository.RepositoryEntry;
import org.olat.repository.RepositoryManager;
import org.olat.repository.RepositoryService;
import org.olat.repository.model.RepositoryEntrySecurity;
import org.olat.repository.ui.RepositoryEntryRuntimeController.RuntimeControllerCreator;
......@@ -127,6 +130,14 @@ public class SCORMCPHandler extends FileHandler {
}
return target;
}
@Override
public MediaResource getAsMediaResource(OLATResourceable res, boolean backwardsCompatible) {
File unzippedDir = FileResourceManager.getInstance().unzipFileResource(res);
String displayName = CoreSpringFactory.getImpl(RepositoryManager.class)
.lookupDisplayNameByOLATResourceableId(res.getResourceableId());
return new ZippedDirectoryMediaResource(displayName, unzippedDir);
}
@Override
public String getSupportedType() {
......
......@@ -39,6 +39,7 @@ import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.control.generic.layout.MainLayoutController;
import org.olat.core.gui.control.generic.wizard.StepsMainRunController;
import org.olat.core.gui.media.MediaResource;
import org.olat.core.id.Identity;
import org.olat.core.id.OLATResourceable;
import org.olat.core.logging.AssertException;
......@@ -186,6 +187,11 @@ public class WebDocumentHandler extends FileHandler {
return target;
}
@Override
public MediaResource getAsMediaResource(OLATResourceable res, boolean backwardsCompatible) {
return FileResourceManager.getInstance().getAsDownloadeableMediaResource(res);
}
@Override
public String getSupportedType() {
return supportedType;
......
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