diff --git a/pom.xml b/pom.xml index f51e4fd..a23ebec 100644 --- a/pom.xml +++ b/pom.xml @@ -117,15 +117,10 @@ compile - - cn.xuyanwu - spring-file-storage - 0.7.0 - io.minio minio - 8.4.3 + 8.5.2 diff --git a/src/main/java/xyz/playedu/api/PlayeduApiApplication.java b/src/main/java/xyz/playedu/api/PlayeduApiApplication.java index 23aaee0..a5524a3 100644 --- a/src/main/java/xyz/playedu/api/PlayeduApiApplication.java +++ b/src/main/java/xyz/playedu/api/PlayeduApiApplication.java @@ -1,13 +1,11 @@ package xyz.playedu.api; -import cn.xuyanwu.spring.file.storage.EnableFileStorage; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @EnableAsync @SpringBootApplication -@EnableFileStorage public class PlayeduApiApplication { public static void main(String[] args) { diff --git a/src/main/java/xyz/playedu/api/config/FileStorageConfig.java b/src/main/java/xyz/playedu/api/config/FileStorageConfig.java deleted file mode 100644 index cde9d6c..0000000 --- a/src/main/java/xyz/playedu/api/config/FileStorageConfig.java +++ /dev/null @@ -1,27 +0,0 @@ -package xyz.playedu.api.config; - -import lombok.Data; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Configuration; - -/** - * @Author 杭州白书科技有限公司 - * @create 2023/2/28 16:38 - */ -@Data -@Configuration -public class FileStorageConfig { - - @Value("${spring.file-storage.default-platform}") - private String defaultPlatform; - - @Value("${spring.file-storage.minio[0].domain}") - private String domain; - - @Value("${spring.file-storage.minio[0].bucket-name}") - private String bucket; - - @Value("${spring.file-storage.minio[0].base-path}") - private String basePath; - -} diff --git a/src/main/java/xyz/playedu/api/config/MinioConfig.java b/src/main/java/xyz/playedu/api/config/MinioConfig.java new file mode 100644 index 0000000..a70e609 --- /dev/null +++ b/src/main/java/xyz/playedu/api/config/MinioConfig.java @@ -0,0 +1,39 @@ +package xyz.playedu.api.config; + +import io.minio.MinioClient; +import lombok.Data; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @Author 杭州白书科技有限公司 + * @create 2023/2/28 16:38 + */ +@Data +@Configuration +public class MinioConfig { + + @Value("${minio.domain}") + private String domain; + + @Value("${minio.bucket}") + private String bucket; + + @Value("${minio.access-key}") + private String accessKey; + + @Value("${minio.secret-key}") + private String secretKey; + + @Value("${minio.end-point}") + private String endPoint; + + @Bean + public MinioClient getMinioClient() { + return MinioClient.builder() + .endpoint(this.endPoint) + .credentials(this.accessKey, this.secretKey) + .build(); + } +} diff --git a/src/main/java/xyz/playedu/api/constant/BackendConstant.java b/src/main/java/xyz/playedu/api/constant/BackendConstant.java index 05efdc3..a24dafe 100644 --- a/src/main/java/xyz/playedu/api/constant/BackendConstant.java +++ b/src/main/java/xyz/playedu/api/constant/BackendConstant.java @@ -12,6 +12,8 @@ public class BackendConstant { public final static String[] COURSE_HOUR_TYPE_WHITELIST = {"VIDEO"}; public final static String[] COURSE_HOUR_TYPE_WHITELIST_TEXT = {"视频"}; + public final static String[] UPLOAD_IMAGE_EXT_WL = {"png", "jpg", "jpeg", "gif"}; + public final static String[] UPLOAD_IMAGE_CONTENT_TYPE_WL = {"image/png", "image/jpg", "image/jpeg", "image/gif"}; public final static String UPLOAD_IMAGE_DIR = "images/"; } 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 c01854d..85d9cba 100644 --- a/src/main/java/xyz/playedu/api/controller/backend/UploadController.java +++ b/src/main/java/xyz/playedu/api/controller/backend/UploadController.java @@ -1,7 +1,7 @@ package xyz.playedu.api.controller.backend; -import cn.xuyanwu.spring.file.storage.FileInfo; -import cn.xuyanwu.spring.file.storage.FileStorageService; +import io.minio.MinioClient; +import io.minio.PutObjectArgs; import org.apache.commons.collections4.MapUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; @@ -9,14 +9,14 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; -import xyz.playedu.api.bus.BackendBus; -import xyz.playedu.api.config.FileStorageConfig; +import xyz.playedu.api.config.MinioConfig; import xyz.playedu.api.constant.BackendConstant; import xyz.playedu.api.domain.Resource; import xyz.playedu.api.service.ResourceService; import xyz.playedu.api.types.JsonResponse; import xyz.playedu.api.util.HelperUtil; +import java.util.Arrays; import java.util.Date; import java.util.HashMap; @@ -28,43 +28,65 @@ import java.util.HashMap; @RequestMapping("/backend/v1/upload") public class UploadController { - @Autowired - private FileStorageService fileStorageService; - @Autowired private ResourceService resourceService; @Autowired - private FileStorageConfig fileStorageConfig; + private MinioConfig minioConfig; + + @Autowired + private MinioClient minioClient; @PostMapping("/image") public JsonResponse image(@RequestParam HashMap params, MultipartFile file) { Integer categoryId = MapUtils.getInteger(params, "category_id", 0); - if (file == null || file.isEmpty()) { + if (file == null || file.isEmpty() || file.getOriginalFilename() == null) { return JsonResponse.error("请上传文件"); } - FileInfo fileInfo = fileStorageService - .of(file) - .setPath(fileStorageConfig.getBasePath() + BackendConstant.UPLOAD_IMAGE_DIR) - .setObjectId(HelperUtil.randomString(32)) - .upload(); - String savePath = fileInfo.getPath() + fileInfo.getFilename(); - String url = fileStorageConfig.getDomain() + fileStorageConfig.getBucket() + "/" + savePath; + String contentType = file.getContentType(); + if (contentType == null || !Arrays.asList(BackendConstant.UPLOAD_IMAGE_CONTENT_TYPE_WL).contains(contentType)) { + return JsonResponse.error("格式不支持"); + } - Resource resource = new Resource(); - resource.setCategoryId(categoryId); - resource.setName(fileInfo.getFilename()); - resource.setExtension(fileInfo.getExt()); - resource.setSize(fileInfo.getSize()); - resource.setDisk(fileStorageConfig.getDefaultPlatform()); - resource.setFileId(fileInfo.getObjectId()); - resource.setPath(savePath); - resource.setUrl(url); - resource.setCreatedAt(new Date()); - resourceService.save(resource); + String filename = file.getOriginalFilename(); + String ext = HelperUtil.fileExt(filename); + if (!Arrays.asList(BackendConstant.UPLOAD_IMAGE_EXT_WL).contains(ext)) { + return JsonResponse.error("格式不支持"); + } - return JsonResponse.data(resource); + String oldFilename = filename.replaceAll("." + ext, ""); + String newFilename = HelperUtil.randomString(32) + "." + ext; + String savePath = BackendConstant.UPLOAD_IMAGE_DIR + newFilename; + + try { + PutObjectArgs objectArgs = PutObjectArgs.builder() + .bucket(minioConfig.getBucket()) + .object(savePath) + .stream(file.getInputStream(), file.getSize(), -1) + .contentType(contentType) + .build(); + + minioClient.putObject(objectArgs); + + String url = minioConfig.getDomain() + minioConfig.getBucket() + "/" + savePath; + + Resource resource = new Resource(); + resource.setCategoryId(categoryId); + resource.setName(oldFilename); + resource.setExtension(ext); + resource.setSize(file.getSize()); + resource.setDisk("minio"); + resource.setFileId(""); + resource.setPath(savePath); + resource.setUrl(url); + resource.setCreatedAt(new Date()); + resourceService.save(resource); + + return JsonResponse.data(resource); + } catch (Exception e) { + return JsonResponse.error("系统错误"); + } } } diff --git a/src/main/java/xyz/playedu/api/util/HelperUtil.java b/src/main/java/xyz/playedu/api/util/HelperUtil.java index c90caca..32340d5 100644 --- a/src/main/java/xyz/playedu/api/util/HelperUtil.java +++ b/src/main/java/xyz/playedu/api/util/HelperUtil.java @@ -163,4 +163,9 @@ public class HelperUtil { return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj); } + public static String fileExt(String filename) { + String[] array = filename.split("\\."); + return array[array.length - 1].toLowerCase(); + } + } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 44b19ba..82d931b 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -41,19 +41,14 @@ spring: shutdown: await-termination: true thread-name-prefix: "playedu-default-thread" - # 文件存储配置 - file-storage: - default-platform: "playedu-minio" - thumbnail-suffix: ".min.png" - minio: - - platform: "playedu-minio" - enable-storage: true - access-key: "username" - secret-key: "password" - end-point: "http://127.0.0.1:9000" - bucket-name: "playedu" - domain: "http://127.0.0.1:9000/" - base-path: "" + +# Minio +minio: + access-key: "username" + secret-key: "password" + end-point: "http://127.0.0.1:9000" + bucket: "playedu" + domain: "http://127.0.0.1:9000/" mybatis: mapper-locations: classpath:mapper/*.xml