Skip to content

Commit 4eaee7a

Browse files
committed
fix: modify resource upload
1 parent f1fe5d0 commit 4eaee7a

File tree

3 files changed

+71
-25
lines changed

3 files changed

+71
-25
lines changed

base/src/main/java/com/tinyengine/it/common/utils/ImageThumbnailGenerator.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
import org.apache.batik.transcoder.image.JPEGTranscoder;
2020
import org.apache.batik.transcoder.image.PNGTranscoder;
2121
import org.apache.batik.util.XMLResourceDescriptor;
22+
import org.springframework.web.multipart.MultipartFile;
2223
import org.w3c.dom.Document;
2324

2425
import javax.imageio.ImageIO;
2526
import java.awt.*;
2627
import java.awt.image.BufferedImage;
2728
import java.io.ByteArrayInputStream;
2829
import java.io.ByteArrayOutputStream;
30+
import java.io.IOException;
2931
import java.util.Base64;
3032
import java.util.HashMap;
3133
import java.util.Map;
@@ -235,6 +237,29 @@ public static String extractContentType(String base64Data) {
235237
throw new IllegalArgumentException("Cannot extract content type from Base64 data");
236238
}
237239

240+
/**
241+
* 检查文件是否为图片类型
242+
*/
243+
public static boolean validateByImageIO(MultipartFile file) {
244+
try {
245+
BufferedImage image = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
246+
return image != null; // 如果是图片,返回true
247+
} catch (IOException e) {
248+
return false;
249+
}
250+
}
251+
252+
/**
253+
* 将MultipartFile转换为Base64字符串
254+
*/
255+
public static String convertToBase64(MultipartFile file) throws IOException {
256+
String mimeType = file.getContentType();
257+
byte[] fileBytes = file.getBytes();
258+
String base64 = Base64.getEncoder().encodeToString(fileBytes);
259+
260+
return "data:" + mimeType + ";base64," + base64;
261+
}
262+
238263
/**
239264
* MIME类型转格式
240265
*/

