Search in sources :

Example 16 with VFSLeaf

use of org.olat.core.util.vfs.VFSLeaf in project OpenOLAT by OpenOLAT.

the class FileElementImpl method evalFormRequest.

@Override
public void evalFormRequest(UserRequest ureq) {
    Form form = getRootForm();
    String dispatchuri = form.getRequestParameter("dispatchuri");
    if (dispatchuri != null && dispatchuri.equals(component.getFormDispatchId())) {
        if ("delete".equals(form.getRequestParameter("delete"))) {
            if (isConfirmDelete()) {
                doConfirmDelete(ureq);
            } else {
                getRootForm().fireFormEvent(ureq, new FileElementEvent(FileElementEvent.DELETE, this, FormEvent.ONCLICK));
            }
        }
    }
    Set<String> keys = form.getRequestMultipartFilesSet();
    if (keys.size() > 0 && keys.contains(component.getFormDispatchId())) {
        // Remove old files first
        if (tempUploadFile != null && tempUploadFile.exists()) {
            tempUploadFile.delete();
        }
        // Move file from a temporary request scope location to a location
        // with a
        // temporary form item scope. The file must be moved later using the
        // moveUploadFileTo() method to the final destination.
        tempUploadFile = new File(WebappHelper.getTmpDir(), CodeHelper.getUniqueID());
        File tmpRequestFile = form.getRequestMultipartFile(component.getFormDispatchId());
        // Move file to internal temp location
        boolean success = tmpRequestFile.renameTo(tempUploadFile);
        if (!success) {
            // try to move file by copying it, command above might fail
            // when source and target are on different volumes
            FileUtils.copyFileToFile(tmpRequestFile, tempUploadFile, true);
        }
        uploadFilename = form.getRequestMultipartFileName(component.getFormDispatchId());
        // prevent an issue with Firefox
        uploadFilename = FileUtils.normalizeFilenameWithSuffix(uploadFilename);
        // use mime-type from file name to have deterministic mime types
        uploadMimeType = WebappHelper.getMimeType(uploadFilename);
        if (uploadMimeType == null) {
            // use browser mime type as fallback if unknown
            uploadMimeType = form.getRequestMultipartFileMimeType(component.getFormDispatchId());
        }
        if (uploadMimeType == null) {
            // use application fallback for worst case
            uploadMimeType = "application/octet-stream";
        }
        if (previewEl != null && uploadMimeType != null && (uploadMimeType.startsWith("image/") || uploadMimeType.startsWith("video/"))) {
            VFSLeaf media = new LocalFileImpl(tempUploadFile);
            previewEl.setMedia(media, uploadMimeType);
            previewEl.setCropSelectionEnabled(cropSelectionEnabled);
            previewEl.setMaxWithAndHeightToFitWithin(300, 200);
            previewEl.setVisible(true);
        } else if (previewEl != null) {
            previewEl.setVisible(false);
        }
        // Mark associated component dirty, that it gets rerendered
        component.setDirty(true);
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) Form(org.olat.core.gui.components.form.flexible.impl.Form) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) File(java.io.File)

Example 17 with VFSLeaf

use of org.olat.core.util.vfs.VFSLeaf in project OpenOLAT by OpenOLAT.

the class FileElementImpl method reset.

@Override
public void reset() {
    if (tempUploadFile != null && tempUploadFile.exists()) {
        tempUploadFile.delete();
    }
    tempUploadFile = null;
    if (previewEl != null) {
        if (initialFile != null) {
            VFSLeaf media = new LocalFileImpl(initialFile);
            previewEl.setMedia(media);
            previewEl.setMaxWithAndHeightToFitWithin(300, 200);
            previewEl.setVisible(true);
        } else if (previewEl != null) {
            previewEl.setVisible(false);
        }
    }
    uploadFilename = null;
    uploadMimeType = null;
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl)

Example 18 with VFSLeaf

