Search in sources :

Example 1 with SystemItemFilter

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

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 2 with SystemItemFilter

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

the class AssignmentEditController method persistUploadedFiles.

private void persistUploadedFiles() {
    if (tempUploadFolder == null)
        return;
    VFSContainer container = portfolioFileStorage.getAssignmentContainer(assignment);
    if (container != null) {
        List<VFSItem> tmpFList = tempUploadFolder.getItems(new SystemItemFilter());
        for (VFSItem file : tmpFList) {
            try {
                VFSLeaf leaf = (VFSLeaf) file;
                VFSLeaf storedFile = container.createChildLeaf(leaf.getName());
                FileUtils.bcopy(leaf.getInputStream(), storedFile.getOutputStream(false), "");
            } catch (Exception e) {
                logError("", e);
            }
        }
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) SystemItemFilter(org.olat.core.util.vfs.filters.SystemItemFilter)

Example 3 with SystemItemFilter

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

the class VersionManagerTest method testDeleteRevisions_withSameFile.

/**
 * The test create an original file and 3 revisions with exactly
 * the same content. We delete the original and the first version.
 * We check that version 2 and 3 survives and that the file exists.
 *
 * @throws IOException
 */
@Test
public void testDeleteRevisions_withSameFile() 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);
    // delete revisions
    versionManager.deleteRevisions(versionedFile, versionedFile.getVersions().getRevisions().subList(0, 2));
    // 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(1, revisions.size());
    // check surviving versions
    Assert.assertEquals("Version 2", revisions.get(0).getComment());
    Assert.assertEquals("Version 3", reloadedVersionedFile.getVersions().getComment());
    // check that the last backup file exists
    RevisionFileImpl revision2 = (RevisionFileImpl) revisions.get(0);
    VFSLeaf revision2File = revision2.getFile();
    Assert.assertNotNull(revision2File);
    Assert.assertTrue(revision2File.exists());
    // 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());
}
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 4 with SystemItemFilter

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

the class VersionManagerTest method testDeleteRevisions_withMissingRevisionFile.

/**
 * The test create an original file and 5 versions. It manually
 * delete the physical back up file. We delete the versions
 * of the orginal, 1 and 2. At the end, there is only version 4
 * and 5.
 *
 * @throws IOException
 */
@Test
public void testDeleteRevisions_withMissingRevisionFile() 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 = new ByteArrayInputStream("Hello original".getBytes());
    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 = new ByteArrayInputStream("Hello version 1".getBytes());
    versionedFile.getVersions().addVersion(id2, "Version 1", in1);
    IOUtils.closeQuietly(in1);
    // save a second version
    InputStream in2 = new ByteArrayInputStream("Hello version 2".getBytes());
    versionedFile.getVersions().addVersion(id2, "Version 2", in2);
    IOUtils.closeQuietly(in2);
    // save a third version
    InputStream in3 = new ByteArrayInputStream("Hello version 3".getBytes());
    versionedFile.getVersions().addVersion(id2, "Version 3", in3);
    IOUtils.closeQuietly(in3);
    // save a fourth version
    InputStream in4 = new ByteArrayInputStream("Hello version 4".getBytes());
    versionedFile.getVersions().addVersion(id2, "Version 4", in4);
    IOUtils.closeQuietly(in4);
    // save a fourth version
    InputStream in5 = new ByteArrayInputStream("Hello version 5".getBytes());
    versionedFile.getVersions().addVersion(id2, "Version 5", in5);
    IOUtils.closeQuietly(in5);
    // delete a specific
    VFSRevision rev3 = versionedFile.getVersions().getRevisions().get(3);
    RevisionFileImpl toDeleteVersionImpl = (RevisionFileImpl) rev3;
    VFSContainer versionContainerAlt = versionManager.getCanonicalVersionFolder(rootTest, false);
    VFSItem itemToDelete = versionContainerAlt.resolve(toDeleteVersionImpl.getFilename());
    itemToDelete.deleteSilently();
    // delete revisions
    List<VFSRevision> toDelete = new ArrayList<>(versionedFile.getVersions().getRevisions().subList(0, 3));
    versionManager.deleteRevisions(versionedFile, toDelete);
    // 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(1, revisions.size());
    VFSRevision revision = revisions.get(0);
    Assert.assertEquals("Version 4", revision.getComment());
    Assert.assertEquals("Version 5", reloadedVersionedFile.getVersions().getComment());
    // 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());
}
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) ArrayList(java.util.ArrayList) VFSItem(org.olat.core.util.vfs.VFSItem) SystemItemFilter(org.olat.core.util.vfs.filters.SystemItemFilter) OlatRootFolderImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 5 with SystemItemFilter

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

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)

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