11package com .theokanning .openai .service ;
22
3- import okhttp3 .MediaType ;
4-
53import java .io .ByteArrayOutputStream ;
64import java .io .IOException ;
75import java .io .InputStream ;
86import java .util .HashMap ;
97import java .util .Map ;
108
11- /**
12- * @author LiangTao
13- * @date 2024年05月08 17:29
14- **/
15- public class FileUtil {
9+ import okhttp3 .MediaType ;
10+
11+ public class FileUtils {
12+
1613 private static final Map <String , String > MIME_TO_EXTENSION = new HashMap <>();
1714 private static final Map <String , String > EXTENSION_TO_MIME = new HashMap <>();
18-
1915 static {
2016 // 初始化MIME类型到文件扩展名的映射
2117 MIME_TO_EXTENSION .put ("text/html" , "html" );
@@ -60,18 +56,6 @@ public class FileUtil {
6056 MIME_TO_EXTENSION .put ("application/epub+zip" , "epub" );
6157 MIME_TO_EXTENSION .put ("message/rfc822" , "eml" );
6258 MIME_TO_EXTENSION .put ("application/vnd.ms-outlook" , "msg" );
63-
64- // 编程语言相关的MIME类型
65- MIME_TO_EXTENSION .put ("text/x-c" , "c" );
66- MIME_TO_EXTENSION .put ("text/x-csharp" , "cs" );
67- MIME_TO_EXTENSION .put ("text/x-c++" , "cpp" );
68- MIME_TO_EXTENSION .put ("text/x-java" , "java" );
69- MIME_TO_EXTENSION .put ("text/x-php" , "php" );
70- MIME_TO_EXTENSION .put ("text/x-python" , "py" );
71- MIME_TO_EXTENSION .put ("text/x-ruby" , "rb" );
72- MIME_TO_EXTENSION .put ("text/x-tex" , "tex" );
73- MIME_TO_EXTENSION .put ("application/x-sh" , "sh" );
74- MIME_TO_EXTENSION .put ("application/typescript" , "ts" );
7559 // 可以根据需要添加更多的MIME类型
7660
7761 EXTENSION_TO_MIME .put ("html" , "text/html" );
@@ -93,16 +77,7 @@ public class FileUtil {
9377 EXTENSION_TO_MIME .put ("mp4" , "video/mp4" );
9478 EXTENSION_TO_MIME .put ("mov" , "video/quicktime" );
9579 EXTENSION_TO_MIME .put ("mpeg" , "video/mpeg" );
96- EXTENSION_TO_MIME .put ("c" , "text/x-c" );
97- EXTENSION_TO_MIME .put ("cs" , "text/x-csharp" );
98- EXTENSION_TO_MIME .put ("cpp" , "text/x-c++" );
99- EXTENSION_TO_MIME .put ("java" , "text/x-java" );
100- EXTENSION_TO_MIME .put ("php" , "text/x-php" );
101- EXTENSION_TO_MIME .put ("py" , "text/x-python" );
102- EXTENSION_TO_MIME .put ("rb" , "text/x-ruby" );
103- EXTENSION_TO_MIME .put ("tex" , "text/x-tex" );
104- EXTENSION_TO_MIME .put ("sh" , "application/x-sh" );
105- EXTENSION_TO_MIME .put ("ts" , "application/typescript" );
80+
10681 EXTENSION_TO_MIME .put ("doc" , "application/msword" );
10782 EXTENSION_TO_MIME .put ("json" , "application/json" );
10883 EXTENSION_TO_MIME .put ("docx" , "application/vnd.openxmlformats-officedocument.wordprocessingml.document" );
@@ -117,14 +92,46 @@ public class FileUtil {
11792 EXTENSION_TO_MIME .put ("epub" , "application/epub+zip" );
11893 EXTENSION_TO_MIME .put ("eml" , "message/rfc822" );
11994 EXTENSION_TO_MIME .put ("msg" , "application/vnd.ms-outlook" );
120-
121-
12295 }
12396
12497 public static MediaType getFileUploadMediaType (String fileName ) {
12598 return MediaType .parse (EXTENSION_TO_MIME .getOrDefault (getFileExtension (fileName ), "text/plain" ));
12699 }
127100
101+ public static MediaType getMediaType (String contentType ) {
102+ return MediaType .parse (contentType );
103+ }
104+
105+ public static String getExtensionFromMimeType (MediaType mediaType ) {
106+ String pure = extraPureMediaType (mediaType );
107+ return MIME_TO_EXTENSION .getOrDefault (pure .toLowerCase (), "bin" );
108+ }
109+
110+ /**
111+ * such as "text", "image", "audio", "video", or "application".
112+ *
113+ * @return
114+ */
115+ public static String getType (String mediaType ) {
116+ MediaType MediaTypeFiltered = MediaType .parse (mediaType );
117+ return getType (MediaTypeFiltered );
118+ }
119+
120+ public static String getType (MediaType mediaType ) {
121+ String t = extraPureMediaType (mediaType ).toLowerCase ();
122+ if (t .startsWith ("image" )) {
123+ return "image" ;
124+ } else if (t .startsWith ("audio" )) {
125+ return "audio" ;
126+ } else if (t .startsWith ("video" )) {
127+ return "video" ;
128+ } else if (t .startsWith ("text" )) {
129+ return "text" ;
130+ } else {
131+ return MIME_TO_EXTENSION .getOrDefault (t , "binary" );
132+ }
133+ }
134+
128135 public static String getSubType (MediaType mediaType ) {
129136 if (mediaType == null ) {
130137 return null ;
@@ -136,20 +143,22 @@ public static String extraPureMediaType(MediaType mediaType) {
136143 return mediaType .type () + "/" + mediaType .subtype ();
137144 }
138145
146+ public static MediaType extraMediaType (String filename ) {
147+ String extension = getFileExtension (filename );
148+ String mimeType = EXTENSION_TO_MIME .get (extension );
149+ if (null == mimeType || "" .equals (mimeType )) {
150+ return null ;
151+ }
152+ return MediaType .parse (mimeType );
153+ }
154+
139155 public static String getFileExtension (String filename ) {
140- int dotIndex = filename .lastIndexOf ('.' );
141- if (dotIndex >= 0 && dotIndex < filename .length () - 1 ) {
142- return filename .substring (dotIndex + 1 ); // Includes the dot
156+ if (filename == null || filename .lastIndexOf ("." ) == -1 ) {
157+ return null ;
143158 }
144- return "" ; // No extension found
159+ return filename . substring ( filename . lastIndexOf ( "." ) + 1 );
145160 }
146161
147- /**
148- * Helper method to read all bytes from an InputStream
149- *
150- * @param inputStream the InputStream to read from
151- * @return a byte array containing all the bytes read from the InputStream
152- */
153162 public static byte [] readAllBytes (InputStream inputStream ) {
154163 try (ByteArrayOutputStream buffer = new ByteArrayOutputStream ()) {
155164 int nRead ;
@@ -160,10 +169,7 @@ public static byte[] readAllBytes(InputStream inputStream) {
160169 buffer .flush ();
161170 return buffer .toByteArray ();
162171 } catch (IOException e ) {
163- throw new RuntimeException ("Error reading from InputStream" , e );
172+ throw new IllegalStateException ("Error reading from InputStream" , e );
164173 }
165174 }
166-
167-
168175}
169-
0 commit comments