mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-07-27 04:19:31 +08:00
增加minio的preSign接口
This commit is contained in:
parent
0037b9e9ae
commit
9792366256
@ -1,17 +1,32 @@
|
||||
package xyz.playedu.api.constant;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class BackendConstant {
|
||||
public final static String SUPER_ADMIN_ROLE = "super-role";
|
||||
|
||||
public final static String[] UN_AUTH_URI_WHITELIST = {"/backend/v1/system/image-captcha", "/backend/v1/auth/login",};
|
||||
|
||||
public final static String[] RESOURCE_TYPE_WHITELIST = {"IMAGE", "PDF", "VIDEO", "WORD", "PPT"};
|
||||
public final static String[] RESOURCE_EXT_WHITELIST = {"IMAGE", "PDF", "VIDEO", "WORD", "PPT"};
|
||||
public final static HashMap<String, String> RESOURCE_EXT_2_CONTENT_TYPE = new HashMap<>() {{
|
||||
put("png", "image/png");
|
||||
put("jpg", "image/jpg");
|
||||
put("jpeg", "image/jpeg");
|
||||
put("gif", "image/gif");
|
||||
put("pdf", "application/pdf");
|
||||
put("mp4", "video/mp4");
|
||||
put("doc", "application/msword");
|
||||
put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
||||
put("ppt", "application/vnd.ms-powerpoint");
|
||||
put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
|
||||
}};
|
||||
|
||||
public final static String[] RESOURCE_DISK_WHITELIST = {"MINIO"};
|
||||
|
||||
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/";
|
||||
|
@ -48,14 +48,14 @@ public class ResourceCategoryController {
|
||||
@GetMapping("/create")
|
||||
public JsonResponse create() {
|
||||
HashMap<String, Object> data = new HashMap<>();
|
||||
data.put("types", BackendConstant.RESOURCE_TYPE_WHITELIST);
|
||||
data.put("types", BackendConstant.RESOURCE_EXT_WHITELIST);
|
||||
return JsonResponse.data(data);
|
||||
}
|
||||
|
||||
@BackendPermissionMiddleware(slug = BPermissionConstant.RESOURCE_CATEGORY)
|
||||
@PostMapping("/create")
|
||||
public JsonResponse store(@RequestBody @Validated ResourceCategoryRequest req) {
|
||||
if (!Arrays.asList(BackendConstant.RESOURCE_TYPE_WHITELIST).contains(req.getType())) {
|
||||
if (!Arrays.asList(BackendConstant.RESOURCE_EXT_WHITELIST).contains(req.getType())) {
|
||||
return JsonResponse.error("资源类型不支持");
|
||||
}
|
||||
resourceCategoryService.create(req.getType(), req.getSort(), req.getName());
|
||||
@ -72,7 +72,7 @@ public class ResourceCategoryController {
|
||||
@BackendPermissionMiddleware(slug = BPermissionConstant.RESOURCE_CATEGORY)
|
||||
@PutMapping("/{id}")
|
||||
public JsonResponse update(@PathVariable(name = "id") Integer id, @RequestBody @Validated ResourceCategoryRequest req) throws NotFoundException {
|
||||
if (!Arrays.asList(BackendConstant.RESOURCE_TYPE_WHITELIST).contains(req.getType())) {
|
||||
if (!Arrays.asList(BackendConstant.RESOURCE_EXT_WHITELIST).contains(req.getType())) {
|
||||
return JsonResponse.error("资源类型不支持");
|
||||
}
|
||||
ResourceCategory category = resourceCategoryService.findOrFail(id);
|
||||
|
@ -1,7 +1,9 @@
|
||||
package xyz.playedu.api.controller.backend;
|
||||
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.PostPolicy;
|
||||
import io.minio.PutObjectArgs;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@ -17,14 +19,17 @@ import xyz.playedu.api.service.ResourceService;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
import xyz.playedu.api.util.HelperUtil;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/2/28 16:26
|
||||
*/
|
||||
@RestController
|
||||
@Slf4j
|
||||
@RequestMapping("/backend/v1/upload")
|
||||
public class UploadController {
|
||||
|
||||
@ -85,4 +90,27 @@ public class UploadController {
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/minio-token")
|
||||
public JsonResponse minioToken(@RequestParam HashMap<String, Object> params) {
|
||||
String extension = MapUtils.getString(params, "extension");
|
||||
if (extension == null || extension.isEmpty()) {
|
||||
return JsonResponse.error("extension参数为空");
|
||||
}
|
||||
String contentType = BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(extension.toLowerCase());
|
||||
if (contentType == null) {
|
||||
return JsonResponse.error("该格式不支持上传");
|
||||
}
|
||||
|
||||
try {
|
||||
PostPolicy postPolicy = new PostPolicy(minioConfig.getBucket(), ZonedDateTime.now().plusDays(1));
|
||||
postPolicy.addStartsWithCondition("Content-Type", contentType);
|
||||
postPolicy.addEqualsCondition("key", HelperUtil.randomString(32));
|
||||
Map<String, String> data = minioClient.getPresignedPostFormData(postPolicy);
|
||||
return JsonResponse.data(data);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
return JsonResponse.error("系统错误");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user