From 4a012afaea8bb3eba3f4270d7a535bac7304ad22 Mon Sep 17 00:00:00 2001 From: none Date: Thu, 9 Mar 2023 10:14:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9B=BE=E7=89=87=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E7=9A=84=E5=AE=89=E5=85=A8=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/backend/UploadController.java | 4 ++-- .../api/service/impl/UploadServiceImpl.java | 21 ++++++------------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/main/java/xyz/playedu/api/controller/backend/UploadController.java b/src/main/java/xyz/playedu/api/controller/backend/UploadController.java index 5064665..afb428a 100644 --- a/src/main/java/xyz/playedu/api/controller/backend/UploadController.java +++ b/src/main/java/xyz/playedu/api/controller/backend/UploadController.java @@ -36,8 +36,8 @@ public class UploadController { @Autowired private ResourceService resourceService; - @PostMapping("/file") - public JsonResponse file(@RequestParam HashMap params, MultipartFile file) throws ServiceException { + @PostMapping("/minio") + public JsonResponse uploadMinio(@RequestParam HashMap params, MultipartFile file) throws ServiceException { String categoryIds = MapUtils.getString(params, "category_ids"); Resource res = uploadService.storeMinio(file, categoryIds); return JsonResponse.data(res); diff --git a/src/main/java/xyz/playedu/api/service/impl/UploadServiceImpl.java b/src/main/java/xyz/playedu/api/service/impl/UploadServiceImpl.java index caf9d8a..3c5bb27 100644 --- a/src/main/java/xyz/playedu/api/service/impl/UploadServiceImpl.java +++ b/src/main/java/xyz/playedu/api/service/impl/UploadServiceImpl.java @@ -41,14 +41,7 @@ public class UploadServiceImpl implements UploadService { String ext = HelperUtil.fileExt(filename); String type = BackendConstant.RESOURCE_EXT_2_TYPE.get(ext); if (type == null) { - throw new ServiceException("格式不支持"); - } - - // content-type校验 - String contentType = file.getContentType(); - String safeContentType = BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(ext); - if (safeContentType == null || !safeContentType.equals(contentType)) { - throw new ServiceException("格式不支持"); + throw new ServiceException("当前资源扩展不支持上传"); } // 上传原文件的文件名 @@ -58,7 +51,7 @@ public class UploadServiceImpl implements UploadService { String savePath = BackendConstant.RESOURCE_TYPE_2_DIR.get(type) + newFilename; // 保存文件 - String url = minioService.saveFile(file, savePath, contentType); + String url = minioService.saveFile(file, savePath, BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(ext)); // 上传记录 return resourceService.create(categoryIds, type, oFilename, ext, file.getSize(), BackendConstant.STORAGE_DRIVER_MINIO, "", savePath, url); } @@ -73,11 +66,9 @@ public class UploadServiceImpl implements UploadService { String ext = contentType.replaceAll("image/", ""); // 通过文件格式解析资源类型 String type = BackendConstant.RESOURCE_EXT_2_TYPE.get(ext); - // 通过资源类型获取安全的content-type - String safeContentType = BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(ext); - // 资源类型必须存在 && 安全的 content-type 必须存在 且与解析出来的 content-type 一致 - if (type == null || safeContentType == null || !safeContentType.equals(contentType)) { - throw new ServiceException("格式不支持"); + // 资源类型必须存在 + if (type == null) { + throw new ServiceException("资源类型不支持"); } byte[] binary = Base64Util.decode(base64Rows[1]); @@ -85,7 +76,7 @@ public class UploadServiceImpl implements UploadService { String savePath = BackendConstant.RESOURCE_TYPE_2_DIR.get(type) + filename; // 保存文件 - String url = minioService.saveBytes(binary, savePath, contentType); + String url = minioService.saveBytes(binary, savePath, BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(ext)); // 上传记录 return resourceService.create(categoryIds, type, filename, ext, (long) binary.length, BackendConstant.STORAGE_DRIVER_MINIO, "", savePath, url); }