diff --git a/src/main/java/org/olat/core/commons/modules/bc/FileUploadController.java b/src/main/java/org/olat/core/commons/modules/bc/FileUploadController.java index 8eb105e69252cbe5b180ed99b21952db6f02252b..cdab92c7cf5e11cd5d8eaddf5622b3b0acf7dae2 100644 --- a/src/main/java/org/olat/core/commons/modules/bc/FileUploadController.java +++ b/src/main/java/org/olat/core/commons/modules/bc/FileUploadController.java @@ -575,7 +575,7 @@ public class FileUploadController extends FormBasicController { // check for available space if (remainingQuotKB != -1 && (fileEl.getUploadFile().length() / 1024 > remainingQuotKB)) { fileEl.setErrorKey("QuotaExceeded", null); - fileEl.getUploadFile().delete(); + FileUtils.deleteFile(fileEl.getUploadFile()); return; } diff --git a/src/main/java/org/olat/core/commons/services/vfs/VFSMetadata.java b/src/main/java/org/olat/core/commons/services/vfs/VFSMetadata.java index 9c7b53d3efc4b0c6d86eacbe373f3d8e4dd4ef79..4ade2d8bb6ff5a4c9171d9ed5935b6cafba938d3 100644 --- a/src/main/java/org/olat/core/commons/services/vfs/VFSMetadata.java +++ b/src/main/java/org/olat/core/commons/services/vfs/VFSMetadata.java @@ -176,6 +176,6 @@ public interface VFSMetadata extends VFSMetadataRef, ModifiedInfo, CreateInfo { public String getMigrated(); - public void copyValues(VFSMetadata metadata); + public void copyValues(VFSMetadata metadata, boolean override); } diff --git a/src/main/java/org/olat/core/commons/services/vfs/manager/VFSMetadataDAO.java b/src/main/java/org/olat/core/commons/services/vfs/manager/VFSMetadataDAO.java index d78899b0d56ce8c13af705c75c42c49048d029f2..983cde4f85525fcf499920230846680595c7558c 100644 --- a/src/main/java/org/olat/core/commons/services/vfs/manager/VFSMetadataDAO.java +++ b/src/main/java/org/olat/core/commons/services/vfs/manager/VFSMetadataDAO.java @@ -422,7 +422,7 @@ public class VFSMetadataDAO { query.setParameter("revisionCount", revisionCount); } if(size > 0) { - query.setParameter("size", new Long(size)); + query.setParameter("size", Long.valueOf(size)); } return query.setFirstResult(0) diff --git a/src/main/java/org/olat/core/commons/services/vfs/manager/VFSRepositoryServiceImpl.java b/src/main/java/org/olat/core/commons/services/vfs/manager/VFSRepositoryServiceImpl.java index d6ff01b2a498c6ed37808b053dda13c71819b3c8..6b9a7585ffa4c75526e400dbe82eb0a0b23d9a9a 100644 --- a/src/main/java/org/olat/core/commons/services/vfs/manager/VFSRepositoryServiceImpl.java +++ b/src/main/java/org/olat/core/commons/services/vfs/manager/VFSRepositoryServiceImpl.java @@ -552,7 +552,7 @@ public class VFSRepositoryServiceImpl implements VFSRepositoryService, GenericEv targetMetadata = metadataDao.createMetadata(UUID.randomUUID().toString(), relativePath, targetFile.getName(), new Date(), targetFile.length(), false, targetFile.toURI().toString(), "file", parentMetadata); } - targetMetadata.copyValues(sourceMetadata); + targetMetadata.copyValues(sourceMetadata, true); if(source.canVersion() == VFSConstants.YES || target.canVersion() == VFSConstants.YES) { targetMetadata.setRevisionComment(sourceMetadata.getRevisionComment()); targetMetadata.setRevisionNr(sourceMetadata.getRevisionNr()); @@ -582,9 +582,16 @@ public class VFSRepositoryServiceImpl implements VFSRepositoryService, GenericEv @Override public VFSMetadata rename(VFSItem item, String newName) { VFSMetadata metadata = getMetadataFor(item); - - ((VFSMetadataImpl)metadata).setFilename(newName); + + // Is there already a metadata from an other file with the same name + VFSMetadata currentMetadata = metadataDao.getMetadata(metadata.getRelativePath(), newName, (item instanceof VFSContainer)); + if(currentMetadata != null && !currentMetadata.equals(metadata)) { + metadata.copyValues(currentMetadata, false); + metadataDao.removeMetadata(currentMetadata); + } + Path newFile = Paths.get(folderModule.getCanonicalRoot(), metadata.getRelativePath(), newName); + ((VFSMetadataImpl)metadata).setFilename(newName); String uri = newFile.toFile().toURI().toString(); ((VFSMetadataImpl)metadata).setUri(uri); diff --git a/src/main/java/org/olat/core/commons/services/vfs/model/VFSMetadataImpl.java b/src/main/java/org/olat/core/commons/services/vfs/model/VFSMetadataImpl.java index 6d1d2b4e3813864841748f9823420ccb29f969f7..fda80a17555a73857d01a243cc66b159acb169dd 100644 --- a/src/main/java/org/olat/core/commons/services/vfs/model/VFSMetadataImpl.java +++ b/src/main/java/org/olat/core/commons/services/vfs/model/VFSMetadataImpl.java @@ -40,6 +40,7 @@ import org.olat.core.commons.services.vfs.VFSMetadata; import org.olat.core.gui.util.CSSHelper; import org.olat.core.id.Identity; import org.olat.core.id.Persistable; +import org.olat.core.util.StringHelper; /** * @@ -531,22 +532,46 @@ public class VFSMetadataImpl implements Persistable, VFSMetadata { } @Override - public void copyValues(VFSMetadata fromMeta) { - setAuthor(fromMeta.getAuthor()); - setComment(fromMeta.getComment()); - setCity(fromMeta.getCity()); - setCreator(fromMeta.getCreator()); - setLanguage(fromMeta.getLanguage()); - setPages(fromMeta.getPages()); - setPublicationDate(fromMeta.getPublicationDate()[1], fromMeta.getPublicationDate()[0]); - setPublisher(fromMeta.getPublisher()); - setSource(fromMeta.getSource()); - setTitle(fromMeta.getTitle()); - setUrl(fromMeta.getUrl()); - setLicenseType(fromMeta.getLicenseType()); - setLicenseTypeName(fromMeta.getLicenseTypeName()); - setLicensor(fromMeta.getLicensor()); - setLicenseText(fromMeta.getLicenseText()); + public void copyValues(VFSMetadata fromMeta, boolean override) { + if(override || getAuthor() == null) { + setAuthor(fromMeta.getAuthor()); + } + if(override || !StringHelper.containsNonWhitespace(getComment())) { + setComment(fromMeta.getComment()); + } + if(override || !StringHelper.containsNonWhitespace(getCity())) { + setCity(fromMeta.getCity()); + } + if(override || !StringHelper.containsNonWhitespace(getCreator())) { + setCreator(fromMeta.getCreator()); + } + if(override || !StringHelper.containsNonWhitespace(getLanguage())) { + setLanguage(fromMeta.getLanguage()); + } + if(override || !StringHelper.containsNonWhitespace(getPages())) { + setPages(fromMeta.getPages()); + } + if(override || getPublicationDate() == null) { + setPublicationDate(fromMeta.getPublicationDate()[1], fromMeta.getPublicationDate()[0]); + } + if(override || !StringHelper.containsNonWhitespace(getPublisher())) { + setPublisher(fromMeta.getPublisher()); + } + if(override || !StringHelper.containsNonWhitespace(getSource())) { + setSource(fromMeta.getSource()); + } + if(override || !StringHelper.containsNonWhitespace(getTitle())) { + setTitle(fromMeta.getTitle()); + } + if(override || !StringHelper.containsNonWhitespace(getUrl())) { + setUrl(fromMeta.getUrl()); + } + if(override || getLicenseType() == null) { + setLicenseType(fromMeta.getLicenseType()); + setLicenseTypeName(fromMeta.getLicenseTypeName()); + setLicensor(fromMeta.getLicensor()); + setLicenseText(fromMeta.getLicenseText()); + } } public void copyValues(VFSRevisionImpl fromMeta) { diff --git a/src/main/java/org/olat/core/util/FileUtils.java b/src/main/java/org/olat/core/util/FileUtils.java index 49877364b18c46d5ffe2192bc2b02310aa232299..9ac62ae7b73f5094b0a1418f7d04e1ac13c53e08 100644 --- a/src/main/java/org/olat/core/util/FileUtils.java +++ b/src/main/java/org/olat/core/util/FileUtils.java @@ -545,7 +545,8 @@ public class FileUtils { } /** - * Delete a file (not a filled directory) + * Delete a file (not a filled directory) and the metadata. + * * @param file The file * @return true if successfully deleted */ diff --git a/src/main/java/org/olat/course/nodes/ta/ConvertToGTACourseNode.java b/src/main/java/org/olat/course/nodes/ta/ConvertToGTACourseNode.java index 6bf5e26961e4280318e0dc4cd06f3c0e5bd687ad..6f92d67914aa173f10477e9e8ea5919dedad5d55 100644 --- a/src/main/java/org/olat/course/nodes/ta/ConvertToGTACourseNode.java +++ b/src/main/java/org/olat/course/nodes/ta/ConvertToGTACourseNode.java @@ -392,7 +392,7 @@ public class ConvertToGTACourseNode { } if(metaTarget != null) { - metaTarget.copyValues(metaSource); + metaTarget.copyValues(metaSource, true); CoreSpringFactory.getImpl(VFSRepositoryService.class).updateMetadata(metaTarget); } } diff --git a/src/main/java/org/olat/modules/library/ui/NotificationAcceptStepController.java b/src/main/java/org/olat/modules/library/ui/NotificationAcceptStepController.java index db5828d541643569e8c70fd928d1a19bcb8913bd..42cc408e3a336086f5cb2c39e7fcb0cdceab0ebd 100644 --- a/src/main/java/org/olat/modules/library/ui/NotificationAcceptStepController.java +++ b/src/main/java/org/olat/modules/library/ui/NotificationAcceptStepController.java @@ -145,7 +145,7 @@ public class NotificationAcceptStepController extends StepFormBasicController { } VFSMetadata metaInfo = targetFile.getMetaInfo(); - metaInfo.copyValues((VFSMetadata)getFromRunContext(MetadataAcceptStepController.STEPS_RUN_CONTEXT_METADATA_KEY)); + metaInfo.copyValues((VFSMetadata)getFromRunContext(MetadataAcceptStepController.STEPS_RUN_CONTEXT_METADATA_KEY), true); metaInfo = vfsRepositoryService.updateMetadata(metaInfo); if (metaInfo == null) { logError("Error writing metadata for " + relativeDestinationDirectoryName + "/" + relativeSourceFileName, null); diff --git a/src/test/java/org/olat/core/util/vfs/VFSTest.java b/src/test/java/org/olat/core/util/vfs/VFSTest.java index d46930bbface7c5e245aa751972e2dfc59604b3f..b1bd2232f0a9f963eefc3a672a9ae62a1f2840fa 100644 --- a/src/test/java/org/olat/core/util/vfs/VFSTest.java +++ b/src/test/java/org/olat/core/util/vfs/VFSTest.java @@ -22,13 +22,15 @@ package org.olat.core.util.vfs; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.List; import java.util.UUID; +import org.apache.logging.log4j.Logger; import org.junit.Assert; import org.junit.Test; import org.olat.core.commons.services.vfs.VFSMetadata; import org.olat.core.commons.services.vfs.VFSRepositoryService; -import org.apache.logging.log4j.Logger; +import org.olat.core.commons.services.vfs.manager.VFSMetadataDAO; import org.olat.core.logging.Tracing; import org.olat.core.util.FileUtils; import org.olat.test.OlatTestCase; @@ -47,6 +49,8 @@ public class VFSTest extends OlatTestCase { private static final String VFS_TEST_DIR = "/vfstest"; + @Autowired + private VFSMetadataDAO vfsMetadaDao; @Autowired private VFSRepositoryService vfsRepositoryService; @@ -107,6 +111,46 @@ public class VFSTest extends OlatTestCase { Assert.assertEquals("Always Me", renamedMetaInfo.getCreator()); } + @Test + public void renameSameName() { + // The first file + String filename = "samename.txt"; + VFSContainer testContainer = VFSManager.olatRootContainer(VFS_TEST_DIR, null); + testContainer = VFSManager.getOrCreateContainer(testContainer, UUID.randomUUID().toString()); + + VFSLeaf firstLeaf = testContainer.createChildLeaf(filename); + Assert.assertEquals(VFSConstants.YES, firstLeaf.canMeta()); + prepareFile(firstLeaf); + + VFSMetadata metaInfo = firstLeaf.getMetaInfo(); + metaInfo.setComment("my old comment"); + metaInfo.setCreator("Always Me"); + Assert.assertNotNull(vfsRepositoryService.updateMetadata(metaInfo)); + + // A second file + String newName = UUID.randomUUID() + ".txt"; + VFSLeaf secondLeaf = testContainer.createChildLeaf(newName); + Assert.assertEquals(VFSConstants.YES, secondLeaf.canMeta()); + prepareFile(secondLeaf); + + // Rename the second file to the first one + VFSStatus renamedStatus = firstLeaf.rename(filename); + Assert.assertEquals(VFSConstants.YES, renamedStatus); + + VFSItem renamedItem = testContainer.resolve(filename); + Assert.assertTrue(renamedItem instanceof VFSLeaf); + VFSLeaf renamedLeaf = (VFSLeaf)renamedItem; + + VFSMetadata renamedMetaInfo = renamedLeaf.getMetaInfo(); + Assert.assertEquals("my old comment", renamedMetaInfo.getComment()); + Assert.assertEquals("Always Me", renamedMetaInfo.getCreator()); + + + List<VFSMetadata> metadata = vfsMetadaDao.getMetadatas(metaInfo.getRelativePath()); + Assert.assertNotNull(metadata); + Assert.assertEquals(1, metadata.size()); + } + private void prepareFile(VFSLeaf file) { try(OutputStream out = file.getOutputStream(false); InputStream in = VFSTest.class.getResourceAsStream("test.txt")) {