use of org.olat.core.util.vfs.VFSLeaf in project OpenOLAT by OpenOLAT.

the class ZipUtil method checkLockedFileBeforeUnzip.

// unzip
/**
 * Check if a file in the zip is already in the path
 * @param zipLeaf
 * @param targetDir
 * @param identity
 * @param isAdmin
 * @return the list of files which already exist
 */
public static List<String> checkLockedFileBeforeUnzip(VFSLeaf zipLeaf, VFSContainer targetDir, Identity identity, Roles isAdmin) {
    List<String> lockedFiles = new ArrayList<String>();
    InputStream in = zipLeaf.getInputStream();
    ZipInputStream oZip = new ZipInputStream(in);
    VFSLockManager vfsLockManager = CoreSpringFactory.getImpl(VFSLockManager.class);
    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
                    // directories aren't locked
                    oZip.closeEntry();
                    oEntr = oZip.getNextEntry();
                    continue;
                } else {
                    // search 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) {
                        // get subdirs
                        createIn = getAllSubdirs(targetDir, name.substring(0, dirSepIndex), identity, false);
                        if (createIn == null) {
                            // sub directories don't exist, and aren't locked
                            oZip.closeEntry();
                            oEntr = oZip.getNextEntry();
                            continue;
                        }
                        name = name.substring(dirSepIndex + 1);
                    }
                    VFSLeaf newEntry = (VFSLeaf) createIn.resolve(name);
                    if (vfsLockManager.isLockedForMe(newEntry, identity, isAdmin)) {
                        lockedFiles.add(name);
                    }
                }
            }
            oZip.closeEntry();
            oEntr = oZip.getNextEntry();
        }
    } catch (IOException e) {
        return null;
    } finally {
        FileUtils.closeSafely(oZip);
        FileUtils.closeSafely(in);
    }
    return lockedFiles;
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) VFSContainer(org.olat.core.util.vfs.VFSContainer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) VFSLockManager(org.olat.core.util.vfs.VFSLockManager)

Example 19 with VFSLeaf

use of org.olat.core.util.vfs.VFSLeaf in project OpenOLAT by OpenOLAT.

the class MailManagerImpl method deleteMail.

