Search in sources :

Example 56 with MetaInfo

use of org.olat.core.commons.modules.bc.meta.MetaInfo in project OpenOLAT by OpenOLAT.

the class CatalogManager method setImage.

public boolean setImage(VFSLeaf newImageFile, CatalogEntryRef re) {
    VFSLeaf currentImage = getImage(re);
    if (currentImage != null) {
        if (currentImage instanceof MetaTagged) {
            MetaInfo info = ((MetaTagged) currentImage).getMetaInfo();
            if (info != null) {
                info.clearThumbnails();
            }
        }
        currentImage.delete();
    }
    String extension = FileUtils.getFileSuffix(newImageFile.getName());
    if (StringHelper.containsNonWhitespace(extension)) {
        extension = extension.toLowerCase();
    }
    boolean ok = false;
    VFSContainer catalogResourceHome = getCatalogResourcesHome();
    try {
        if ("jpeg".equals(extension) || "jpg".equals(extension)) {
            VFSLeaf repoImage = catalogResourceHome.createChildLeaf(re.getKey() + ".jpg");
            ok = VFSManager.copyContent(newImageFile, repoImage);
        } else if ("png".equals(extension)) {
            VFSLeaf repoImage = catalogResourceHome.createChildLeaf(re.getKey() + ".png");
            ok = VFSManager.copyContent(newImageFile, repoImage);
        } else {
            // scale to default and png
            VFSLeaf repoImage = catalogResourceHome.createChildLeaf(re.getKey() + ".png");
            Size size = imageHelper.scaleImage(newImageFile, repoImage, 570, 570, true);
            ok = size != null;
        }
    } catch (Exception e) {
        log.error("", e);
    }
    return ok;
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) Size(org.olat.core.commons.services.image.Size) VFSContainer(org.olat.core.util.vfs.VFSContainer) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo)

Example 57 with MetaInfo

use of org.olat.core.commons.modules.bc.meta.MetaInfo in project OpenOLAT by OpenOLAT.

the class CourseResourceFolderWebService method attachFileToCourseFolder.

