mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-14 14:16:57 +08:00
上传仅使用minio
This commit is contained in:
parent
f46f319428
commit
26a26c52c2
7
pom.xml
7
pom.xml
@ -117,15 +117,10 @@
|
|||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>cn.xuyanwu</groupId>
|
|
||||||
<artifactId>spring-file-storage</artifactId>
|
|
||||||
<version>0.7.0</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.minio</groupId>
|
<groupId>io.minio</groupId>
|
||||||
<artifactId>minio</artifactId>
|
<artifactId>minio</artifactId>
|
||||||
<version>8.4.3</version>
|
<version>8.5.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
package xyz.playedu.api;
|
package xyz.playedu.api;
|
||||||
|
|
||||||
import cn.xuyanwu.spring.file.storage.EnableFileStorage;
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.scheduling.annotation.EnableAsync;
|
import org.springframework.scheduling.annotation.EnableAsync;
|
||||||
|
|
||||||
@EnableAsync
|
@EnableAsync
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableFileStorage
|
|
||||||
public class PlayeduApiApplication {
|
public class PlayeduApiApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -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;
|
|
||||||
|
|
||||||
}
|
|
39
src/main/java/xyz/playedu/api/config/MinioConfig.java
Normal file
39
src/main/java/xyz/playedu/api/config/MinioConfig.java
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -12,6 +12,8 @@ public class BackendConstant {
|
|||||||
public final static String[] COURSE_HOUR_TYPE_WHITELIST = {"VIDEO"};
|
public final static String[] COURSE_HOUR_TYPE_WHITELIST = {"VIDEO"};
|
||||||
public final static String[] COURSE_HOUR_TYPE_WHITELIST_TEXT = {"视频"};
|
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/";
|
public final static String UPLOAD_IMAGE_DIR = "images/";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package xyz.playedu.api.controller.backend;
|
package xyz.playedu.api.controller.backend;
|
||||||
|
|
||||||
import cn.xuyanwu.spring.file.storage.FileInfo;
|
import io.minio.MinioClient;
|
||||||
import cn.xuyanwu.spring.file.storage.FileStorageService;
|
import io.minio.PutObjectArgs;
|
||||||
import org.apache.commons.collections4.MapUtils;
|
import org.apache.commons.collections4.MapUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
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.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import xyz.playedu.api.bus.BackendBus;
|
import xyz.playedu.api.config.MinioConfig;
|
||||||
import xyz.playedu.api.config.FileStorageConfig;
|
|
||||||
import xyz.playedu.api.constant.BackendConstant;
|
import xyz.playedu.api.constant.BackendConstant;
|
||||||
import xyz.playedu.api.domain.Resource;
|
import xyz.playedu.api.domain.Resource;
|
||||||
import xyz.playedu.api.service.ResourceService;
|
import xyz.playedu.api.service.ResourceService;
|
||||||
import xyz.playedu.api.types.JsonResponse;
|
import xyz.playedu.api.types.JsonResponse;
|
||||||
import xyz.playedu.api.util.HelperUtil;
|
import xyz.playedu.api.util.HelperUtil;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
@ -28,43 +28,65 @@ import java.util.HashMap;
|
|||||||
@RequestMapping("/backend/v1/upload")
|
@RequestMapping("/backend/v1/upload")
|
||||||
public class UploadController {
|
public class UploadController {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private FileStorageService fileStorageService;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ResourceService resourceService;
|
private ResourceService resourceService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private FileStorageConfig fileStorageConfig;
|
private MinioConfig minioConfig;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MinioClient minioClient;
|
||||||
|
|
||||||
@PostMapping("/image")
|
@PostMapping("/image")
|
||||||
public JsonResponse image(@RequestParam HashMap<String, Object> params, MultipartFile file) {
|
public JsonResponse image(@RequestParam HashMap<String, Object> params, MultipartFile file) {
|
||||||
Integer categoryId = MapUtils.getInteger(params, "category_id", 0);
|
Integer categoryId = MapUtils.getInteger(params, "category_id", 0);
|
||||||
if (file == null || file.isEmpty()) {
|
if (file == null || file.isEmpty() || file.getOriginalFilename() == null) {
|
||||||
return JsonResponse.error("请上传文件");
|
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 contentType = file.getContentType();
|
||||||
String url = fileStorageConfig.getDomain() + fileStorageConfig.getBucket() + "/" + savePath;
|
if (contentType == null || !Arrays.asList(BackendConstant.UPLOAD_IMAGE_CONTENT_TYPE_WL).contains(contentType)) {
|
||||||
|
return JsonResponse.error("格式不支持");
|
||||||
|
}
|
||||||
|
|
||||||
Resource resource = new Resource();
|
String filename = file.getOriginalFilename();
|
||||||
resource.setCategoryId(categoryId);
|
String ext = HelperUtil.fileExt(filename);
|
||||||
resource.setName(fileInfo.getFilename());
|
if (!Arrays.asList(BackendConstant.UPLOAD_IMAGE_EXT_WL).contains(ext)) {
|
||||||
resource.setExtension(fileInfo.getExt());
|
return JsonResponse.error("格式不支持");
|
||||||
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);
|
|
||||||
|
|
||||||
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("系统错误");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -163,4 +163,9 @@ public class HelperUtil {
|
|||||||
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
|
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String fileExt(String filename) {
|
||||||
|
String[] array = filename.split("\\.");
|
||||||
|
return array[array.length - 1].toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -41,19 +41,14 @@ spring:
|
|||||||
shutdown:
|
shutdown:
|
||||||
await-termination: true
|
await-termination: true
|
||||||
thread-name-prefix: "playedu-default-thread"
|
thread-name-prefix: "playedu-default-thread"
|
||||||
# 文件存储配置
|
|
||||||
file-storage:
|
# Minio
|
||||||
default-platform: "playedu-minio"
|
minio:
|
||||||
thumbnail-suffix: ".min.png"
|
access-key: "username"
|
||||||
minio:
|
secret-key: "password"
|
||||||
- platform: "playedu-minio"
|
end-point: "http://127.0.0.1:9000"
|
||||||
enable-storage: true
|
bucket: "playedu"
|
||||||
access-key: "username"
|
domain: "http://127.0.0.1:9000/"
|
||||||
secret-key: "password"
|
|
||||||
end-point: "http://127.0.0.1:9000"
|
|
||||||
bucket-name: "playedu"
|
|
||||||
domain: "http://127.0.0.1:9000/"
|
|
||||||
base-path: ""
|
|
||||||
|
|
||||||
mybatis:
|
mybatis:
|
||||||
mapper-locations: classpath:mapper/*.xml
|
mapper-locations: classpath:mapper/*.xml
|
||||||
|
Loading…
x
Reference in New Issue
Block a user