diff --git a/README.md b/README.md index 9a501fae..f080f2b5 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,9 @@ [![Components Lib javadoc](https://javadoc.io/badge2/org.eclipse.basyx/basyx.components.lib/javadoc.svg)](https://javadoc.io/doc/org.eclipse.basyx/basyx.components.lib) Components Lib +This repository contains BaSyx Java V1. If you're looking for BaSyx Java V2, see [here](https://github.com/eclipse-basyx/basyx-java-server-sdk). + + The Eclipse BaSyx Java Components are part of the [Eclipse BaSyx middleware](https://www.eclipse.org/basyx/). The Components utilize the [BaSyx Java SDK](https://github.com/eclipse-basyx/basyx-java-sdk) to provide easy to use off-the-shelf components and have been created within the [BaSys](https://www.basys40.de/) project. ## Eclipse Wiki diff --git a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/mongodb/TestMongoDBSubmodelAPI.java b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/mongodb/TestMongoDBSubmodelAPI.java index a05dbee2..3068c067 100644 --- a/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/mongodb/TestMongoDBSubmodelAPI.java +++ b/basyx.components/basyx.components.docker/basyx.components.AASServer/src/test/java/org/eclipse/basyx/regression/AASServer/mongodb/TestMongoDBSubmodelAPI.java @@ -33,6 +33,7 @@ import java.io.OutputStream; import java.util.Collection; import java.util.Map; +import java.util.Objects; import org.eclipse.basyx.aas.metamodel.map.descriptor.CustomId; import org.eclipse.basyx.components.aas.mongodb.MongoDBSubmodelAPI; @@ -91,7 +92,7 @@ public void fileSubmodelElementFileUpload() throws FileNotFoundException { java.io.File value = submodelAPI.getSubmodelElementFile("fileSmeIdShort"); - assertEquals("mySubmodelId-fileSmeIdShort.xml", value.getName()); + assertEquals("#"+ Objects.hashCode(submodelAPI.getSubmodelId())+"#mySubmodelId-fileSmeIdShort.xml", value.getName()); assertEquals(expected.length(), value.length()); } diff --git a/basyx.components/basyx.components.lib/src/main/java/org/eclipse/basyx/components/internal/mongodb/MongoDBBaSyxStorageAPI.java b/basyx.components/basyx.components.lib/src/main/java/org/eclipse/basyx/components/internal/mongodb/MongoDBBaSyxStorageAPI.java index 39f7484a..393faf7c 100644 --- a/basyx.components/basyx.components.lib/src/main/java/org/eclipse/basyx/components/internal/mongodb/MongoDBBaSyxStorageAPI.java +++ b/basyx.components/basyx.components.lib/src/main/java/org/eclipse/basyx/components/internal/mongodb/MongoDBBaSyxStorageAPI.java @@ -30,6 +30,7 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.util.Collection; import java.util.Map; @@ -172,11 +173,17 @@ public java.io.File getFile(String idShortPath, String parentKey, Map) element); GridFSBucket bucket = getGridFSBucket(client, config); String fileName = constructFileName(submodelId, file, idShortPath); - deleteAllDuplicateFiles(bucket, fileName); + deleteAllDuplicateFiles(bucket, fileName, legacyFileName(submodelId, file, idShortPath)); bucket.uploadFromStream(fileName, newValue); return fileName; } @@ -69,7 +72,11 @@ public static void deleteAllFilesFromGridFsIfIsFileSubmodelElement(MongoClient c return; File file = File.createAsFacade(submodelElement); GridFSBucket bucket = MongoDBFileHelper.getGridFSBucket(client, config); - bucket.find(Filters.eq("filename", file.getValue())).forEach(gridFile -> bucket.delete(gridFile.getObjectId())); + deleteAllDuplicateFiles(bucket, constructFileName(sm.getIdentification().getId(), file, idShort), legacyFileName(sm.getIdentification().getId(), file, idShort)); + } + + protected static boolean fileExists(GridFSBucket bucket, String fileName) { + return bucket.find(Filters.eq("filename", fileName)).first() != null; } public static GridFSBucket getGridFSBucket(MongoClient client, BaSyxMongoDBConfiguration config) { @@ -77,14 +84,32 @@ public static GridFSBucket getGridFSBucket(MongoClient client, BaSyxMongoDBConfi return GridFSBuckets.create(database, config.getFileCollection()); } - private static void deleteAllDuplicateFiles(GridFSBucket bucket, String fileName) { - bucket.find(Filters.eq("filename", fileName)).forEach(gridFile -> bucket.delete(gridFile.getObjectId())); + private static void deleteAllDuplicateFiles(GridFSBucket bucket, String... fileNames) { + bucket.find(Filters.or( + Arrays.stream(fileNames) + .map(fileName -> Filters.eq("filename", fileName)) + .collect(Collectors.toList()))) + .forEach(gridFile -> bucket.delete(gridFile.getObjectId())); } public static String constructFileName(String submodelId, File file, String idShortPath) { + String fileName = submodelId + "-" + idShortPath.replaceAll("/", "-") + getFileExtension(file); + // replace those chars that are not permitted on filesystems + fileName = fileName.replaceAll("[^a-zA-Z0-9-_\\.]", "_"); + // ensure the filename is still unique to be used as key in MongoDB storage layer + return String.format("#%s#", Objects.hashCode(submodelId)).concat(fileName); + } + /** + * This method is kept for backwards compatibility to still find files from DB which where stored with old scheme. + * @param file the filename of this file is requested. + * @param idShortPath the shortId of the given file. + * @return the filename as it was constructed in older versions. + */ + public static String legacyFileName(String submodelId, File file, String idShortPath) { return submodelId + "-" + idShortPath.replaceAll("/", "-") + getFileExtension(file); } + private static String getFileExtension(File file) { MimeTypes allTypes = MimeTypes.getDefaultMimeTypes(); try {