|
7 | 7 | import java.io.File; |
8 | 8 | import java.io.IOException; |
9 | 9 | import java.nio.file.Paths; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.Map; |
10 | 12 | import java.util.Objects; |
11 | 13 |
|
12 | 14 | import javax.activation.MimetypesFileTypeMap; |
13 | 15 |
|
14 | 16 | public class FileUploader { |
15 | 17 |
|
| 18 | + // Static map for common file extensions to MIME types |
| 19 | + private static final Map<String, String> EXTENSION_TO_MIME; |
| 20 | + static { |
| 21 | + EXTENSION_TO_MIME = new HashMap<>(); |
| 22 | + EXTENSION_TO_MIME.put(".svg", "image/svg+xml"); |
| 23 | + EXTENSION_TO_MIME.put(".webp", "image/webp"); |
| 24 | + EXTENSION_TO_MIME.put(".json", "application/json"); |
| 25 | + EXTENSION_TO_MIME.put(".woff", "font/woff"); |
| 26 | + EXTENSION_TO_MIME.put(".woff2", "font/woff2"); |
| 27 | + EXTENSION_TO_MIME.put(".ttf", "font/ttf"); |
| 28 | + EXTENSION_TO_MIME.put(".otf", "font/otf"); |
| 29 | + EXTENSION_TO_MIME.put(".eot", "application/vnd.ms-fontobject"); |
| 30 | + EXTENSION_TO_MIME.put(".mp4", "video/mp4"); |
| 31 | + EXTENSION_TO_MIME.put(".m4a", "audio/mp4"); |
| 32 | + EXTENSION_TO_MIME.put(".mkv", "video/x-matroska"); |
| 33 | + EXTENSION_TO_MIME.put(".webm", "video/webm"); |
| 34 | + EXTENSION_TO_MIME.put(".ico", "image/x-icon"); |
| 35 | + EXTENSION_TO_MIME.put(".csv", "text/csv"); |
| 36 | + EXTENSION_TO_MIME.put(".md", "text/markdown"); |
| 37 | + } |
16 | 38 |
|
17 | 39 | public MultipartBody createMultipartBody(String filePath, String parentUid, String title, String description, String[] tags) { |
18 | 40 | MultipartBody.Builder builder = new MultipartBody.Builder(); |
@@ -47,9 +69,16 @@ public MultipartBody createMultipartBody(String filePath, String parentUid, Stri |
47 | 69 |
|
48 | 70 | // Helper method to get content type of file |
49 | 71 | private String getContentType(File file) { |
| 72 | + String name = file.getName().toLowerCase(); |
| 73 | + int dot = name.lastIndexOf('.'); |
| 74 | + if (dot != -1) { |
| 75 | + String ext = name.substring(dot); |
| 76 | + String mime = EXTENSION_TO_MIME.get(ext); |
| 77 | + if (mime != null) return mime; |
| 78 | + } |
50 | 79 | try { |
51 | | - java.nio.file.Path source = Paths.get(file.toString()); |
52 | | - MimetypesFileTypeMap m = new MimetypesFileTypeMap(source.toString()); |
| 80 | + java.nio.file.Path source = java.nio.file.Paths.get(file.toString()); |
| 81 | + javax.activation.MimetypesFileTypeMap m = new javax.activation.MimetypesFileTypeMap(source.toString()); |
53 | 82 | return m.getContentType(file); |
54 | 83 | } catch (IOException e) { |
55 | 84 | throw new RuntimeException("Failed to determine content type of file", e); |
|
0 commit comments