Search in sources :

Example 16 with SystemItemFilter

use of org.olat.core.util.vfs.filters.SystemItemFilter in project openolat by klemens.

the class AssignmentEditController method updateAssignmentDocumentForm.

protected void updateAssignmentDocumentForm(UserRequest ureq) {
    if (mapperUri == null) {
        mapperUri = registerCacheableMapper(ureq, "assigment-" + CodeHelper.getRAMUniqueID(), new DocumentMapper());
        filesLayout.contextPut("mapperUri", mapperUri);
    }
    List<VFSItem> files = new ArrayList<>();
    if (assignment != null && StringHelper.containsNonWhitespace(assignment.getStorage())) {
        documentContainer = fileStorage.getAssignmentContainer(assignment);
        files.addAll(documentContainer.getItems(new SystemItemFilter()));
    }
    // add files from TempFolder
    if (tempUploadFolder != null) {
        files.addAll(tempUploadFolder.getItems(new SystemItemFilter()));
    }
    Collections.sort(files, new Comparator<VFSItem>() {

        final Collator c = Collator.getInstance(getLocale());

        @Override
        public int compare(final VFSItem o1, final VFSItem o2) {
            return c.compare((o1).getName(), (o2).getName());
        }
    });
    filesLayout.contextPut("files", files);
    // add delete links for each attachment if user is allowed to see them
    int count = 0;
    for (VFSItem file : files) {
        FormLink deleteLink = uifactory.addFormLink("delete_" + (++count), filesLayout, Link.BUTTON_XSMALL);
        deleteLink.setUserObject(file);
        deleteLink.setI18nKey("delete");
    }
    boolean hasFile = files.size() > 0;
    filesLayout.setVisible(files.size() > 0);
    filesLayout.showLabel(hasFile);
    documentUploadEl.showLabel(!hasFile);
}
Also used : ArrayList(java.util.ArrayList) VFSItem(org.olat.core.util.vfs.VFSItem) SystemItemFilter(org.olat.core.util.vfs.filters.SystemItemFilter) FormLink(org.olat.core.gui.components.form.flexible.elements.FormLink) Collator(java.text.Collator)

Example 17 with SystemItemFilter

use of org.olat.core.util.vfs.filters.SystemItemFilter in project openolat by klemens.

the class SharedFolderWebService method getFiles.

