Skip to content

Commit

Permalink
Fixes file extension handling
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Schnicke <frank.schnicke@iese.fraunhofer.de>
  • Loading branch information
FrankSchnicke committed Mar 21, 2023
1 parent f297ae7 commit 0182170
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,12 @@
<version>5.2.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import java.util.stream.Collectors;

import org.apache.commons.io.IOUtils;
import org.apache.tika.mime.MimeType;
import org.apache.tika.mime.MimeTypeException;
import org.apache.tika.mime.MimeTypes;
import org.eclipse.basyx.submodel.metamodel.api.ISubmodel;
import org.eclipse.basyx.submodel.metamodel.api.submodelelement.ISubmodelElement;
import org.eclipse.basyx.submodel.metamodel.api.submodelelement.operation.IOperation;
Expand Down Expand Up @@ -173,7 +176,20 @@ private void createFile(String idShortPath, Object newValue, ISubmodelElement su
private String getFilePath(String idShortPath, File file) {
String fileName = idShortPath.replaceAll("/", "-");

return tmpDirectory + "/" + fileName;
String extension = getFileExtension(file);

return tmpDirectory + "/" + fileName + extension;
}

private String getFileExtension(File file) {
MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
try {
MimeType mimeType = allTypes.forName(file.getMimeType());
return mimeType.getExtension();
} catch (MimeTypeException e) {
e.printStackTrace();
return "";
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void fileIsUploadedCorrectly() throws IOException {

java.io.File file = submodelAPI.getSubmodelElementFile(FILE_SME_ID_SHORT);

assertEquals("fileSmeIdShort", file.getName());
assertEquals("fileSmeIdShort.xml", file.getName());
assertEquals(tempFile.length(), file.length());
}

Expand Down

0 comments on commit 0182170

Please sign in to comment.