Skip to content

Commit 6329922

Browse files
authored
fix: modify resource upload and download (opentiny#265)
1 parent 15011b4 commit 6329922

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ public Result<Resource> resourceUpload(@RequestParam MultipartFile file) throws
202202
resource.setName(fileName);
203203
resource.setResourceData(base64);
204204
resource.setAppId(loginUserContext.getAppId());
205-
resource.setCategory("image");
206205
Resource result = resourceService.resourceUpload(resource);
207206
return Result.success(result);
208207
}
@@ -308,7 +307,7 @@ public Result<Resource> detail(@PathVariable Integer id) {
308307
@SystemControllerLog(description = "获取资源")
309308
@GetMapping("/resource/download")
310309
public void getResource(@RequestParam String name, @RequestParam boolean isResource,
311-
HttpServletResponse response) throws Exception {
310+
@RequestParam(required = false) boolean isChat, HttpServletResponse response) throws Exception {
312311
Resource resource = resourceService.queryResourceByName(name);
313312
if(resource == null) {
314313
throw new ServiceException(ExceptionEnum.CM009.getResultCode(),ExceptionEnum.CM009.getResultMsg());
@@ -326,11 +325,12 @@ public void getResource(@RequestParam String name, @RequestParam boolean isResou
326325
response.setContentType(detectedType);
327326

328327
// 只使用 filename* 格式,避免中文字符直接出现在header中
329-
response.setHeader("Content-Disposition",
330-
"inline; filename*=UTF-8''" + encodedFileName);
328+
response.setHeader("Content-Disposition", "inline; filename*=UTF-8''" + encodedFileName);
329+
if(isChat){
330+
response.setHeader("Content-Disposition", "attachment ; filename*=UTF-8''" + encodedFileName);
331+
}
331332
try (OutputStream out = response.getOutputStream()) {
332333
out.write(imageBytes);
333334
}
334335
}
335-
336336
}

base/src/main/java/com/tinyengine/it/service/material/impl/ResourceServiceImpl.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,19 @@ public Resource createResource(Resource resource) throws Exception {
174174
@Override
175175
@SystemServiceLog(description = "图片上传")
176176
public Resource resourceUpload(Resource resource) {
177-
String imageName = Instant.now().toEpochMilli()+resource.getName();
177+
String imageName = Instant.now().toEpochMilli() + resource.getName();
178178
resource.setName(imageName);
179179
String resourceData = resource.getResourceData();
180180
String tinyEngineUrl = System.getenv("TINY_ENGINE_URL");
181-
181+
String encodedName = URLEncoder.encode(imageName, StandardCharsets.UTF_8);
182+
String resourceUrl = tinyEngineUrl + "?name=" + encodedName + "&isResource=" + true;
183+
String thumbnailUrl = tinyEngineUrl + "?name=" + encodedName + "&isResource=" + false;
184+
if (resource.getCategory() == null) {
185+
resourceUrl = tinyEngineUrl + "?name=" + encodedName + "&isResource=" + true + "&isChat=" + true;
186+
thumbnailUrl = tinyEngineUrl + "?name=" + encodedName + "&isResource=" + false + "&isChat=" + true;
187+
resource.setCategory("image");
188+
}
182189
if (!StringUtils.isEmpty(resourceData)) {
183-
String encodedName = URLEncoder.encode(imageName, StandardCharsets.UTF_8);
184-
String resourceUrl = tinyEngineUrl + "?name=" + encodedName + "&isResource=" + true;
185-
String thumbnailUrl = tinyEngineUrl + "?name=" + encodedName + "&isResource=" + false;
186190
resource.setResourceUrl(resourceUrl);
187191
resource.setThumbnailUrl(thumbnailUrl);
188192
resource.setThumbnailData(ImageThumbnailGenerator.createThumbnail(resource.getResourceData(), 200, 200));

0 commit comments

Comments
 (0)