From 0ba153b11800e6e7919bc877117051d5c4d419ba Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Sat, 3 Nov 2018 20:33:07 +0100 Subject: [PATCH] Some changes for mime handling - added additional formats - added get function - added tests --- src/zutil/MimeTypeUtil.java | 36 ++++++++++++++++++---------- src/zutil/data/mime.json | 8 +++++++ test/zutil/MimeTypeUtilTest.java | 40 ++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 test/zutil/MimeTypeUtilTest.java diff --git a/src/zutil/MimeTypeUtil.java b/src/zutil/MimeTypeUtil.java index 58428e6..aea557b 100644 --- a/src/zutil/MimeTypeUtil.java +++ b/src/zutil/MimeTypeUtil.java @@ -28,10 +28,7 @@ import zutil.io.file.FileUtil; import zutil.parser.DataNode; import zutil.parser.json.JSONParser; -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; +import java.io.*; import java.lang.reflect.Array; import java.util.*; @@ -40,7 +37,12 @@ import java.util.*; */ public class MimeTypeUtil { - // Define mime types + // Static variables + + private static final ArrayList mimes = new ArrayList(); + private static final HashMap mimesByExtenion = new HashMap<>(); + + // Initialize mime types static { try { readMimeFile("zutil/data/mime.json"); @@ -49,14 +51,8 @@ public class MimeTypeUtil { } } - // Static variables - - private static final ArrayList mimes = new ArrayList(); - - private static void readMimeFile(String path) throws IOException { - BufferedReader in = new BufferedReader(new FileReader(FileUtil.find(path))); - DataNode json = new JSONParser(in).read(); + DataNode json = JSONParser.read(FileUtil.getContent(path)); for (Iterator it = json.keyIterator(); it.hasNext(); ) { String primaryType = it.next(); @@ -76,9 +72,17 @@ public class MimeTypeUtil { private static void addMimeType(MimeType mime){ mimes.add(mime); + + for (String extension : mime.getExtensions()) { + mimesByExtenion.put(extension, mime); + } } + public static MimeType getMimeByExtension(String extension) { + return mimesByExtenion.get(extension); + } + public static class MimeType{ private final String primaryType; @@ -101,6 +105,14 @@ public class MimeTypeUtil { return subType; } + public String getDescription() { + return description; + } + + public String[] getExtensions() { + return extensions; + } + public String toString() { return primaryType + "/" + subType; } diff --git a/src/zutil/data/mime.json b/src/zutil/data/mime.json index b2631ed..597ab6f 100644 --- a/src/zutil/data/mime.json +++ b/src/zutil/data/mime.json @@ -162,10 +162,18 @@ } }, "video": { + "x-matroska": { + "description": "Matroska Multimedia Container", + "extensions": ["mkv"] + }, "x-msvideo": { "description": "AVI: Audio Video Interleave", "extensions": ["avi"] }, + "mp4": { + "description": "MPEG-4 Video", + "extensions": ["mp4"] + }, "mpeg": { "description": "MPEG Video", "extensions": ["mpeg"] diff --git a/test/zutil/MimeTypeUtilTest.java b/test/zutil/MimeTypeUtilTest.java new file mode 100644 index 0000000..55c1fe1 --- /dev/null +++ b/test/zutil/MimeTypeUtilTest.java @@ -0,0 +1,40 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018 Ziver Koc + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package zutil; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class MimeTypeUtilTest { + + @Test + public void getMimeByExtension() { + assertEquals("image/jpeg", MimeTypeUtil.getMimeByExtension("jpg").toString()); + assertEquals("image/png", MimeTypeUtil.getMimeByExtension("png").toString()); + assertEquals("video/mp4", MimeTypeUtil.getMimeByExtension("mp4").toString()); + assertEquals("video/x-matroska", MimeTypeUtil.getMimeByExtension("mkv").toString()); + } +} \ No newline at end of file