public Response getFiles(Long repoEntryKey, List<PathSegment> path, UriInfo uriInfo, HttpServletRequest httpRequest, Request request) {
    RepositoryEntry re = repositoryManager.lookupRepositoryEntry(repoEntryKey);
    if (re == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    VFSContainer container = SharedFolderManager.getInstance().getNamedSharedFolder(re, true);
    if (container == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    if (!repositoryManager.isAllowedToLaunch(RestSecurityHelper.getIdentity(httpRequest), RestSecurityHelper.getRoles(httpRequest), re)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    VFSLeaf leaf = null;
    for (PathSegment seg : path) {
        VFSItem item = container.resolve(seg.getPath());
        if (item instanceof VFSLeaf) {
            leaf = (VFSLeaf) item;
            break;
        } else if (item instanceof VFSContainer) {
            container = (VFSContainer) item;
        }
    }
    if (leaf != null) {
        Date lastModified = new Date(leaf.getLastModified());
        Response.ResponseBuilder response = request.evaluatePreconditions(lastModified);
        if (response == null) {
            String mimeType = WebappHelper.getMimeType(leaf.getName());
            if (mimeType == null)
                mimeType = MediaType.APPLICATION_OCTET_STREAM;
            response = Response.ok(leaf.getInputStream(), mimeType).lastModified(lastModified).cacheControl(cc);
        }
        return response.build();
    }
    List<VFSItem> items = container.getItems(new SystemItemFilter());
    int count = 0;
    LinkVO[] links = new LinkVO[items.size()];
    for (VFSItem item : items) {
        UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();
        UriBuilder repoUri = baseUriBuilder.path(SharedFolderWebService.class).path(repoEntryKey.toString()).path("files");
        for (PathSegment pathSegment : path) {
            repoUri.path(pathSegment.getPath());
        }
        String uri = repoUri.path(item.getName()).build().toString();
        links[count++] = new LinkVO("self", uri, item.getName());
    }
    return Response.ok(links).build();
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) RepositoryEntry(org.olat.repository.RepositoryEntry) PathSegment(javax.ws.rs.core.PathSegment) SystemItemFilter(org.olat.core.util.vfs.filters.SystemItemFilter) Date(java.util.Date) Response(javax.ws.rs.core.Response) LinkVO(org.olat.restapi.support.vo.LinkVO) UriBuilder(javax.ws.rs.core.UriBuilder)

Example 18 with SystemItemFilter

use of org.olat.core.util.vfs.filters.SystemItemFilter in project openolat by klemens.

the class CourseResourceFolderWebService method getFiles.

public Response getFiles(Long courseId, List<PathSegment> path, FolderType type, UriInfo uriInfo, HttpServletRequest httpRequest, Request request) {
    if (!isAuthor(httpRequest)) {
        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 = null;
    RepositoryEntry re = null;
    switch(type) {
        case COURSE_FOLDER:
            container = course.getCourseFolderContainer();
            break;
        case SHARED_FOLDER:
            {
                container = null;
                String sfSoftkey = course.getCourseConfig().getSharedFolderSoftkey();
                OLATResource sharedResource = CoreSpringFactory.getImpl(RepositoryService.class).loadRepositoryEntryResourceBySoftKey(sfSoftkey);
                if (sharedResource != null) {
                    re = CoreSpringFactory.getImpl(RepositoryService.class).loadByResourceKey(sharedResource.getKey());
                    container = SharedFolderManager.getInstance().getNamedSharedFolder(re, true);
                    CourseConfig courseConfig = course.getCourseConfig();
                    if (courseConfig.isSharedFolderReadOnlyMount()) {
                        container.setLocalSecurityCallback(new ReadOnlyCallback());
                    }
                }
                break;
            }
    }
    if (container == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    VFSLeaf leaf = null;
    for (PathSegment seg : path) {
        VFSItem item = container.resolve(seg.getPath());
        if (item instanceof VFSLeaf) {
            leaf = (VFSLeaf) item;
            break;
        } else if (item instanceof VFSContainer) {
            container = (VFSContainer) item;
        }
    }
    if (leaf != null) {
        Date lastModified = new Date(leaf.getLastModified());
        Response.ResponseBuilder response = request.evaluatePreconditions(lastModified);
        if (response == null) {
            String mimeType = WebappHelper.getMimeType(leaf.getName());
            if (mimeType == null)
                mimeType = MediaType.APPLICATION_OCTET_STREAM;
            response = Response.ok(leaf.getInputStream(), mimeType).lastModified(lastModified).cacheControl(cc);
        }
        return response.build();
    }
    List<VFSItem> items = container.getItems(new SystemItemFilter());
    int count = 0;
    LinkVO[] links = new LinkVO[items.size()];
    for (VFSItem item : items) {
        UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();
        UriBuilder repoUri = baseUriBuilder.path(CourseResourceFolderWebService.class).path("files");
        if (type.equals(FolderType.SHARED_FOLDER) && re != null) {
            repoUri = baseUriBuilder.replacePath("restapi").path(SharedFolderWebService.class).path(re.getKey().toString()).path("files");
        }
        for (PathSegment pathSegment : path) {
            repoUri.path(pathSegment.getPath());
        }
        String uri = repoUri.path(item.getName()).build(courseId).toString();
        links[count++] = new LinkVO("self", uri, item.getName());
    }
    return Response.ok(links).build();
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) ReadOnlyCallback(org.olat.core.util.vfs.callbacks.ReadOnlyCallback) SharedFolderWebService(org.olat.restapi.repository.SharedFolderWebService) VFSContainer(org.olat.core.util.vfs.VFSContainer) OLATResource(org.olat.resource.OLATResource) VFSItem(org.olat.core.util.vfs.VFSItem) ICourse(org.olat.course.ICourse) RepositoryEntry(org.olat.repository.RepositoryEntry) PathSegment(javax.ws.rs.core.PathSegment) SystemItemFilter(org.olat.core.util.vfs.filters.SystemItemFilter) Date(java.util.Date) CourseConfig(org.olat.course.config.CourseConfig) Response(javax.ws.rs.core.Response) LinkVO(org.olat.restapi.support.vo.LinkVO) UriBuilder(javax.ws.rs.core.UriBuilder) RepositoryService(org.olat.repository.RepositoryService)

Example 19 with SystemItemFilter

use of org.olat.core.util.vfs.filters.SystemItemFilter in project openolat by klemens.

the class VersionManagerTest method testVersionChecksum.

@Test
public void testVersionChecksum() throws IOException {
    OlatRootFolderImpl rootTest = new OlatRootFolderImpl("/ver-" + UUID.randomUUID(), null);
    String filename = getRandomName();
    VFSLeaf file = rootTest.createChildLeaf(filename);
    OutputStream out = file.getOutputStream(false);
    InputStream in = VersionManagerTest.class.getResourceAsStream("test.txt");
    int byteCopied = IOUtils.copy(in, out);
    IOUtils.closeQuietly(in);
    assertFalse(byteCopied == 0);
    assertTrue(file instanceof Versionable);
    // save a first version
    Versionable versionedFile = (Versionable) file;
    InputStream in1 = VersionManagerTest.class.getResourceAsStream("test.txt");
    versionedFile.getVersions().addVersion(id2, "Version 1", in1);
    IOUtils.closeQuietly(in1);
    // save a second version
    InputStream in2 = VersionManagerTest.class.getResourceAsStream("test.txt");
    versionedFile.getVersions().addVersion(id2, "Version 2", in2);
    IOUtils.closeQuietly(in2);
    // save a third version
    InputStream in3 = VersionManagerTest.class.getResourceAsStream("test.txt");
    versionedFile.getVersions().addVersion(id2, "Version 3", in2);
    IOUtils.closeQuietly(in3);
    // check if there is only one backup file
    VFSContainer versionContainer = versionManager.getCanonicalVersionFolder(file.getParentContainer(), false);
    Assert.assertNotNull(versionContainer);
    List<VFSItem> items = versionContainer.getItems(new SystemItemFilter());
    Assert.assertNotNull(items);
    Assert.assertEquals(2, items.size());
    // check number of versions
    VFSItem reloadFile = rootTest.resolve(filename);
    assertTrue(reloadFile instanceof Versionable);
    Versionable reloadedVersionedFile = (Versionable) reloadFile;
    List<VFSRevision> revisions = reloadedVersionedFile.getVersions().getRevisions();
    Assert.assertNotNull(revisions);
    Assert.assertEquals(3, revisions.size());
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) SystemItemFilter(org.olat.core.util.vfs.filters.SystemItemFilter) OlatRootFolderImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl) Test(org.junit.Test)

Example 20 with SystemItemFilter

use of org.olat.core.util.vfs.filters.SystemItemFilter in project openolat by klemens.

the class FolderManager method getFileInfosRecursively.

private static void getFileInfosRecursively(OlatRelPathImpl relPath, List<FileInfo> fileInfos, long newerThan, int basePathlen) {
    if (relPath instanceof VFSLeaf) {
        // is a file
        long lastModified = ((VFSLeaf) relPath).getLastModified();
        if (lastModified > newerThan) {
            MetaInfo meta = CoreSpringFactory.getImpl(MetaInfoFactory.class).createMetaInfoFor(relPath);
            String bcrootPath = relPath.getRelPath();
            String bcRelPath = bcrootPath.substring(basePathlen);
            fileInfos.add(new FileInfo(bcRelPath, meta, new Date(lastModified)));
        }
    } else {
        // is a folder
        OlatRootFolderImpl container = (OlatRootFolderImpl) relPath;
        for (VFSItem item : container.getItems(new SystemItemFilter())) {
            getFileInfosRecursively((OlatRelPathImpl) item, fileInfos, newerThan, basePathlen);
        }
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) OlatRootFolderImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) MetaInfoFactory(org.olat.core.commons.modules.bc.meta.MetaInfoFactory) VFSItem(org.olat.core.util.vfs.VFSItem) SystemItemFilter(org.olat.core.util.vfs.filters.SystemItemFilter) Date(java.util.Date)

Aggregations

SystemItemFilter (org.olat.core.util.vfs.filters.SystemItemFilter)36 VFSItem (org.olat.core.util.vfs.VFSItem)34 VFSContainer (org.olat.core.util.vfs.VFSContainer)30 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)24 Date (java.util.Date)10 OutputStream (java.io.OutputStream)8 PathSegment (javax.ws.rs.core.PathSegment)8 UriBuilder (javax.ws.rs.core.UriBuilder)8 OlatRootFolderImpl (org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 Response (javax.ws.rs.core.Response)6 Test (org.junit.Test)6 RepositoryEntry (org.olat.repository.RepositoryEntry)4 FileVO (org.olat.restapi.support.vo.FileVO)4 LinkVO (org.olat.restapi.support.vo.LinkVO)4 File (java.io.File)2 Collator (java.text.Collator)2 HashSet (java.util.HashSet)2