Merge pull request #14701 from rajat-garg/rg/extension-from-mime-type

[BAEL-6730] Add getting Extension from MIME type of a file
This commit is contained in:
Liam Williams 2023-09-07 18:34:53 +01:00 committed by GitHub
commit 62e3033f5c
2 changed files with 73 additions and 0 deletions

View File

@ -43,6 +43,17 @@
<version>${angus-activation.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jodd</groupId>
<artifactId>jodd-util</artifactId>
<version>6.2.1</version>
</dependency>
<dependency>
<groupId>com.j256.simplemagic</groupId>
<artifactId>simplemagic</artifactId>
<version>1.17</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,62 @@
package com.baeldung.extension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.tika.mime.MimeTypeException;
import org.junit.Test;
import com.j256.simplemagic.ContentInfo;
public class ExtensionFromMimeTypeUnitTest {
private static final String IMAGE_JPEG_MIME_TYPE = "image/jpeg";
@Test
public void whenUsingTika_thenGetFileExtension() throws MimeTypeException {
List<String> expectedExtensions = Arrays.asList(".jpg", ".jpeg", ".jpe", ".jif", ".jfif", ".jfi");
org.apache.tika.mime.MimeTypes allTypes = org.apache.tika.mime.MimeTypes.getDefaultMimeTypes();
org.apache.tika.mime.MimeType type = allTypes.forName(IMAGE_JPEG_MIME_TYPE);
String primaryExtension = type.getExtension();
assertEquals(".jpg", primaryExtension);
List<String> detectedExtensions = type.getExtensions();
assertThat(detectedExtensions).containsExactlyElementsOf(expectedExtensions);
}
@Test
public void whenUsingJodd_thenGetFileExtension() {
List<String> expectedExtensions = Arrays.asList("jpeg", "jpg", "jpe");
String[] detectedExtensions = jodd.net.MimeTypes.findExtensionsByMimeTypes(IMAGE_JPEG_MIME_TYPE, false);
assertThat(detectedExtensions).containsExactlyElementsOf(expectedExtensions);
}
@Test
public void whenUsingMimetypesFileTypeMap_thenGetFileExtension() {
List<String> expectedExtensions = Arrays.asList("jpeg", "jpg", "jpe");
ContentInfo contentInfo = new ContentInfo("", IMAGE_JPEG_MIME_TYPE, "", true);
String[] detectedExtensions = contentInfo.getFileExtensions();
assertThat(detectedExtensions).containsExactlyElementsOf(expectedExtensions);
}
@Test
public void whenUsingCustomLogic_thenGetFileExtension() {
Map<String, Set<String>> mimeExtensionsMap = new HashMap<>();
List<String> expectedExtensions = Arrays.asList(".jpg", ".jpe", ".jpeg");
addMimeExtensions(mimeExtensionsMap, "image/jpeg", ".jpg");
addMimeExtensions(mimeExtensionsMap, "image/jpeg", ".jpe");
addMimeExtensions(mimeExtensionsMap, "image/jpeg", ".jpeg");
Set<String> detectedExtensions = mimeExtensionsMap.get(IMAGE_JPEG_MIME_TYPE);
assertThat(detectedExtensions).containsExactlyElementsOf(expectedExtensions);
}
private void addMimeExtensions(Map<String, Set<String>> map, String mimeType, String extension) {
map.computeIfAbsent(mimeType, k -> new HashSet<>()).add(extension);
}
}