From a0b445d7885312ba0c330474ce460dc4749c6e27 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 31 May 2016 19:34:52 +0200 Subject: [PATCH] Use better MIME type detection --- pom.xml | 2 +- .../de/todesbaum/jsite/application/Project.java | 26 ++++++++++++++++++++-- .../todesbaum/jsite/application/ProjectTest.java | 22 ++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 src/test/java/de/todesbaum/jsite/application/ProjectTest.java diff --git a/pom.xml b/pom.xml index 411146f..0becb74 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ net.pterodactylus utils - 0.12.1 + 0.13 junit diff --git a/src/main/java/de/todesbaum/jsite/application/Project.java b/src/main/java/de/todesbaum/jsite/application/Project.java index aa0c00b..59dcba9 100644 --- a/src/main/java/de/todesbaum/jsite/application/Project.java +++ b/src/main/java/de/todesbaum/jsite/application/Project.java @@ -19,8 +19,10 @@ 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 { */ 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 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 getSuffixes(String filename) { + List 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 index 0000000..df89de9 --- /dev/null +++ b/src/test/java/de/todesbaum/jsite/application/ProjectTest.java @@ -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 David ‘Bombe’ Roden + */ +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")); + } + +} -- 2.7.4