Some changes for mime handling
- added additional formats - added get function - added tests
This commit is contained in:
parent
119fc82267
commit
0ba153b118
3 changed files with 72 additions and 12 deletions
|
|
@ -28,10 +28,7 @@ import zutil.io.file.FileUtil;
|
||||||
import zutil.parser.DataNode;
|
import zutil.parser.DataNode;
|
||||||
import zutil.parser.json.JSONParser;
|
import zutil.parser.json.JSONParser;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.*;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.FileReader;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
|
@ -40,7 +37,12 @@ import java.util.*;
|
||||||
*/
|
*/
|
||||||
public class MimeTypeUtil {
|
public class MimeTypeUtil {
|
||||||
|
|
||||||
// Define mime types
|
// Static variables
|
||||||
|
|
||||||
|
private static final ArrayList<MimeType> mimes = new ArrayList<MimeType>();
|
||||||
|
private static final HashMap<String, MimeType> mimesByExtenion = new HashMap<>();
|
||||||
|
|
||||||
|
// Initialize mime types
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
readMimeFile("zutil/data/mime.json");
|
readMimeFile("zutil/data/mime.json");
|
||||||
|
|
@ -49,14 +51,8 @@ public class MimeTypeUtil {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Static variables
|
|
||||||
|
|
||||||
private static final ArrayList<MimeType> mimes = new ArrayList<MimeType>();
|
|
||||||
|
|
||||||
|
|
||||||
private static void readMimeFile(String path) throws IOException {
|
private static void readMimeFile(String path) throws IOException {
|
||||||
BufferedReader in = new BufferedReader(new FileReader(FileUtil.find(path)));
|
DataNode json = JSONParser.read(FileUtil.getContent(path));
|
||||||
DataNode json = new JSONParser(in).read();
|
|
||||||
|
|
||||||
for (Iterator<String> it = json.keyIterator(); it.hasNext(); ) {
|
for (Iterator<String> it = json.keyIterator(); it.hasNext(); ) {
|
||||||
String primaryType = it.next();
|
String primaryType = it.next();
|
||||||
|
|
@ -76,9 +72,17 @@ public class MimeTypeUtil {
|
||||||
|
|
||||||
private static void addMimeType(MimeType mime){
|
private static void addMimeType(MimeType mime){
|
||||||
mimes.add(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{
|
public static class MimeType{
|
||||||
private final String primaryType;
|
private final String primaryType;
|
||||||
|
|
@ -101,6 +105,14 @@ public class MimeTypeUtil {
|
||||||
return subType;
|
return subType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getExtensions() {
|
||||||
|
return extensions;
|
||||||
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return primaryType + "/" + subType;
|
return primaryType + "/" + subType;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -162,10 +162,18 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"video": {
|
"video": {
|
||||||
|
"x-matroska": {
|
||||||
|
"description": "Matroska Multimedia Container",
|
||||||
|
"extensions": ["mkv"]
|
||||||
|
},
|
||||||
"x-msvideo": {
|
"x-msvideo": {
|
||||||
"description": "AVI: Audio Video Interleave",
|
"description": "AVI: Audio Video Interleave",
|
||||||
"extensions": ["avi"]
|
"extensions": ["avi"]
|
||||||
},
|
},
|
||||||
|
"mp4": {
|
||||||
|
"description": "MPEG-4 Video",
|
||||||
|
"extensions": ["mp4"]
|
||||||
|
},
|
||||||
"mpeg": {
|
"mpeg": {
|
||||||
"description": "MPEG Video",
|
"description": "MPEG Video",
|
||||||
"extensions": ["mpeg"]
|
"extensions": ["mpeg"]
|
||||||
|
|
|
||||||
40
test/zutil/MimeTypeUtilTest.java
Normal file
40
test/zutil/MimeTypeUtilTest.java
Normal file
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue