use of org.olat.core.util.vfs.version.Versionable in project openolat by klemens.
the class FileCopyController method event.
@Override
public void event(UserRequest ureq, Controller source, Event event) {
if (source instanceof FileLinkChooserController) {
if (event == Event.DONE_EVENT || event == Event.CANCELLED_EVENT) {
fireEvent(ureq, FolderCommand.FOLDERCOMMAND_FINISHED);
} else if (event instanceof URLChoosenEvent) {
URLChoosenEvent choosenEvent = (URLChoosenEvent) event;
String url = choosenEvent.getURL();
if (url.indexOf("://") < 0) {
VFSContainer cContainer = folderComponent.getExternContainerForCopy();
VFSItem item = cContainer.resolve(url);
if (item instanceof VFSLeaf) {
sourceLeaf = (VFSLeaf) item;
String filename = sourceLeaf.getName();
VFSContainer tContainer = folderComponent.getCurrentContainer();
newFile = tContainer.createChildLeaf(filename);
if (newFile == null) {
existingVFSItem = (VFSLeaf) tContainer.resolve(filename);
fileAlreadyExists(ureq);
} else {
finishUpload(ureq);
}
} else {
fireEvent(ureq, FolderCommand.FOLDERCOMMAND_FINISHED);
}
} else {
fireEvent(ureq, FolderCommand.FOLDERCOMMAND_FINISHED);
}
}
} else if (source == overwriteDialog) {
if (event instanceof ButtonClickedEvent) {
ButtonClickedEvent buttonClickedEvent = (ButtonClickedEvent) event;
if (buttonClickedEvent.getPosition() == 0) {
// ok
if (existingVFSItem instanceof Versionable && ((Versionable) existingVFSItem).getVersions().isVersioned()) {
// new version
String relPath = null;
if (existingVFSItem instanceof OlatRootFileImpl) {
relPath = ((OlatRootFileImpl) existingVFSItem).getRelPath();
}
int maxNumOfRevisions = FolderConfig.versionsAllowed(relPath);
if (maxNumOfRevisions == 0) {
// someone play with the configuration
// Overwrite...
String fileName = existingVFSItem.getName();
existingVFSItem.delete();
newFile = folderComponent.getCurrentContainer().createChildLeaf(fileName);
// ... and notify listeners.
finishUpload(ureq);
} else {
removeAsListenerAndDispose(commentVersionCtr);
boolean locked = vfsLockManager.isLocked(existingVFSItem);
commentVersionCtr = new VersionCommentController(ureq, getWindowControl(), locked, true);
listenTo(commentVersionCtr);
removeAsListenerAndDispose(commentVersionDialogBox);
commentVersionDialogBox = new CloseableModalController(getWindowControl(), translate("save"), commentVersionCtr.getInitialComponent());
listenTo(commentVersionDialogBox);
commentVersionDialogBox.activate();
}
} else {
// if the file is locked, ask for unlocking it
if (vfsLockManager.isLocked(existingVFSItem)) {
removeAsListenerAndDispose(unlockCtr);
unlockCtr = new VersionCommentController(ureq, getWindowControl(), true, false);
listenTo(unlockCtr);
removeAsListenerAndDispose(unlockDialogBox);
unlockDialogBox = new CloseableModalController(getWindowControl(), translate("ok"), unlockCtr.getInitialComponent());
listenTo(unlockDialogBox);
unlockDialogBox.activate();
} else {
// Overwrite...
String fileName = existingVFSItem.getName();
existingVFSItem.delete();
newFile = folderComponent.getCurrentContainer().createChildLeaf(fileName);
// ... and notify listeners.
finishUpload(ureq);
}
}
} else if (buttonClickedEvent.getPosition() == 1) {
// not ok
// make newFile with the proposition of filename
newFile = folderComponent.getCurrentContainer().createChildLeaf(renamedFilename);
// ... and notify listeners.
finishUpload(ureq);
} else if (buttonClickedEvent.getPosition() == 2) {
// cancel
// cancel -> do nothing
} else {
throw new RuntimeException("Unknown button number " + buttonClickedEvent.getPosition());
}
}
} else if (source == lockedFileDialog) {
if (event instanceof ButtonClickedEvent) {
ButtonClickedEvent buttonClickedEvent = (ButtonClickedEvent) event;
switch(buttonClickedEvent.getPosition()) {
case 0:
{
// ... and notify listeners.
newFile = existingVFSItem;
finishUpload(ureq);
break;
}
case 1:
{
// cancel
fireEvent(ureq, FolderCommand.FOLDERCOMMAND_FINISHED);
break;
}
default:
throw new RuntimeException("Unknown button number " + buttonClickedEvent.getPosition());
}
}
} else if (source == commentVersionCtr) {
String comment = commentVersionCtr.getComment();
Roles roles = ureq.getUserSession().getRoles();
boolean locked = vfsLockManager.isLocked(existingVFSItem);
if (locked && !commentVersionCtr.keepLocked()) {
vfsLockManager.unlock(existingVFSItem, getIdentity(), roles);
}
commentVersionDialogBox.deactivate();
if (revisionListDialogBox != null) {
revisionListDialogBox.deactivate();
}
// ok, new version of the file
Versionable existingVersionableItem = (Versionable) existingVFSItem;
boolean ok = existingVersionableItem.getVersions().addVersion(ureq.getIdentity(), comment, sourceLeaf.getInputStream());
if (ok) {
newFile = existingVFSItem;
}
finishSuccessfullUpload(existingVFSItem.getName(), ureq);
} else if (source == unlockCtr) {
// Overwrite...
if (!unlockCtr.keepLocked()) {
vfsLockManager.unlock(existingVFSItem, getIdentity(), ureq.getUserSession().getRoles());
}
unlockDialogBox.deactivate();
newFile = existingVFSItem;
// ... and notify listeners.
finishSuccessfullUpload(existingVFSItem.getName(), ureq);
} else if (source == revisionListCtr) {
if (FolderCommandStatus.STATUS_CANCELED == revisionListCtr.getStatus()) {
revisionListDialogBox.deactivate();
// don't want to delete revisions
fireEvent(ureq, FolderCommand.FOLDERCOMMAND_FINISHED);
} else {
if (existingVFSItem instanceof Versionable && ((Versionable) existingVFSItem).getVersions().isVersioned()) {
revisionListDialogBox.deactivate();
Versionable versionable = (Versionable) existingVFSItem;
Versions versions = versionable.getVersions();
int maxNumOfRevisions = FolderConfig.versionsAllowed(null);
if (maxNumOfRevisions < 0 || maxNumOfRevisions > versions.getRevisions().size()) {
removeAsListenerAndDispose(commentVersionCtr);
boolean locked = vfsLockManager.isLocked(existingVFSItem);
commentVersionCtr = new VersionCommentController(ureq, getWindowControl(), locked, true);
listenTo(commentVersionCtr);
removeAsListenerAndDispose(commentVersionDialogBox);
commentVersionDialogBox = new CloseableModalController(getWindowControl(), translate("save"), commentVersionCtr.getInitialComponent());
listenTo(commentVersionDialogBox);
commentVersionDialogBox.activate();
} else {
removeAsListenerAndDispose(revisionListCtr);
revisionListCtr = new RevisionListController(ureq, getWindowControl(), versionable, false);
listenTo(revisionListCtr);
removeAsListenerAndDispose(revisionListDialogBox);
revisionListDialogBox = new CloseableModalController(getWindowControl(), translate("delete"), revisionListCtr.getInitialComponent());
listenTo(revisionListDialogBox);
revisionListDialogBox.activate();
}
}
}
}
}
use of org.olat.core.util.vfs.version.Versionable in project openolat by klemens.
the class FileUploadController method doUpload.
private void doUpload(UserRequest ureq) {
// check for available space
if (remainingQuotKB != -1 && (fileEl.getUploadFile().length() / 1024 > remainingQuotKB)) {
fileEl.setErrorKey("QuotaExceeded", null);
fileEl.getUploadFile().delete();
return;
}
String fileName = fileEl.getUploadFileName();
if (metaDataCtr != null && StringHelper.containsNonWhitespace(metaDataCtr.getFilename())) {
fileName = metaDataCtr.getFilename();
}
File uploadedFile = fileEl.getUploadFile();
if (resizeImg && fileName != null && imageExtPattern.matcher(fileName.toLowerCase()).find() && resizeEl.isSelected(0)) {
String extension = FileUtils.getFileSuffix(fileName);
File imageScaled = new File(uploadedFile.getParentFile(), "scaled_" + uploadedFile.getName() + "." + extension);
if (imageHelper.scaleImage(uploadedFile, extension, imageScaled, 1280, 1280, false) != null) {
// problem happen, special GIF's (see bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6358674)
// don't try to scale if not all ok
uploadedFile = imageScaled;
}
}
// check if such a filename does already exist
existingVFSItem = uploadVFSContainer.resolve(fileName);
if (existingVFSItem == null) {
uploadNewFile(ureq, uploadedFile, fileName);
} else {
// rename file and ask user what to do
if (!(existingVFSItem instanceof LocalImpl)) {
throw new AssertException("Can only LocalImpl VFS items, don't know what to do with file of type::" + existingVFSItem.getClass().getCanonicalName());
}
String renamedFilename = VFSManager.rename(uploadVFSContainer, existingVFSItem.getName());
newFile = uploadVFSContainer.createChildLeaf(renamedFilename);
// Copy content to tmp file
boolean success = false;
try (InputStream in = new FileInputStream(uploadedFile);
BufferedOutputStream out = new BufferedOutputStream(newFile.getOutputStream(false))) {
success = FileUtils.copy(in, out);
uploadedFile.delete();
} catch (IOException e) {
success = false;
}
if (success) {
boolean locked = vfsLockManager.isLockedForMe(existingVFSItem, getIdentity(), ureq.getUserSession().getRoles());
if (locked) {
// the file is locked and cannot be overwritten
lockedFileDialog(ureq, renamedFilename);
} else if (existingVFSItem instanceof Versionable && ((Versionable) existingVFSItem).getVersions().isVersioned()) {
uploadVersionedFile(ureq, renamedFilename);
} else {
askOverwriteOrRename(ureq, renamedFilename);
}
} else {
showError("failed");
status = FolderCommandStatus.STATUS_FAILED;
fireEvent(ureq, Event.FAILED_EVENT);
}
}
}
use of org.olat.core.util.vfs.version.Versionable in project openolat by klemens.
the class ZipUtil method unzip.
/**
* Unzip an inputstream to a directory using the versioning system of VFS
* @param zipLeaf The file to unzip
* @param targetDir The directory to unzip the file to
* @param the identity of who unzip the file
* @param versioning enabled or not
* @return True if successfull, false otherwise
*/
private static boolean unzip(InputStream in, VFSContainer targetDir, Identity identity, boolean versioning) {
ZipInputStream oZip = new ZipInputStream(in);
try {
// unzip files
ZipEntry oEntr = oZip.getNextEntry();
while (oEntr != null) {
if (oEntr.getName() != null && !oEntr.getName().startsWith(DIR_NAME__MACOSX)) {
if (oEntr.isDirectory()) {
// skip MacOSX specific metadata directory
// create directories
getAllSubdirs(targetDir, oEntr.getName(), identity, true);
} else {
// create file
VFSContainer createIn = targetDir;
String name = oEntr.getName();
// check if entry has directories which did not show up as
// directories above
int dirSepIndex = name.lastIndexOf('/');
if (dirSepIndex == -1) {
// try it windows style, backslash is also valid format
dirSepIndex = name.lastIndexOf('\\');
}
if (dirSepIndex > 0) {
// create subdirs
createIn = getAllSubdirs(targetDir, name.substring(0, dirSepIndex), identity, true);
if (createIn == null) {
if (log.isDebug())
log.debug("Error creating directory structure for zip entry: " + oEntr.getName());
return false;
}
name = name.substring(dirSepIndex + 1);
}
if (versioning) {
VFSLeaf newEntry = (VFSLeaf) createIn.resolve(name);
if (newEntry == null) {
newEntry = createIn.createChildLeaf(name);
OutputStream out = newEntry.getOutputStream(false);
if (!FileUtils.copy(oZip, out))
return false;
FileUtils.closeSafely(out);
} else if (newEntry instanceof Versionable) {
Versionable versionable = (Versionable) newEntry;
if (versionable.getVersions().isVersioned()) {
versionable.getVersions().addVersion(identity, "", oZip);
}
}
if (newEntry instanceof MetaTagged) {
MetaInfo info = ((MetaTagged) newEntry).getMetaInfo();
if (info != null) {
info.setAuthor(identity);
info.write();
}
}
} else {
VFSLeaf newEntry = createIn.createChildLeaf(name);
if (newEntry != null) {
OutputStream out = newEntry.getOutputStream(false);
if (!FileUtils.copy(oZip, out))
return false;
FileUtils.closeSafely(out);
}
if (newEntry instanceof MetaTagged) {
MetaInfo info = ((MetaTagged) newEntry).getMetaInfo();
if (info != null && identity != null) {
info.setAuthor(identity);
info.write();
}
}
}
}
}
oZip.closeEntry();
oEntr = oZip.getNextEntry();
}
} catch (IOException e) {
return false;
} finally {
FileUtils.closeSafely(oZip);
}
return true;
}
use of org.olat.core.util.vfs.version.Versionable in project openolat by klemens.
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 klemens.
the class NodeExportVisitor method writeObject.
/**
* Write a structure to an XML file in the course base path folder.
*
* @param fileName
* @param obj
*/
private void writeObject(String fileName, Object obj) {
VFSItem vfsItem = getCourseBaseContainer().resolve(fileName);
if (vfsItem == null) {
vfsItem = getCourseBaseContainer().createChildLeaf(fileName);
} else if (vfsItem.exists() && vfsItem instanceof Versionable) {
try {
VersionsFileManager.getInstance().addToRevisions((Versionable) vfsItem, null, "");
} catch (Exception e) {
log.error("Cannot versioned " + fileName, e);
}
}
XStream xstream = CourseXStreamAliases.getWriteCourseXStream();
XStreamHelper.writeObject(xstream, (VFSLeaf) vfsItem, obj);
}
Aggregations