use of org.olat.core.util.vfs.VFSContainer in project OpenOLAT by OpenOLAT.
the class CmdUnzip method checkLockedFiles.
private List<String> checkLockedFiles(VFSLeaf vfsItem, VFSContainer currentContainer, Identity identity, Roles roles) {
String name = vfsItem.getName();
if (!name.toLowerCase().endsWith(".zip")) {
return Collections.emptyList();
}
boolean versioning = FolderConfig.versionsEnabled(currentContainer);
if (!versioning) {
// this command don't overwrite existing folders
return Collections.emptyList();
}
String sZipContainer = name.substring(0, name.length() - 4);
VFSItem zipContainer = currentContainer.resolve(sZipContainer);
if (zipContainer == null) {
return Collections.emptyList();
} else if (zipContainer instanceof VFSContainer) {
return ZipUtil.checkLockedFileBeforeUnzipNonStrict(vfsItem, (VFSContainer) zipContainer, identity, roles);
} else {
// replace a file with a folder ???
return Collections.emptyList();
}
}
use of org.olat.core.util.vfs.VFSContainer in project OpenOLAT by OpenOLAT.
the class CmdUnzip method execute.
public Controller execute(FolderComponent folderComponent, UserRequest ureq, WindowControl wContr, Translator trans) {
this.translator = trans;
FileSelection selection = new FileSelection(ureq, folderComponent.getCurrentContainerPath());
VFSContainer currentContainer = folderComponent.getCurrentContainer();
if (!(currentContainer.canWrite() == VFSConstants.YES))
throw new AssertException("Cannot unzip to folder. Writing denied.");
// check if command is executed on a file containing invalid filenames or paths - checks if the resulting folder has a valid name
if (selection.getInvalidFileNames().size() > 0) {
status = FolderCommandStatus.STATUS_INVALID_NAME;
return null;
}
List<String> lockedFiles = new ArrayList<String>();
for (String sItem : selection.getFiles()) {
VFSItem vfsItem = currentContainer.resolve(sItem);
if (vfsItem instanceof VFSLeaf) {
try {
Roles roles = ureq.getUserSession().getRoles();
lockedFiles.addAll(checkLockedFiles((VFSLeaf) vfsItem, currentContainer, ureq.getIdentity(), roles));
} catch (Exception e) {
String name = vfsItem == null ? "NULL" : vfsItem.getName();
getWindowControl().setError(translator.translate("FileUnzipFailed", new String[] { name }));
}
}
}
if (!lockedFiles.isEmpty()) {
String msg = FolderCommandHelper.renderLockedMessageAsHtml(trans, lockedFiles);
List<String> buttonLabels = Collections.singletonList(trans.translate("ok"));
lockedFiledCtr = activateGenericDialog(ureq, trans.translate("lock.title"), msg, buttonLabels, lockedFiledCtr);
return null;
}
VFSItem currentVfsItem = null;
try {
boolean fileNotExist = false;
for (String sItem : selection.getFiles()) {
currentVfsItem = currentContainer.resolve(sItem);
if (currentVfsItem != null && (currentVfsItem instanceof VFSLeaf)) {
if (!doUnzip((VFSLeaf) currentVfsItem, currentContainer, ureq, wContr)) {
status = FolderCommandStatus.STATUS_FAILED;
break;
}
} else {
fileNotExist = true;
break;
}
}
if (fileNotExist) {
status = FolderCommandStatus.STATUS_FAILED;
getWindowControl().setError(translator.translate("FileDoesNotExist"));
}
VFSContainer inheritingCont = VFSManager.findInheritingSecurityCallbackContainer(folderComponent.getRootContainer());
if (inheritingCont != null) {
VFSSecurityCallback secCallback = inheritingCont.getLocalSecurityCallback();
if (secCallback != null) {
SubscriptionContext subsContext = secCallback.getSubscriptionContext();
if (subsContext != null) {
NotificationsManager.getInstance().markPublisherNews(subsContext, ureq.getIdentity(), true);
}
}
}
} catch (IllegalArgumentException e) {
logError("Corrupted ZIP", e);
String name = currentVfsItem == null ? "NULL" : currentVfsItem.getName();
getWindowControl().setError(translator.translate("FileUnzipFailed", new String[] { name }));
}
return null;
}
use of org.olat.core.util.vfs.VFSContainer in project OpenOLAT by OpenOLAT.
the class FileUploadController method validateFormLogic.
@Override
protected boolean validateFormLogic(UserRequest ureq) {
// Check sub path
if (targetSubPath != null) {
String subPath = targetSubPath.getValue();
if (subPath != null) {
// Cleanup first
subPath = subPath.toLowerCase().trim();
if (!validSubPathPattern.matcher(subPath).matches()) {
targetSubPath.setErrorKey("subpath.error.characters", null);
return false;
} else {
// Fix mess with slashes and dots
// reduce doubled slashes with single slash
subPath = subPath.replaceAll("\\.*\\/+\\.*", "\\/");
// do it a second time to catch the double slashes created by previous replacement
subPath = subPath.replaceAll("\\/+", "\\/");
// remove slash at end
if (subPath.endsWith("/")) {
subPath = subPath.substring(0, subPath.length() - 1);
}
// single slash means no sub-directory
if (subPath.length() == 1 && subPath.startsWith("/")) {
subPath = "";
}
// fix missing slash at start
if (subPath.length() > 0 && !subPath.startsWith("/")) {
subPath = "/" + subPath;
}
// update in GUI so user sees how we optimized
targetSubPath.setValue(subPath);
}
// Now check if this path does not already exist
if (StringHelper.containsNonWhitespace(subPath)) {
// Try to resolve given rel path from current container
VFSItem uploadDir = currentContainer.resolve(subPath);
if (uploadDir != null) {
// already exists. this is fine, as long as it is a directory and not a file
if (!(uploadDir instanceof VFSContainer)) {
// error
targetSubPath.setErrorKey("subpath.error.dir.is.file", new String[] { subPath });
return false;
}
}
}
targetSubPath.clearError();
}
}
// Check file name
if (metaDataCtr != null && StringHelper.containsNonWhitespace(metaDataCtr.getFilename())) {
return validateFilename(metaDataCtr.getFilename(), metaDataCtr.getFilenameEl());
}
boolean allOk = validateFilename(fileEl);
return allOk;
}
use of org.olat.core.util.vfs.VFSContainer in project OpenOLAT by OpenOLAT.
the class CmdCreateFile method formOK.
@Override
protected void formOK(UserRequest ureq) {
// create the file
fileName = textElement.getValue();
VFSContainer currentContainer = folderComponent.getCurrentContainer();
VFSItem item = currentContainer.createChildLeaf(fileName);
if (item == null) {
status = FolderCommandStatus.STATUS_FAILED;
notifyFinished(ureq);
} else {
if (item instanceof MetaTagged) {
MetaInfo meta = ((MetaTagged) item).getMetaInfo();
meta.setAuthor(ureq.getIdentity());
if (licenseModule.isEnabled(licenseHandler)) {
License license = licenseService.createDefaultLicense(licenseHandler, getIdentity());
meta.setLicenseTypeKey(String.valueOf(license.getLicenseType().getKey()));
meta.setLicenseTypeName(license.getLicenseType().getName());
meta.setLicensor(license.getLicensor());
meta.setLicenseText(LicenseUIFactory.getLicenseText(license));
}
meta.write();
}
// start HTML editor with the folders root folder as base and the file
// path as a relative path from the root directory. But first check if the
// root directory is wirtable at all (e.g. not the case in users personal
// briefcase), and seach for the next higher directory that is writable.
String relFilePath = "/" + fileName;
// add current container path if not at root level
if (!folderComponent.getCurrentContainerPath().equals("/")) {
relFilePath = folderComponent.getCurrentContainerPath() + relFilePath;
}
VFSContainer writableRootContainer = folderComponent.getRootContainer();
ContainerAndFile result = VFSManager.findWritableRootFolderFor(writableRootContainer, relFilePath);
if (result != null) {
writableRootContainer = result.getContainer();
relFilePath = result.getFileName();
} else {
// use fallback that always work: current directory and current file
relFilePath = fileName;
writableRootContainer = folderComponent.getCurrentContainer();
}
if (relFilePath.endsWith(".html") || relFilePath.endsWith(".htm")) {
editorCtr = WysiwygFactory.createWysiwygController(ureq, getWindowControl(), writableRootContainer, relFilePath, true, true);
((HTMLEditorController) editorCtr).setNewFile(true);
} else {
editorCtr = new PlainTextEditorController(ureq, getWindowControl(), (VFSLeaf) writableRootContainer.resolve(relFilePath), "utf-8", true, true, null);
}
listenTo(editorCtr);
initialPanel.setContent(editorCtr.getInitialComponent());
}
}
use of org.olat.core.util.vfs.VFSContainer in project OpenOLAT by OpenOLAT.
the class FolderRunController method activatePath.
public void activatePath(UserRequest ureq, String path) {
if (path != null && path.length() > 0) {
VFSItem vfsItem = folderComponent.getRootContainer().resolve(path.endsWith("/") ? path.substring(0, path.length() - 1) : path);
if (vfsItem instanceof VFSLeaf) {
// could be a file - create the mapper - otherwise don't create one if it's a directory
// Create a mapper to deliver the auto-download of the file. We have to
// create a dedicated mapper here
// and can not reuse the standard briefcase way of file delivering, some
// very old fancy code
// Mapper is cleaned up automatically by basic controller
String baseUrl = registerMapper(ureq, new VFSContainerMapper(folderComponent.getRootContainer()));
// Trigger auto-download
DisplayOrDownloadComponent dordc = new DisplayOrDownloadComponent("downloadcomp", baseUrl + path);
folderContainer.put("autoDownloadComp", dordc);
if (path.lastIndexOf("/") > 0) {
String dirPath = path.substring(0, path.lastIndexOf("/"));
if (StringHelper.containsNonWhitespace(dirPath)) {
folderComponent.setCurrentContainerPath(dirPath);
}
}
} else if (vfsItem instanceof VFSContainer) {
if (StringHelper.containsNonWhitespace(path)) {
folderComponent.setCurrentContainerPath(path);
}
}
updatePathResource(ureq);
}
}
Aggregations