use of org.olat.core.util.vfs.LocalImpl in project openolat by klemens.
the class GuiDemoFileChooserController method event.
/**
* @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest, org.olat.core.gui.control.Controller, org.olat.core.gui.control.Event)
*/
protected void event(UserRequest ureq, Controller source, Event event) {
if (source == chooserCtr) {
// catch the events from the file chooser controller here
if (event instanceof FileChoosenEvent) {
// User pressed select button, get selected item from controller
VFSItem selectedItem = FileChooserUIFactory.getSelectedItem((FileChoosenEvent) event);
// for this demo just get file path and display an info message
LocalImpl localFile = (LocalImpl) selectedItem;
String absPath = localFile.getBasefile().getAbsolutePath();
String relPath = absPath.substring(webappRootFile.getAbsolutePath().length());
getWindowControl().setInfo("user selected /static" + relPath);
} else if (event == Event.CANCELLED_EVENT) {
// user pressed cancel
getWindowControl().setInfo("user cancelled");
} else if (event == Event.FAILED_EVENT) {
// selection failed for unknown reason
getWindowControl().setError("Ups, an error occured");
}
}
}
use of org.olat.core.util.vfs.LocalImpl in project OpenOLAT by OpenOLAT.
the class ZipUtil method zip.
// zip
public static boolean zip(List<VFSItem> vfsFiles, VFSLeaf target, boolean compress) {
boolean success = true;
String zname = target.getName();
if (target instanceof LocalImpl) {
zname = ((LocalImpl) target).getBasefile().getAbsolutePath();
}
OutputStream out = target.getOutputStream(false);
if (out == null) {
throw new OLATRuntimeException(ZipUtil.class, "Error getting output stream for file: " + zname, null);
}
long s = System.currentTimeMillis();
ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(out, FileUtils.BSIZE));
if (vfsFiles.size() == 0) {
try {
zipOut.close();
} catch (IOException e) {
//
}
return true;
}
zipOut.setLevel(compress ? 9 : 0);
for (Iterator<VFSItem> iter = vfsFiles.iterator(); success && iter.hasNext(); ) {
success = addToZip(iter.next(), "", zipOut);
}
try {
zipOut.flush();
zipOut.close();
log.info("zipped (" + (compress ? "compress" : "store") + ") " + zname + " t=" + Long.toString(System.currentTimeMillis() - s));
} catch (IOException e) {
throw new OLATRuntimeException(ZipUtil.class, "I/O error closing file: " + zname, null);
}
return success;
}
use of org.olat.core.util.vfs.LocalImpl in project OpenOLAT by OpenOLAT.
the class ZipUtil method addToZip.
public static boolean addToZip(VFSItem vfsItem, String currentPath, ZipOutputStream out) {
boolean success = true;
InputStream in = null;
byte[] buffer = new byte[FileUtils.BSIZE];
try {
// The separator / is the separator defined by the ZIP standard
String itemName = currentPath.length() == 0 ? vfsItem.getName() : currentPath + "/" + vfsItem.getName();
if (vfsItem instanceof VFSContainer) {
out.putNextEntry(new ZipEntry(itemName + "/"));
out.closeEntry();
List<VFSItem> items = ((VFSContainer) vfsItem).getItems();
for (Iterator<VFSItem> iter = items.iterator(); iter.hasNext(); ) {
if (!addToZip(iter.next(), itemName, out)) {
success = false;
break;
}
}
} else {
out.putNextEntry(new ZipEntry(itemName));
in = ((VFSLeaf) vfsItem).getInputStream();
int c;
while ((c = in.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, c);
}
out.closeEntry();
}
} catch (IOException ioe) {
String name = vfsItem.getName();
if (vfsItem instanceof LocalImpl) {
name = ((LocalImpl) vfsItem).getBasefile().getAbsolutePath();
}
log.error("I/O error while adding " + name + " to zip:" + ioe);
return false;
} finally {
FileUtils.closeSafely(in);
}
return success;
}
use of org.olat.core.util.vfs.LocalImpl in project OpenOLAT by OpenOLAT.
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.LocalImpl in project openolat by klemens.
the class OLATUpgrade_10_3_0 method processExportFolder.
private void processExportFolder(RepositoryEntry courseRe) {
try {
VFSContainer courseFolder = CourseFactory.getCourseBaseContainer(courseRe.getOlatResource().getResourceableId());
VFSItem exportFolder = courseFolder.resolve(ICourse.EXPORTED_DATA_FOLDERNAME);
if (exportFolder != null && exportFolder.exists() && exportFolder instanceof LocalImpl) {
File exportDir = ((LocalImpl) exportFolder).getBasefile();
if (exportDir.exists()) {
FileUtils.deleteDirsAndFiles(exportDir.toPath());
}
}
} catch (IOException e) {
log.error("", e);
}
}
Aggregations