minio-token返回resource_type

This commit is contained in:
none 2023-03-02 15:03:19 +08:00
parent 9792366256
commit c126e47823
3 changed files with 20 additions and 9 deletions

View File

@ -7,7 +7,7 @@ public class BackendConstant {
public final static String[] UN_AUTH_URI_WHITELIST = {"/backend/v1/system/image-captcha", "/backend/v1/auth/login",};
public final static String[] RESOURCE_EXT_WHITELIST = {"IMAGE", "PDF", "VIDEO", "WORD", "PPT"};
public final static String[] RESOURCE_TYPE_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");
@ -20,6 +20,18 @@ public class BackendConstant {
put("ppt", "application/vnd.ms-powerpoint");
put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
}};
public final static HashMap<String, String> RESOURCE_EXT_2_TYPE = new HashMap<>() {{
put("png", "IMAGE");
put("jpg", "IMAGE");
put("jpeg", "IMAGE");
put("gif", "IMAGE");
put("pdf", "PDF");
put("mp4", "VIDEO");
put("doc", "WORD");
put("docx", "WORD");
put("ppt", "PPT");
put("pptx", "PPT");
}};
public final static String[] RESOURCE_DISK_WHITELIST = {"MINIO"};

View File

@ -48,14 +48,14 @@ public class ResourceCategoryController {
@GetMapping("/create")
public JsonResponse create() {
HashMap<String, Object> data = new HashMap<>();
data.put("types", BackendConstant.RESOURCE_EXT_WHITELIST);
data.put("types", BackendConstant.RESOURCE_TYPE_WHITELIST);
return JsonResponse.data(data);
}
@BackendPermissionMiddleware(slug = BPermissionConstant.RESOURCE_CATEGORY)
@PostMapping("/create")
public JsonResponse store(@RequestBody @Validated ResourceCategoryRequest req) {
if (!Arrays.asList(BackendConstant.RESOURCE_EXT_WHITELIST).contains(req.getType())) {
if (!Arrays.asList(BackendConstant.RESOURCE_TYPE_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_EXT_WHITELIST).contains(req.getType())) {
if (!Arrays.asList(BackendConstant.RESOURCE_TYPE_WHITELIST).contains(req.getType())) {
return JsonResponse.error("资源类型不支持");
}
ResourceCategory category = resourceCategoryService.findOrFail(id);

View File

@ -6,10 +6,7 @@ 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;
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.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xyz.playedu.api.config.MinioConfig;
import xyz.playedu.api.constant.BackendConstant;
@ -90,7 +87,7 @@ public class UploadController {
}
}
@PostMapping("/minio-token")
@GetMapping("/minio-token")
public JsonResponse minioToken(@RequestParam HashMap<String, Object> params) {
String extension = MapUtils.getString(params, "extension");
if (extension == null || extension.isEmpty()) {
@ -100,12 +97,14 @@ public class UploadController {
if (contentType == null) {
return JsonResponse.error("该格式不支持上传");
}
String resourceType = BackendConstant.RESOURCE_EXT_2_TYPE.get(extension.toLowerCase());
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);
data.put("resource_type", resourceType);
return JsonResponse.data(data);
} catch (Exception e) {
log.error(e.getMessage());