private void deleteMail(DBMailLight mail, Identity identity, boolean forceRemoveRecipient) {
    boolean delete = true;
    List<DBMailRecipient> updates = new ArrayList<DBMailRecipient>();
    if (mail.getFrom() != null && mail.getFrom().getRecipient() != null) {
        if (identity.equalsByPersistableKey(mail.getFrom().getRecipient())) {
            DBMailRecipient from = mail.getFrom();
            from.setDeleted(Boolean.TRUE);
            if (forceRemoveRecipient) {
                from.setRecipient(null);
            }
            updates.add(from);
        }
        if (mail.getFrom().getDeleted() != null) {
            delete &= mail.getFrom().getDeleted().booleanValue();
        }
    }
    for (DBMailRecipient recipient : mail.getRecipients()) {
        if (recipient == null)
            continue;
        if (recipient.getRecipient() != null && recipient.getRecipient().equalsByPersistableKey(identity)) {
            recipient.setDeleted(Boolean.TRUE);
            if (forceRemoveRecipient) {
                recipient.setRecipient(null);
            }
            updates.add(recipient);
        }
        if (recipient.getDeleted() != null) {
            delete &= recipient.getDeleted().booleanValue();
        }
    }
    if (delete) {
        Set<String> paths = new HashSet<String>();
        // all marked as deleted -> delete the mail
        List<DBMailAttachment> attachments = getAttachments(mail);
        for (DBMailAttachment attachment : attachments) {
            // reload from the hibernate session
            mail = attachment.getMail();
            dbInstance.deleteObject(attachment);
            if (StringHelper.containsNonWhitespace(attachment.getPath())) {
                paths.add(attachment.getPath());
            }
        }
        dbInstance.deleteObject(mail);
        // try to remove orphans file
        for (String path : paths) {
            int count = countAttachment(path);
            if (count == 0) {
                VFSItem item = mailModule.getRootForAttachments().resolve(path);
                if (item instanceof VFSLeaf) {
                    ((VFSLeaf) item).delete();
                }
            }
        }
    } else {
        for (DBMailRecipient update : updates) {
            dbInstance.updateObject(update);
        }
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) DBMailAttachment(org.olat.core.util.mail.model.DBMailAttachment) ArrayList(java.util.ArrayList) VFSItem(org.olat.core.util.vfs.VFSItem) DBMailRecipient(org.olat.core.util.mail.model.DBMailRecipient) HashSet(java.util.HashSet)

Example 20 with VFSLeaf

use of org.olat.core.util.vfs.VFSLeaf in project OpenOLAT by OpenOLAT.

the class SendDocumentsByEMailController method execute.

public Controller execute(FolderComponent folderComponent, UserRequest ureq, WindowControl wControl, Translator translator) {
    VFSContainer currentContainer = folderComponent.getCurrentContainer();
    VFSContainer rootContainer = folderComponent.getRootContainer();
    if (!VFSManager.exists(currentContainer)) {
        status = FolderCommandStatus.STATUS_FAILED;
        showError(translator.translate("FileDoesNotExist"));
        return null;
    }
    status = FolderCommandHelper.sanityCheck(wControl, folderComponent);
    if (status == FolderCommandStatus.STATUS_FAILED) {
        return null;
    }
    selection = new FileSelection(ureq, folderComponent.getCurrentContainerPath());
    status = FolderCommandHelper.sanityCheck3(wControl, folderComponent, selection);
    if (status == FolderCommandStatus.STATUS_FAILED) {
        return null;
    }
    boolean selectionWithContainer = false;
    List<String> filenames = selection.getFiles();
    List<VFSLeaf> leafs = new ArrayList<VFSLeaf>();
    for (String file : filenames) {
        VFSItem item = currentContainer.resolve(file);
        if (item instanceof VFSContainer) {
            selectionWithContainer = true;
        } else if (item instanceof VFSLeaf) {
            leafs.add((VFSLeaf) item);
        }
    }
    if (selectionWithContainer) {
        if (leafs.isEmpty()) {
            wControl.setError(getTranslator().translate("send.mail.noFileSelected"));
            return null;
        } else {
            setFormWarning(getTranslator().translate("send.mail.selectionContainsFolder"));
        }
    }
    setFiles(rootContainer, leafs);
    return this;
}
Also used : FileSelection(org.olat.core.commons.modules.bc.FileSelection) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VFSContainer(org.olat.core.util.vfs.VFSContainer) ArrayList(java.util.ArrayList) VFSItem(org.olat.core.util.vfs.VFSItem)

Aggregations

VFSLeaf (org.olat.core.util.vfs.VFSLeaf)642 VFSContainer (org.olat.core.util.vfs.VFSContainer)338 VFSItem (org.olat.core.util.vfs.VFSItem)280 File (java.io.File)114 InputStream (java.io.InputStream)96 IOException (java.io.IOException)92 Test (org.junit.Test)86 ArrayList (java.util.ArrayList)72 OutputStream (java.io.OutputStream)60 MetaInfo (org.olat.core.commons.modules.bc.meta.MetaInfo)58 MetaTagged (org.olat.core.commons.modules.bc.meta.tagged.MetaTagged)58 OlatRootFolderImpl (org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl)58 Identity (org.olat.core.id.Identity)54 LocalFileImpl (org.olat.core.util.vfs.LocalFileImpl)52 VFSMediaResource (org.olat.core.util.vfs.VFSMediaResource)46 URL (java.net.URL)40 Date (java.util.Date)40 RepositoryEntry (org.olat.repository.RepositoryEntry)36 URI (java.net.URI)34 MediaResource (org.olat.core.gui.media.MediaResource)34