base/src/main/java/com/tinyengine/it/controller/AiChatController.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ public class AiChatController {
6666
@Parameter(name = "ChatRequest", description = "入参对象")
6767
}, responses = {
6868
@ApiResponse(responseCode = "200", description = "返回信息",
69-
content = @Content(mediaType = "application/json", schema = @Schema())),
69+
content = @Content(mediaType = "application/json", schema = @Schema())),
7070
@ApiResponse(responseCode = "400", description = "请求失败")
7171
})
72-
@SystemControllerLog(description = "AI api")
72+
@SystemControllerLog(description = "AI chat")
7373
@PostMapping("/ai/chat")
7474
public ResponseEntity<?> aiChat(@RequestBody ChatRequest request) {
7575
try {
@@ -99,12 +99,12 @@ public ResponseEntity<?> aiChat(@RequestBody ChatRequest request) {
9999
@Parameter(name = "ChatRequest", description = "入参对象")
100100
}, responses = {
101101
@ApiResponse(responseCode = "200", description = "返回信息",
102-
content = @Content(mediaType = "application/json", schema = @Schema())),
102+
content = @Content(mediaType = "application/json", schema = @Schema())),
103103
@ApiResponse(responseCode = "400", description = "请求失败")
104104
})
105-
@SystemControllerLog(description = "AI api v1")
105+
@SystemControllerLog(description = "AI completions")
106106
@PostMapping("/chat/completions")
107-
public ResponseEntity<?> chat(@RequestBody ChatRequest request,
107+
public ResponseEntity<?> completions(@RequestBody ChatRequest request,
108108
@RequestHeader("Authorization") String authorization) {
109109
if (authorization != null && authorization.startsWith("Bearer ")) {
110110
String token = authorization.replace("Bearer ", "");
@@ -133,11 +133,11 @@ public ResponseEntity<?> chat(@RequestBody ChatRequest request,
133133
* @return ai回答信息 result
134134
*/
135135
@Operation(summary = "搜索知识库", description = "搜索知识库",
136-
parameters = {
137-
@Parameter(name = "content", description = "入参对象")
138-
}, responses = {
136+
parameters = {
137+
@Parameter(name = "content", description = "入参对象")
138+
}, responses = {
139139
@ApiResponse(responseCode = "200", description = "返回信息",
140-
content = @Content(mediaType = "application/json", schema = @Schema())),
140+
content = @Content(mediaType = "application/json", schema = @Schema())),
141141
@ApiResponse(responseCode = "400", description = "请求失败")
142142
})
143143
@SystemControllerLog(description = "AI serarch api")

base/src/main/java/com/tinyengine/it/controller/ResourceController.java

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
package com.tinyengine.it.controller;
1414

1515
import com.tinyengine.it.common.base.Result;
16+
import com.tinyengine.it.common.context.LoginUserContext;
17+
import com.tinyengine.it.common.exception.ExceptionEnum;
1618
import com.tinyengine.it.common.log.SystemControllerLog;
1719
import com.tinyengine.it.common.utils.ImageThumbnailGenerator;
1820
import com.tinyengine.it.common.utils.Utils;
@@ -38,6 +40,7 @@
3840
import org.springframework.web.bind.annotation.RequestMapping;
3941
import org.springframework.web.bind.annotation.RequestParam;
4042
import org.springframework.web.bind.annotation.RestController;
43+
import org.springframework.web.multipart.MultipartFile;
4144

4245
import java.io.OutputStream;
4346
import java.net.URLEncoder;
@@ -61,6 +64,9 @@ public class ResourceController {
6164
@Autowired
6265
private ResourceService resourceService;
6366

67+
@Autowired
68+
private LoginUserContext loginUserContext;
69+
6470
/**
6571
* 查询表Resource信息
6672
*
@@ -69,7 +75,7 @@ public class ResourceController {
6975
@Operation(summary = "查询表Resource信息", description = "查询表Resource信息",
7076
responses = {
7177
@ApiResponse(responseCode = "200", description = "返回信息",
72-
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
78+
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
7379
@ApiResponse(responseCode = "400", description = "请求失败")
7480
})
7581
@SystemControllerLog(description = "查询表Resource信息")
@@ -90,7 +96,7 @@ public Result<List<Resource>> getAllResource() {
9096
@Parameter(name = "id", description = "Resource主键id")
9197
}, responses = {
9298
@ApiResponse(responseCode = "200", description = "返回信息",
93-
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
99+
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
94100
@ApiResponse(responseCode = "400", description = "请求失败")
95101
})
96102
@SystemControllerLog(description = "根据id查询表Resource信息")
@@ -110,7 +116,7 @@ public Result<Resource> getResourceById(@PathVariable Integer id) {
110116
@Parameter(name = "resourceGroupId", description = "ResourceGroup主键id")
111117
}, responses = {
112118
@ApiResponse(responseCode = "200", description = "返回信息",
113-
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
119+
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
114120
@ApiResponse(responseCode = "400", description = "请求失败")
115121
})
116122
@SystemControllerLog(description = "根据分组id和创建人查询表t_resource信息")
@@ -132,7 +138,7 @@ public Result<List<Resource>> getResourceByResourceGroupId(@PathVariable Integer
132138
@Parameter(name = "des", description = "描述")
133139
}, responses = {
134140
@ApiResponse(responseCode = "200", description = "返回信息",
135-
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
141+
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
136142
@ApiResponse(responseCode = "400", description = "请求失败")
137143
})
138144
@SystemControllerLog(description = "模糊查询表Resource信息列表")
@@ -154,7 +160,7 @@ public Result<List<Resource>> getResourceById(@RequestParam String name, @Reques
154160
@Parameter(name = "resource", description = "Resource入参对象")
155161
}, responses = {
156162
@ApiResponse(responseCode = "200", description = "返回信息",
157-
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
163+
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
158164
@ApiResponse(responseCode = "400", description = "请求失败")
159165
})
160166
@SystemControllerLog(description = "创建resource")
@@ -167,20 +173,35 @@ public Result<Resource> createResource(@Valid @RequestBody Resource resource) th
167173
/**
168174
* 上传图片
169175
*
170-
* @param resource the resource
176+
* @param file the file
171177
* @return Resource信息 result
172178
*/
173179
@Operation(summary = "上传图片", description = "上传图片",
174-
parameters = {
175-
@Parameter(name = "resource", description = "Resource入参对象")
176-
}, responses = {
180+
parameters = {
181+
@Parameter(name = "file", description = "图片")
182+
}, responses = {
177183
@ApiResponse(responseCode = "200", description = "返回信息",
178-
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
184+
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
179185
@ApiResponse(responseCode = "400", description = "请求失败")
180186
})
181187
@SystemControllerLog(description = "上传图片")
182-
@PostMapping("/resource/uoload")
183-
public Result<Resource> resourceUoload(@Valid @RequestBody Resource resource) throws Exception {
188+
@PostMapping("/resource/upload")
189+
public Result<Resource> resourceUpload(@RequestParam MultipartFile file) throws Exception {
190+
// 获取文件的原始名称
191+
String fileName = file.getOriginalFilename();
192+
if (file.isEmpty()) {
193+
return Result.failed(ExceptionEnum.CM009);
194+
}
195+
if(!ImageThumbnailGenerator.validateByImageIO(file)){
196+
return Result.failed(ExceptionEnum.CM325);
197+
}
198+
// 将文件转为 Base64
199+
String base64 = ImageThumbnailGenerator.convertToBase64(file);
200+
Resource resource = new Resource();
201+
resource.setName(fileName);
202+
resource.setResourceData(base64);
203+
resource.setAppId(loginUserContext.getAppId());
204+
resource.setCategory("image");
184205
Resource result = resourceService.resourceUpload(resource);
185206
return Result.success(result);
186207
}
@@ -196,7 +217,7 @@ public Result<Resource> resourceUoload(@Valid @RequestBody Resource resource) th
196217
@Parameter(name = "resources", description = "Resource入参对象")
197218
}, responses = {
198219
@ApiResponse(responseCode = "200", description = "返回信息",
199-
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
220+
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
200221
@ApiResponse(responseCode = "400", description = "请求失败")
201222
})
202223
@SystemControllerLog(description = "批量创建Resource")
@@ -218,7 +239,7 @@ public Result<List<Resource>> createResource(@Valid @RequestBody List<Resource>
218239
@Parameter(name = "id", description = "id"),
219240
@Parameter(name = "Resource", description = "入参对象")}, responses = {
220241
@ApiResponse(responseCode = "200", description = "返回信息",
221-
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
242+
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
222243
@ApiResponse(responseCode = "400", description = "请求失败")
223244
})
224245
@SystemControllerLog(description = "修改单个Resource信息")
@@ -258,7 +279,7 @@ public Result<Resource> deleteResource(@PathVariable Integer id) {
258279
parameters = {
259280
@Parameter(name = "id", description = "id")}, responses = {
260281
@ApiResponse(responseCode = "200", description = "返回信息",
261-
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
282+
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
262283
@ApiResponse(responseCode = "400", description = "请求失败")
263284
})
264285
@SystemControllerLog(description = "获取resource信息详情")
@@ -277,7 +298,7 @@ public Result<Resource> detail(@PathVariable Integer id) {
277298
parameters = {
278299
@Parameter(name = "data", description = "base64编码数据")}, responses = {
279300
@ApiResponse(responseCode = "200", description = "图片流数据",
280-
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
301+
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Resource.class))),
281302
@ApiResponse(responseCode = "400", description = "请求失败")
282303
})
283304
@SystemControllerLog(description = "获取资源")

0 commit comments

Comments
 (0)