use of org.olat.core.util.vfs.version.Versionable in project OpenOLAT by OpenOLAT.
the class FileCopyController method fileAlreadyExists.
private void fileAlreadyExists(UserRequest ureq) {
renamedFilename = proposedRenamedFilename(existingVFSItem);
boolean locked = vfsLockManager.isLockedForMe(existingVFSItem, getIdentity(), ureq.getUserSession().getRoles());
if (locked) {
// the file is locked and cannot be overwritten
removeAsListenerAndDispose(lockedFileDialog);
lockedFileDialog = DialogBoxUIFactory.createGenericDialog(ureq, getWindowControl(), translate("ul.lockedFile.title"), translate("ul.lockedFile.text", new String[] { existingVFSItem.getName(), renamedFilename }), asList(translate("ul.overwrite.threeoptions.rename", renamedFilename), translate("ul.overwrite.threeoptions.cancel")));
listenTo(lockedFileDialog);
lockedFileDialog.activate();
} else if (existingVFSItem instanceof Versionable && ((Versionable) existingVFSItem).getVersions().isVersioned()) {
Versionable versionable = (Versionable) existingVFSItem;
Versions versions = versionable.getVersions();
String relPath = null;
if (existingVFSItem instanceof OlatRootFileImpl) {
relPath = ((OlatRootFileImpl) existingVFSItem).getRelPath();
}
int maxNumOfRevisions = FolderConfig.versionsAllowed(relPath);
if (maxNumOfRevisions == 0) {
// it's possible if someone change the configuration
// let calling method decide what to do.
removeAsListenerAndDispose(overwriteDialog);
overwriteDialog = DialogBoxUIFactory.createGenericDialog(ureq, getWindowControl(), translate("ul.overwrite.threeoptions.title"), translate("ul.overwrite.threeoptions.text", new String[] { existingVFSItem.getName(), renamedFilename }), asList(translate("ul.overwrite.threeoptions.overwrite"), translate("ul.overwrite.threeoptions.rename", renamedFilename), translate("ul.overwrite.threeoptions.cancel")));
listenTo(overwriteDialog);
overwriteDialog.activate();
} else if (versions.getRevisions().isEmpty() || maxNumOfRevisions < 0 || maxNumOfRevisions > versions.getRevisions().size()) {
// let calling method decide what to do.
removeAsListenerAndDispose(overwriteDialog);
overwriteDialog = DialogBoxUIFactory.createGenericDialog(ureq, getWindowControl(), translate("ul.overwrite.threeoptions.title"), translate("ul.versionoroverwrite", new String[] { existingVFSItem.getName(), renamedFilename }), asList(translate("ul.overwrite.threeoptions.newVersion"), translate("ul.overwrite.threeoptions.rename", renamedFilename), translate("ul.overwrite.threeoptions.cancel")));
listenTo(overwriteDialog);
overwriteDialog.activate();
} else {
String title = translate("ul.tooManyRevisions.title", new String[] { Integer.toString(maxNumOfRevisions), Integer.toString(versions.getRevisions().size()) });
String description = translate("ul.tooManyRevisions.description", new String[] { Integer.toString(maxNumOfRevisions), Integer.toString(versions.getRevisions().size()) });
removeAsListenerAndDispose(revisionListCtr);
revisionListCtr = new RevisionListController(ureq, getWindowControl(), versionable, null, description, false);
listenTo(revisionListCtr);
removeAsListenerAndDispose(revisionListDialogBox);
revisionListDialogBox = new CloseableModalController(getWindowControl(), translate("delete"), revisionListCtr.getInitialComponent(), true, title);
listenTo(revisionListDialogBox);
revisionListDialogBox.activate();
}
} else {
// let calling method decide what to do.
// for this, we put a list with "existing name" and "new name"
overwriteDialog = DialogBoxUIFactory.createGenericDialog(ureq, getWindowControl(), translate("ul.overwrite.threeoptions.title"), translate("ul.overwrite.threeoptions.text", new String[] { existingVFSItem.getName(), renamedFilename }), asList(translate("ul.overwrite.threeoptions.overwrite"), translate("ul.overwrite.threeoptions.rename", renamedFilename), translate("ul.overwrite.threeoptions.cancel")));
listenTo(overwriteDialog);
overwriteDialog.activate();
}
}
use of org.olat.core.util.vfs.version.Versionable in project OpenOLAT by OpenOLAT.
the class VFSResourceRoot method write.
@Override
public boolean write(String path, InputStream is, boolean overwrite, WebResource movedFrom) throws QuotaExceededException {
VFSLeaf childLeaf;
VFSItem file = resolveFile(path);
if (file instanceof VFSLeaf) {
if (overwrite) {
// overwrite the file
childLeaf = (VFSLeaf) file;
// versioning
if (childLeaf instanceof Versionable && ((Versionable) childLeaf).getVersions().isVersioned()) {
if (childLeaf.getSize() == 0) {
VersionsManager.getInstance().createVersionsFor(childLeaf, true);
} else {
VersionsManager.getInstance().addToRevisions((Versionable) childLeaf, identity, "");
}
}
} else {
return false;
}
} else if (file instanceof VFSContainer) {
return false;
} else {
// create a new file
int lastSlash = path.lastIndexOf('/');
if (lastSlash == -1)
return false;
String parentPath = path.substring(0, lastSlash);
VFSItem parentItem = resolveFile(parentPath);
if (parentItem instanceof VFSContainer) {
VFSContainer folder = (VFSContainer) parentItem;
String name = path.substring(lastSlash + 1);
childLeaf = folder.createChildLeaf(name);
} else {
return false;
}
}
if (childLeaf == null) {
return false;
}
try {
copyVFS(childLeaf, is);
} catch (QuotaExceededException e) {
throw e;
} catch (Exception e) {
log.error("", e);
return false;
}
VFSContainer inheritingCont = VFSManager.findInheritingSecurityCallbackContainer(childLeaf.getParentContainer());
if (inheritingCont != null) {
VFSSecurityCallback callback = inheritingCont.getLocalSecurityCallback();
if (callback != null && callback.getSubscriptionContext() != null) {
SubscriptionContext subContext = callback.getSubscriptionContext();
NotificationsManager.getInstance().markPublisherNews(subContext, null, true);
}
}
if (childLeaf instanceof MetaTagged && identity != null) {
MetaInfo infos = ((MetaTagged) childLeaf).getMetaInfo();
if (infos != null && !infos.hasAuthorIdentity()) {
infos.setAuthor(identity);
addLicense(infos, identity);
infos.clearThumbnails();
// infos.write(); the clearThumbnails call write()
}
}
if (movedFrom instanceof VFSResource) {
VFSResource vfsResource = (VFSResource) movedFrom;
if (vfsResource.getItem() instanceof Versionable && ((Versionable) vfsResource.getItem()).getVersions().isVersioned()) {
VFSLeaf currentVersion = (VFSLeaf) vfsResource.getItem();
VersionsManager.getInstance().move(currentVersion, childLeaf, identity);
}
}
return true;
}
use of org.olat.core.util.vfs.version.Versionable in project OpenOLAT by OpenOLAT.
the class LocalFolderImpl method copyFrom.
/**
* Internal copy from, preventing quota checks on subfolders.
*
* @param source
* @param checkQuota
* @return
*/
private VFSStatus copyFrom(VFSItem source, boolean checkQuota) {
if (source.canCopy() != VFSConstants.YES)
throw new RuntimeException("cannot copy from");
String sourcename = source.getName();
File basefile = getBasefile();
// check if there is already an item with the same name...
if (resolve(sourcename) != null)
return VFSConstants.ERROR_NAME_ALREDY_USED;
// add either file bla.txt or folder blu as a child of this folder
if (source instanceof VFSContainer) {
// copy recursively
VFSContainer sourcecontainer = (VFSContainer) source;
// check if this is a containing container...
if (VFSManager.isSelfOrParent(sourcecontainer, this))
return VFSConstants.ERROR_OVERLAPPING;
// "copy" the container means creating a folder with that name
// and let the children copy
// create the folder
File outdir = new File(basefile, sourcename);
outdir.mkdir();
LocalFolderImpl rootcopyfolder = new LocalFolderImpl(outdir, this);
List<VFSItem> children = sourcecontainer.getItems();
for (VFSItem chd : children) {
VFSStatus status = rootcopyfolder.copyFrom(chd, false);
if (status != VFSConstants.SUCCESS)
return status;
}
} else if (source instanceof VFSLeaf) {
// copy single item
VFSLeaf s = (VFSLeaf) source;
// check quota
if (checkQuota) {
long quotaLeft = VFSManager.getQuotaLeftKB(this);
if (quotaLeft != Quota.UNLIMITED && quotaLeft < (s.getSize() / 1024))
return VFSConstants.ERROR_QUOTA_EXCEEDED;
}
try {
FileUtils.bcopy(s.getInputStream(), new File(basefile, sourcename), "VFScopyFrom");
} catch (Exception e) {
return VFSConstants.ERROR_FAILED;
}
if (s instanceof Versionable && ((Versionable) s).getVersions().isVersioned()) {
((Versionable) s).getVersions().move(this);
}
} else
throw new RuntimeException("neither a leaf nor a container!");
return VFSConstants.SUCCESS;
}
use of org.olat.core.util.vfs.version.Versionable in project OpenOLAT by OpenOLAT.
the class HTMLEditorController method doSaveData.
/**
* Event implementation for savedata
*
* @param ureq
*/
public boolean doSaveData() {
// No XSS checks, are done in the HTML editor - users can upload illegal
// stuff, JS needs to be enabled for users
String content = htmlElement.getRawValue();
// If preface was null -> append own head and save it in utf-8. Preface
// is the header that was in the file when we opened the file
StringBuilder fileContent = new StringBuilder();
if (preface == null) {
fileContent.append(DOCTYPE).append(OPEN_HTML).append(OPEN_HEAD);
fileContent.append(GENERATOR_META).append(UTF8CHARSET);
// In new documents, create empty title to be W3C conform. Title
// is mandatory element in meta element.
fileContent.append(EMTPY_TITLE);
fileContent.append(CLOSE_HEAD_OPEN_BODY);
fileContent.append(content);
fileContent.append(CLOSE_BODY_HTML);
// use utf-8 by default for new files
charSet = UTF_8;
} else {
// existing preface, just reinsert so we don't lose stuff the user put
// in there
fileContent.append(preface).append(content).append(CLOSE_BODY_HTML);
}
int fileSize = fileContent.toString().getBytes().length;
if (fileSize >= FolderConfig.getMaxEditSizeLimit()) {
String msg = translate("file.too.large.server", new String[] { (fileSize / 1000) + "", (FolderConfig.getMaxEditSizeLimit() / 1000) + "" });
getWindowControl().setError(msg);
return false;
}
// save the file
if (versionsEnabled && fileLeaf instanceof Versionable && ((Versionable) fileLeaf).getVersions().isVersioned()) {
InputStream inStream = FileUtils.getInputStream(fileContent.toString(), charSet);
((Versionable) fileLeaf).getVersions().addVersion(getIdentity(), "", inStream);
} else {
FileUtils.save(fileLeaf.getOutputStream(false), fileContent.toString(), charSet);
}
// Update last modified date in view
long lm = fileLeaf.getLastModified();
metadataVC.contextPut("lastModified", Formatter.getInstance(getLocale()).formatDateAndTime(new Date(lm)));
// Set new content as default value in element
htmlElement.setNewOriginalValue(content);
return true;
}
use of org.olat.core.util.vfs.version.Versionable in project OpenOLAT by OpenOLAT.
the class TextForm method event.
@Override
protected void event(UserRequest ureq, Controller source, Event event) {
if (source == tf && event == Event.DONE_EVENT) {
if (!readOnly) {
if ((!newFile) && vfsfile instanceof Versionable && ((Versionable) vfsfile).getVersions().isVersioned()) {
InputStream inStream = FileUtils.getInputStream(tf.getTextValue(), encoding);
((Versionable) vfsfile).getVersions().addVersion(ureq.getIdentity(), "", inStream);
} else {
FileUtils.save(vfsfile.getOutputStream(false), tf.getTextValue(), encoding);
}
}
fireEvent(ureq, Event.DONE_EVENT);
}
}
Aggregations