Skip to content

Commit 980e121

Browse files
Jiakaicszl97
authored andcommitted
feat: file utils supports
1 parent caa06f6 commit 980e121

File tree

7 files changed

+64
-54
lines changed

7 files changed

+64
-54
lines changed

api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>top.bella</groupId>
88
<artifactId>openai-java</artifactId>
9-
<version>0.23.86</version>
9+
<version>0.23.87</version>
1010
</parent>
1111
<packaging>jar</packaging>
1212
<artifactId>openai-api</artifactId>

client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>top.bella</groupId>
88
<artifactId>openai-java</artifactId>
9-
<version>0.23.86</version>
9+
<version>0.23.87</version>
1010
</parent>
1111
<packaging>jar</packaging>
1212

example/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.ke</groupId>
88
<artifactId>example</artifactId>
9-
<version>0.23.86</version>
9+
<version>0.23.87</version>
1010
<name>example</name>
1111

1212
<properties>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>top.bella</groupId>
77
<artifactId>openai-java</artifactId>
8-
<version>0.23.86</version>
8+
<version>0.23.87</version>
99
<packaging>pom</packaging>
1010
<description>openai java 版本</description>
1111
<name>openai-java</name>

service/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>top.bella</groupId>
88
<artifactId>openai-java</artifactId>
9-
<version>0.23.86</version>
9+
<version>0.23.87</version>
1010
</parent>
1111
<packaging>jar</packaging>
1212

@@ -41,6 +41,11 @@
4141
</exclusion>
4242
</exclusions>
4343
</dependency>
44+
<dependency>
45+
<groupId>com.squareup.okhttp3</groupId>
46+
<artifactId>okhttp</artifactId>
47+
<version>4.12.0</version>
48+
</dependency>
4449

4550
<dependency>
4651
<groupId>org.mockito</groupId>

service/src/main/java/com/theokanning/openai/service/FileUtil.java renamed to service/src/main/java/com/theokanning/openai/service/FileUtils.java

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
package com.theokanning.openai.service;
22

3-
import okhttp3.MediaType;
4-
53
import java.io.ByteArrayOutputStream;
64
import java.io.IOException;
75
import java.io.InputStream;
86
import java.util.HashMap;
97
import 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-

service/src/main/java/com/theokanning/openai/service/OpenAiService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@
110110
import java.nio.file.Files;
111111
import java.nio.file.Path;
112112
import java.nio.file.Paths;
113-
import java.nio.file.StandardCopyOption;
114113
import java.time.Duration;
115114
import java.time.LocalDate;
116115
import java.util.ArrayList;
@@ -341,7 +340,7 @@ public Flowable<CompletionChunk> streamCompletion(CompletionRequest request) {
341340
*/
342341
public File uploadFile(String purpose, byte[] bytes, String filename) {
343342
RequestBody purposeBody = RequestBody.create(MultipartBody.FORM, purpose);
344-
RequestBody fileBody = RequestBody.create(FileUtil.getFileUploadMediaType(filename), bytes);
343+
RequestBody fileBody = RequestBody.create(FileUtils.extraMediaType(filename), bytes);
345344
MultipartBody.Part body = MultipartBody.Part.createFormData("file", filename, fileBody);
346345
return execute(api.uploadFile(purposeBody, body));
347346
}
@@ -374,7 +373,7 @@ public File uploadFile(String purpose, InputStream fileInputStream, String filen
374373
RequestBody fileBody = new RequestBody() {
375374
@Override
376375
public MediaType contentType() {
377-
return FileUtil.getFileUploadMediaType(filename);
376+
return FileUtils.extraMediaType(filename);
378377
}
379378

380379
@Override

0 commit comments

Comments
 (0)