use of org.olat.core.util.vfs.VFSItem 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.VFSItem in project OpenOLAT by OpenOLAT.
the class CmdZip method formOK.
/**
* Creates a zipFile by using ZipUtil and fires Event.DONE_EVENT if successful.
*
* @see org.olat.core.commons.modules.bc.commands.AbstractCreateItemForm#formOK(org.olat.core.gui.UserRequest)
*/
@Override
protected void formOK(UserRequest ureq) {
String name = textElement.getValue();
if (!name.toLowerCase().endsWith(".zip")) {
name += ".zip";
}
VFSItem zipFile = currentContainer.createChildLeaf(name);
if (zipFile == null) {
fireEvent(ureq, Event.FAILED_EVENT);
return;
}
List<VFSItem> vfsFiles = new ArrayList<VFSItem>();
for (String fileName : selection.getFiles()) {
VFSItem item = currentContainer.resolve(fileName);
if (item != null) {
vfsFiles.add(item);
}
}
if (!ZipUtil.zip(vfsFiles, (VFSLeaf) zipFile, true)) {
// cleanup zip file
zipFile.delete();
status = FolderCommandStatus.STATUS_FAILED;
fireEvent(ureq, FOLDERCOMMAND_FINISHED);
} else {
if (zipFile instanceof MetaTagged) {
MetaInfo info = ((MetaTagged) zipFile).getMetaInfo();
if (info != null) {
info.setAuthor(ureq.getIdentity());
info.write();
}
}
fireEvent(ureq, new FolderEvent(FolderEvent.ZIP_EVENT, selection.renderAsHtml()));
fireEvent(ureq, FolderCommand.FOLDERCOMMAND_FINISHED);
}
}
use of org.olat.core.util.vfs.VFSItem in project OpenOLAT by OpenOLAT.
the class CmdZip method validateFormLogic.
/**
* Checks if input valid.
* @see org.olat.core.commons.modules.bc.commands.AbstractCreateItemForm#validateFormLogic(org.olat.core.gui.UserRequest)
*/
@Override
protected boolean validateFormLogic(UserRequest ureq) {
boolean isInputValid = true;
String name = textElement.getValue();
if (name == null || name.trim().equals("")) {
textElement.setErrorKey("zip.name.empty", new String[0]);
isInputValid = false;
} else {
if (!validateFileName(name)) {
textElement.setErrorKey("zip.name.notvalid", new String[0]);
isInputValid = false;
return isInputValid;
}
// Note: use java.io.File and not VFS to create a leaf. File must not exist upon ZipUtil.zip()
name = name + ".zip";
VFSItem zipFile = currentContainer.resolve(name);
if (zipFile != null) {
textElement.setErrorKey("zip.alreadyexists", new String[] { name });
isInputValid = false;
} else {
isInputValid = true;
}
}
return isInputValid;
}
use of org.olat.core.util.vfs.VFSItem 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.VFSItem 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());
}
}
Aggregations