Search in sources :

Example 11 with LocalImpl

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");
        }
    }
}
Also used : VFSItem(org.olat.core.util.vfs.VFSItem) FileChoosenEvent(org.olat.core.commons.controllers.filechooser.FileChoosenEvent) LocalImpl(org.olat.core.util.vfs.LocalImpl)

Example 12 with LocalImpl

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;
}
Also used : OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) ZipOutputStream(java.util.zip.ZipOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) VFSItem(org.olat.core.util.vfs.VFSItem) IOException(java.io.IOException) LocalImpl(org.olat.core.util.vfs.LocalImpl) BufferedOutputStream(java.io.BufferedOutputStream)

Example 13 with LocalImpl

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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) VFSContainer(org.olat.core.util.vfs.VFSContainer) ZipEntry(java.util.zip.ZipEntry) VFSItem(org.olat.core.util.vfs.VFSItem) IOException(java.io.IOException) LocalImpl(org.olat.core.util.vfs.LocalImpl)

Example 14 with LocalImpl

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);
        }
    }
}
Also used : Versionable(org.olat.core.util.vfs.version.Versionable) AssertException(org.olat.core.logging.AssertException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) LocalImpl(org.olat.core.util.vfs.LocalImpl) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream)

Example 15 with LocalImpl

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);
    }
}
Also used : VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) IOException(java.io.IOException) LocalImpl(org.olat.core.util.vfs.LocalImpl) File(java.io.File)

Aggregations

LocalImpl (org.olat.core.util.vfs.LocalImpl)18 File (java.io.File)10 IOException (java.io.IOException)10 VFSItem (org.olat.core.util.vfs.VFSItem)10 VFSContainer (org.olat.core.util.vfs.VFSContainer)8 BufferedOutputStream (java.io.BufferedOutputStream)4 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)4 BufferedInputStream (java.io.BufferedInputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 ZipEntry (java.util.zip.ZipEntry)2 ZipInputStream (java.util.zip.ZipInputStream)2 ZipOutputStream (java.util.zip.ZipOutputStream)2 FileChoosenEvent (org.olat.core.commons.controllers.filechooser.FileChoosenEvent)2 Translator (org.olat.core.gui.translator.Translator)2 AssertException (org.olat.core.logging.AssertException)2 OLATRuntimeException (org.olat.core.logging.OLATRuntimeException)2 LocalFileImpl (org.olat.core.util.vfs.LocalFileImpl)2