Search in sources :

Example 56 with VFSItem

use of org.olat.core.util.vfs.VFSItem 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 57 with VFSItem

use of org.olat.core.util.vfs.VFSItem 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)

Example 58 with VFSItem

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

the class VersionManagerTest method testVersionManager.

@Test
public void testVersionManager() throws IOException {
    // create a file
    OlatRootFolderImpl rootTest = new OlatRootFolderImpl("/test", 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 versionedFile1 = (Versionable) file;
    InputStream in1 = new ByteArrayInputStream("Hello version 1".getBytes());
    versionedFile1.getVersions().addVersion(id2, "Version 1", in1);
    IOUtils.closeQuietly(in1);
    // save a second version
    Versionable versionedFile2 = (Versionable) file;
    InputStream in2 = new ByteArrayInputStream("Hello version 2".getBytes());
    versionedFile2.getVersions().addVersion(id2, "Version 2", in2);
    IOUtils.closeQuietly(in2);
    // save a third version
    Versionable versionedFile3 = (Versionable) file;
    InputStream in3 = new ByteArrayInputStream("Hello version 3".getBytes());
    versionedFile3.getVersions().addVersion(id2, "Version 3", in3);
    IOUtils.closeQuietly(in3);
    // make the checks
    VFSItem retrievedFile = rootTest.resolve(filename);
    assertTrue(retrievedFile instanceof Versionable);
    Versions versions = VersionsFileManager.getInstance().createVersionsFor((VFSLeaf) retrievedFile);
    List<VFSRevision> revisions = versions.getRevisions();
    assertNotNull(revisions);
    assertEquals(3, revisions.size());
    VFSRevision revision0 = revisions.get(0);
    // we don't set an author for the original file
    assertEquals("-", revision0.getAuthor());
    VFSRevision revision1 = revisions.get(1);
    assertEquals(id2.getName(), revision1.getAuthor());
    VFSRevision revision2 = revisions.get(2);
    assertEquals(id2.getName(), revision2.getAuthor());
    // check the comments
    assertNull(revision0.getComment());
    assertEquals("Version 1", revision1.getComment());
    assertEquals("Version 2", revision2.getComment());
    assertEquals("Version 3", versions.getComment());
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) OlatRootFolderImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) VFSItem(org.olat.core.util.vfs.VFSItem) Test(org.junit.Test)

Example 59 with VFSItem

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

the class EditSolutionController method formOK.

@Override
protected void formOK(UserRequest ureq) {
    solution.setTitle(titleEl.getValue());
    if (fileEl.getUploadFile() != null) {
        if (replaceFile && StringHelper.containsNonWhitespace(solution.getFilename())) {
            File currentFile = new File(solutionDir, solution.getFilename());
            if (currentFile.exists()) {
                currentFile.delete();
            }
        }
        String filename = fileEl.getUploadFileName();
        if (!replaceFile) {
            File currentFile = new File(solutionDir, filename);
            if (currentFile.exists()) {
                filename = VFSManager.rename(solutionContainer, filename);
            }
        }
        solution.setFilename(filename);
        try {
            Path upload = fileEl.getUploadFile().toPath();
            File newFile = new File(solutionDir, filename);
            Files.move(upload, newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
            VFSItem uploadedItem = solutionContainer.resolve(filename);
            if (uploadedItem instanceof MetaTagged) {
                MetaInfo metaInfo = ((MetaTagged) uploadedItem).getMetaInfo();
                metaInfo.setAuthor(ureq.getIdentity());
                metaInfo.write();
            }
        } catch (Exception ex) {
            logError("", ex);
        }
    }
    fireEvent(ureq, Event.DONE_EVENT);
}
Also used : Path(java.nio.file.Path) 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) File(java.io.File)

Example 60 with VFSItem

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

the class CPManagerImpl method writeToZip.

/**
 * @see org.olat.ims.cp.CPManager#writeToZip(org.olat.ims.cp.ContentPackage)
 */
public VFSLeaf writeToZip(ContentPackage cp) {
    OLATResourceable ores = cp.getResourcable();
    VFSContainer cpRoot = cp.getRootDir();
    VFSContainer oresRoot = FileResourceManager.getInstance().getFileResourceRootImpl(ores);
    RepositoryEntry repoEntry = RepositoryManager.getInstance().lookupRepositoryEntry(ores, false);
    String zipFileName = "imscp.zip";
    if (repoEntry != null) {
        String zipName = repoEntry.getResourcename();
        if (zipName != null && zipName.endsWith(".zip")) {
            zipFileName = zipName;
        }
    }
    // delete old archive and create new one
    VFSItem oldArchive = oresRoot.resolve(zipFileName);
    if (oldArchive != null) {
        // don't versioned the zip
        oldArchive.deleteSilently();
    }
    ZipUtil.zip(cpRoot.getItems(), oresRoot.createChildLeaf(zipFileName), true);
    VFSLeaf zip = (VFSLeaf) oresRoot.resolve(zipFileName);
    return zip;
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) OLATResourceable(org.olat.core.id.OLATResourceable) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) RepositoryEntry(org.olat.repository.RepositoryEntry)

Aggregations

VFSItem (org.olat.core.util.vfs.VFSItem)546 VFSContainer (org.olat.core.util.vfs.VFSContainer)356 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)272 File (java.io.File)78 Test (org.junit.Test)68 OlatRootFolderImpl (org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl)68 ArrayList (java.util.ArrayList)64 InputStream (java.io.InputStream)52 MetaInfo (org.olat.core.commons.modules.bc.meta.MetaInfo)52 Identity (org.olat.core.id.Identity)50 MetaTagged (org.olat.core.commons.modules.bc.meta.tagged.MetaTagged)48 IOException (java.io.IOException)42 Date (java.util.Date)40 URI (java.net.URI)38 LocalFolderImpl (org.olat.core.util.vfs.LocalFolderImpl)38 SystemItemFilter (org.olat.core.util.vfs.filters.SystemItemFilter)34 OutputStream (java.io.OutputStream)30 VFSMediaResource (org.olat.core.util.vfs.VFSMediaResource)28 HttpResponse (org.apache.http.HttpResponse)22 URL (java.net.URL)20