Use better MIME type detection
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Tue, 31 May 2016 17:34:52 +0000 (19:34 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Tue, 31 May 2016 17:37:24 +0000 (19:37 +0200)
pom.xml
src/main/java/de/todesbaum/jsite/application/Project.java
src/test/java/de/todesbaum/jsite/application/ProjectTest.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 411146f..0becb74 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -11,7 +11,7 @@
                <dependency>
                        <groupId>net.pterodactylus</groupId>
                        <artifactId>utils</artifactId>
-                       <version>0.12.1</version>
+                       <version>0.13</version>
                </dependency>
                <dependency>
                        <groupId>junit</groupId>
index aa0c00b..59dcba9 100644 (file)
 package de.todesbaum.jsite.application;
 
 import java.io.File;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
@@ -365,13 +367,33 @@ public class Project implements Comparable<Project> {
         */
        public FileOption getFileOption(String filename) {
                FileOption fileOption = fileOptions.get(filename);
+               String defaultMimeType = "application/octet-stream";
                if (fileOption == null) {
-                       fileOption = new FileOption(MimeTypes.getMimeType(filename.substring(filename.lastIndexOf('.') + 1)));
-                       fileOptions.put(filename, fileOption);
+                       List<String> suffixes = getSuffixes(filename);
+                       for (String suffix : suffixes) {
+                               String mimeType = MimeTypes.getMimeType(suffix);
+                               if (!mimeType.equals(defaultMimeType)) {
+                                       defaultMimeType = mimeType;
+                                       break;
+                               }
+                       }
+                       fileOption = new FileOption(defaultMimeType);
                }
+               fileOptions.put(filename, fileOption);
                return fileOption;
        }
 
+       private List<String> getSuffixes(String filename) {
+               List<String> suffixes = new ArrayList<>();
+               int dot = filename.lastIndexOf(".");
+               while (dot > -1) {
+                       String suffix = filename.substring(dot + 1);
+                       suffixes.add(0, suffix);
+                       dot = filename.lastIndexOf(".", dot - 1);
+               }
+               return suffixes;
+       }
+
        /**
         * Sets options for a file.
         *
diff --git a/src/test/java/de/todesbaum/jsite/application/ProjectTest.java b/src/test/java/de/todesbaum/jsite/application/ProjectTest.java
new file mode 100644 (file)
index 0000000..df89de9
--- /dev/null
@@ -0,0 +1,22 @@
+package de.todesbaum.jsite.application;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link ProjectTest}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ProjectTest {
+
+       @Test
+       public void mimeTypeForTarBz2IsRecognizedCorrectly() {
+               Project project = new Project();
+               FileOption fileOption = project.getFileOption("foo.tar.bz2");
+               assertThat(fileOption.getMimeType(), is("application/x-gtar"));
+       }
+
+}