private Response attachFileToCourseFolder(Long courseId, List<PathSegment> path, String filename, InputStream file, HttpServletRequest request) {
    if (!isAuthor(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    ICourse course = CoursesWebService.loadCourse(courseId);
    if (course == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    VFSContainer container = course.getCourseFolderContainer();
    for (PathSegment segment : path) {
        VFSItem item = container.resolve(segment.getPath());
        if (item instanceof VFSContainer) {
            container = (VFSContainer) item;
        } else if (item == null) {
            // create the folder
            container = container.createChildContainer(segment.getPath());
        }
    }
    VFSItem newFile;
    UserRequest ureq = RestSecurityHelper.getUserRequest(request);
    if (container.resolve(filename) != null) {
        VFSItem existingVFSItem = container.resolve(filename);
        if (existingVFSItem instanceof VFSContainer) {
            // already exists
            return Response.ok().build();
        }
        // check if it's locked
        boolean locked = CoreSpringFactory.getImpl(VFSLockManager.class).isLockedForMe(existingVFSItem, ureq.getIdentity(), ureq.getUserSession().getRoles());
        if (locked) {
            return Response.serverError().status(Status.UNAUTHORIZED).build();
        }
        if (existingVFSItem instanceof Versionable && ((Versionable) existingVFSItem).getVersions().isVersioned()) {
            Versionable existingVersionableItem = (Versionable) existingVFSItem;
            boolean ok = existingVersionableItem.getVersions().addVersion(ureq.getIdentity(), "REST upload", file);
            if (ok) {
                log.audit("");
            }
            newFile = (VFSLeaf) existingVersionableItem;
        } else {
            existingVFSItem.delete();
            newFile = container.createChildLeaf(filename);
            OutputStream out = ((VFSLeaf) newFile).getOutputStream(false);
            FileUtils.copy(file, out);
            FileUtils.closeSafely(out);
            FileUtils.closeSafely(file);
        }
    } else if (file != null) {
        newFile = container.createChildLeaf(filename);
        OutputStream out = ((VFSLeaf) newFile).getOutputStream(false);
        FileUtils.copy(file, out);
        FileUtils.closeSafely(out);
        FileUtils.closeSafely(file);
    } else {
        newFile = container.createChildContainer(filename);
    }
    if (newFile instanceof MetaTagged && ((MetaTagged) newFile).getMetaInfo() != null) {
        MetaInfo infos = ((MetaTagged) newFile).getMetaInfo();
        infos.setAuthor(ureq.getIdentity());
        infos.write();
    }
    return Response.ok().build();
}
Also used : Versionable(org.olat.core.util.vfs.version.Versionable) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VFSContainer(org.olat.core.util.vfs.VFSContainer) OutputStream(java.io.OutputStream) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) VFSItem(org.olat.core.util.vfs.VFSItem) ICourse(org.olat.course.ICourse) PathSegment(javax.ws.rs.core.PathSegment) UserRequest(org.olat.core.gui.UserRequest) VFSLockManager(org.olat.core.util.vfs.VFSLockManager)

Example 58 with MetaInfo

use of org.olat.core.commons.modules.bc.meta.MetaInfo in project OpenOLAT by OpenOLAT.

the class FileDocument method init.

protected void init(SearchResourceContext leafResourceContext, VFSLeaf leaf) throws IOException, DocumentException, DocumentAccessException {
    // Load metadata for this file
    MetaInfo meta = null;
    if (leaf instanceof MetaTagged) {
        meta = ((MetaTagged) leaf).getMetaInfo();
    }
    // Set all know attributes
    setResourceUrl(leafResourceContext.getResourceUrl());
    setLastChange(new Date(leaf.getLastModified()));
    // Check if there are documents attributes set in resource context
    if (StringHelper.containsNonWhitespace(leafResourceContext.getDocumentType())) {
        // document-type in context is set => get from there
        setDocumentType(leafResourceContext.getDocumentType());
    } else {
        setDocumentType(TYPE);
    }
    FileContent content = readContent(leaf);
    String metaTitle;
    if (meta != null && StringHelper.containsNonWhitespace(meta.getTitle())) {
        metaTitle = meta.getTitle();
    } else if (content != null && StringHelper.containsNonWhitespace(content.getTitle())) {
        metaTitle = content.getTitle();
    } else {
        String beautfiedName = leaf.getName();
        int dotpos = beautfiedName.lastIndexOf('.');
        if (dotpos > 0) {
            beautfiedName = beautfiedName.substring(0, dotpos);
        }
        metaTitle = beautfiedName.replace('_', ' ');
    }
    StringBuilder title = new StringBuilder();
    if (StringHelper.containsNonWhitespace(leafResourceContext.getTitle())) {
        // Title in context is set => get from there and add filename
        title.append(leafResourceContext.getTitle()).append(", ");
    }
    if (metaTitle != null) {
        title.append(metaTitle).append(" ( ");
    }
    title.append(leaf.getName());
    if (metaTitle != null) {
        title.append(" )");
    }
    setTitle(title.toString());
    String metaDesc = (meta == null ? null : meta.getComment());
    if (StringHelper.containsNonWhitespace(leafResourceContext.getDescription())) {
        // Title in context is set => get from there
        setDescription(leafResourceContext.getDescription() + (metaDesc == null ? "" : " " + metaDesc));
    } else if (metaDesc != null) {
        setDescription(metaDesc);
    }
    setParentContextType(leafResourceContext.getParentContextType());
    setParentContextName(leafResourceContext.getParentContextName());
    setContent(content.getContent());
    // Add other metadata from meta info
    if (meta != null) {
        addMetadata(SimpleDublinCoreMetadataFieldsProvider.DC_DESCRIPTION, meta.getComment());
        addMetadata(SimpleDublinCoreMetadataFieldsProvider.DC_LANGUAGE, meta.getLanguage());
        // Date is 2009 200902 or 20090228
        String[] pubDateArray = meta.getPublicationDate();
        if (pubDateArray != null) {
            StringBuilder pubDate = new StringBuilder();
            for (String d : pubDateArray) {
                if (d != null) {
                    pubDate.append(d);
                }
            }
            addMetadata(SimpleDublinCoreMetadataFieldsProvider.DC_DATE, pubDate.toString());
            Date publicationDate = getDateFromPublicationDateArray(pubDateArray);
            if (publicationDate != null) {
                setPublicationDate(publicationDate);
            }
        }
        addMetadata(SimpleDublinCoreMetadataFieldsProvider.DC_PUBLISHER, meta.getPublisher());
        addMetadata(SimpleDublinCoreMetadataFieldsProvider.DC_SOURCE, meta.getSource());
        addMetadata(SimpleDublinCoreMetadataFieldsProvider.DC_SOURCE, meta.getUrl());
        // use creator and author as olat author
        setAuthor((meta.getCreator() == null ? meta.getAuthor() : meta.getAuthor() + " " + meta.getCreator()));
        addMetadata(SimpleDublinCoreMetadataFieldsProvider.DC_CREATOR, meta.getCreator());
    }
    // Add file type
    String mimeType = WebappHelper.getMimeType(leaf.getName());
    addMetadata(SimpleDublinCoreMetadataFieldsProvider.DC_FORMAT, mimeType);
    // License
    String licenseTypeKey = "";
    if (meta != null && StringHelper.containsNonWhitespace(meta.getLicenseTypeKey())) {
        licenseTypeKey = meta.getLicenseTypeKey();
    }
    setLicenseTypeKey(licenseTypeKey);
}
Also used : MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) Date(java.util.Date)

Example 59 with MetaInfo

use of org.olat.core.commons.modules.bc.meta.MetaInfo in project OpenOLAT by OpenOLAT.

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;
}
Also used : Versionable(org.olat.core.util.vfs.version.Versionable) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) VFSContainer(org.olat.core.util.vfs.VFSContainer) ZipOutputStream(java.util.zip.ZipOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) IOException(java.io.IOException)

Example 60 with MetaInfo

use of org.olat.core.commons.modules.bc.meta.MetaInfo in project OpenOLAT by OpenOLAT.

the class ZipUtil method getAllSubdirs.

/**
 * Get the whole subpath.
 * @param create the missing directories
 * @param base
 * @param subDirPath
 * @return Returns the last container of this subpath.
 */
public static VFSContainer getAllSubdirs(VFSContainer base, String subDirPath, Identity identity, boolean create) {
    StringTokenizer st;
    if (subDirPath.indexOf("/") != -1) {
        st = new StringTokenizer(subDirPath, "/", false);
    } else {
        // try it windows style, backslash is also valid format
        st = new StringTokenizer(subDirPath, "\\", false);
    }
    VFSContainer currentPath = base;
    while (st.hasMoreTokens()) {
        String nextSubpath = st.nextToken();
        VFSItem vfsSubpath = currentPath.resolve(nextSubpath);
        if (vfsSubpath == null && !create) {
            return null;
        }
        if (vfsSubpath == null || (vfsSubpath instanceof VFSLeaf)) {
            vfsSubpath = currentPath.createChildContainer(nextSubpath);
            if (vfsSubpath == null)
                return null;
            if (identity != null && vfsSubpath instanceof MetaTagged) {
                MetaInfo info = ((MetaTagged) vfsSubpath).getMetaInfo();
                if (info != null) {
                    info.setAuthor(identity);
                    info.write();
                }
            }
        }
        currentPath = (VFSContainer) vfsSubpath;
    }
    return currentPath;
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) StringTokenizer(java.util.StringTokenizer) VFSContainer(org.olat.core.util.vfs.VFSContainer) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) VFSItem(org.olat.core.util.vfs.VFSItem)

Aggregations

MetaInfo (org.olat.core.commons.modules.bc.meta.MetaInfo)108 MetaTagged (org.olat.core.commons.modules.bc.meta.tagged.MetaTagged)86 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)58 VFSItem (org.olat.core.util.vfs.VFSItem)52 VFSContainer (org.olat.core.util.vfs.VFSContainer)40 Date (java.util.Date)18 OutputStream (java.io.OutputStream)14 File (java.io.File)12 IOException (java.io.IOException)12 Versionable (org.olat.core.util.vfs.version.Versionable)12 InputStream (java.io.InputStream)10 ArrayList (java.util.ArrayList)10 FolderEvent (org.olat.core.commons.modules.bc.FolderEvent)10 MediaResource (org.olat.core.gui.media.MediaResource)10 Identity (org.olat.core.id.Identity)10 VFSMediaResource (org.olat.core.util.vfs.VFSMediaResource)10 FileInfo (org.olat.core.commons.modules.bc.FileInfo)8 OlatRootFolderImpl (org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl)8 SubscriptionListItem (org.olat.core.commons.services.notifications.model.SubscriptionListItem)8 VFSSecurityCallback (org.olat.core.util.vfs.callbacks.VFSSecurityCallback)8