mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-12-17 17:19:42 +08:00
完善minio上传
This commit is contained in:
@@ -116,11 +116,20 @@ public class ResourceController {
|
||||
return JsonResponse.error("duration参数必须存在且大于0");
|
||||
}
|
||||
if (poster == null || poster.trim().length() == 0) {
|
||||
return JsonResponse.error("视频封面为空");
|
||||
return JsonResponse.error("poster参数值不能为空");
|
||||
}
|
||||
}
|
||||
|
||||
Resource res = resourceService.create(req.getCategoryId(), type, req.getName(), extension, req.getSize(), disk, req.getFileId(), req.getPath(), req.getUrl());
|
||||
Resource res = resourceService.create(
|
||||
req.getCategoryId(),
|
||||
type, req.getName(),
|
||||
extension,
|
||||
req.getSize(),
|
||||
disk,
|
||||
req.getFileId(),
|
||||
req.getPath(),
|
||||
req.getUrl()
|
||||
);
|
||||
|
||||
if (isVideoType) {
|
||||
resourceVideoService.create(res.getId(), duration, poster);
|
||||
|
||||
@@ -3,17 +3,20 @@ package xyz.playedu.api.controller.backend;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import xyz.playedu.api.constant.BackendConstant;
|
||||
import xyz.playedu.api.domain.Resource;
|
||||
import xyz.playedu.api.exception.ServiceException;
|
||||
import xyz.playedu.api.request.backend.UploadVideoMergeRequest;
|
||||
import xyz.playedu.api.service.MinioService;
|
||||
import xyz.playedu.api.service.ResourceCategoryService;
|
||||
import xyz.playedu.api.service.ResourceService;
|
||||
import xyz.playedu.api.service.UploadService;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
import xyz.playedu.api.util.HelperUtil;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
@@ -24,74 +27,49 @@ import java.util.HashMap;
|
||||
@Slf4j
|
||||
@RequestMapping("/backend/v1/upload")
|
||||
public class UploadController {
|
||||
|
||||
@Autowired
|
||||
private ResourceService resourceService;
|
||||
|
||||
@Autowired
|
||||
private ResourceCategoryService resourceCategoryService;
|
||||
|
||||
@Autowired
|
||||
private MinioService minioService;
|
||||
|
||||
@PostMapping("/image")
|
||||
public JsonResponse image(@RequestParam HashMap<String, Object> params, MultipartFile file) {
|
||||
if (file == null || file.isEmpty() || file.getOriginalFilename() == null) {
|
||||
return JsonResponse.error("请上传文件");
|
||||
}
|
||||
@Autowired
|
||||
private UploadService uploadService;
|
||||
|
||||
String contentType = file.getContentType();
|
||||
if (contentType == null || !Arrays.asList(BackendConstant.UPLOAD_IMAGE_CONTENT_TYPE_WL).contains(contentType)) {
|
||||
return JsonResponse.error("格式不支持");
|
||||
}
|
||||
@Autowired
|
||||
private ResourceService resourceService;
|
||||
|
||||
@PostMapping("/file")
|
||||
public JsonResponse image(@RequestParam HashMap<String, Object> params, MultipartFile file) throws ServiceException {
|
||||
Integer cid = MapUtils.getInteger(params, "category_id");
|
||||
if (cid != null && !cid.equals(0) && resourceCategoryService.getById(cid) == null) {
|
||||
return JsonResponse.error("分类不存在");
|
||||
}
|
||||
|
||||
String filename = file.getOriginalFilename();
|
||||
String ext = HelperUtil.fileExt(filename);
|
||||
if (!Arrays.asList(BackendConstant.UPLOAD_IMAGE_EXT_WL).contains(ext)) {
|
||||
return JsonResponse.error("格式不支持");
|
||||
}
|
||||
|
||||
String oldFilename = filename.replaceAll("." + ext, "");
|
||||
String newFilename = HelperUtil.randomString(32) + "." + ext;
|
||||
String savePath = BackendConstant.UPLOAD_IMAGE_DIR + newFilename;
|
||||
|
||||
// 保存文件
|
||||
String url = minioService.saveFile(file, savePath, contentType);
|
||||
// 上传记录
|
||||
Resource res = resourceService.create(cid, BackendConstant.RESOURCE_TYPE_IMAGE, oldFilename, ext, file.getSize(), "minio", "", savePath, url);
|
||||
|
||||
Resource res = uploadService.storeMinio(file, cid);
|
||||
return JsonResponse.data(res);
|
||||
}
|
||||
|
||||
@GetMapping("/minio-upload-id")
|
||||
@GetMapping("/minio/upload-id")
|
||||
public JsonResponse minioUploadId(@RequestParam HashMap<String, Object> params) {
|
||||
String extension = MapUtils.getString(params, "extension");
|
||||
if (extension == null || extension.trim().length() == 0) {
|
||||
return JsonResponse.error("extension参数为空");
|
||||
}
|
||||
String contentType = BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(extension.toLowerCase());
|
||||
if (contentType == null) {
|
||||
return JsonResponse.error("该格式不支持上传");
|
||||
String type = BackendConstant.RESOURCE_EXT_2_TYPE.get(extension.toLowerCase());
|
||||
if (type == null) {
|
||||
return JsonResponse.error("该格式文件不支持上传");
|
||||
}
|
||||
|
||||
String filename = HelperUtil.randomString(32) + "." + extension;//文件名
|
||||
String path = BackendConstant.UPLOAD_VIDEO_DIR + filename;//存储路径
|
||||
String path = BackendConstant.RESOURCE_TYPE_2_DIR.get(type) + filename;//存储路径
|
||||
String uploadId = minioService.uploadId(path);
|
||||
|
||||
HashMap<String, String> data = new HashMap<>();
|
||||
data.put("resource_type", BackendConstant.RESOURCE_EXT_2_TYPE.get(extension.toLowerCase()));
|
||||
data.put("resource_type", type);
|
||||
data.put("upload_id", uploadId);
|
||||
data.put("filename", path);
|
||||
|
||||
return JsonResponse.data(data);
|
||||
}
|
||||
|
||||
@GetMapping("/minio-pre-sign-url")
|
||||
@GetMapping("/minio/pre-sign-url")
|
||||
public JsonResponse minioPreSignUrl(@RequestParam HashMap<String, Object> params) {
|
||||
String uploadId = MapUtils.getString(params, "upload_id");
|
||||
Integer partNumber = MapUtils.getInteger(params, "part_number");
|
||||
@@ -105,7 +83,46 @@ public class UploadController {
|
||||
return JsonResponse.data(data);
|
||||
}
|
||||
|
||||
@GetMapping("/minio-merge")
|
||||
@PostMapping("/minio/merge-video")
|
||||
public JsonResponse minioMergeVideo(@RequestBody @Validated UploadVideoMergeRequest req) throws ServiceException {
|
||||
Integer cid = req.getCategoryId();
|
||||
String type = BackendConstant.RESOURCE_EXT_2_TYPE.get(req.getExtension());
|
||||
if (type == null) {
|
||||
return JsonResponse.error("当前格式不支持上传");
|
||||
}
|
||||
if (cid != null && resourceCategoryService.find(cid, type) == null) {
|
||||
return JsonResponse.error("资源分类不存在");
|
||||
}
|
||||
|
||||
// 合并视频文件
|
||||
String url = minioService.merge(req.getFilename(), req.getUploadId());
|
||||
|
||||
// 视频素材保存
|
||||
Resource videoResource = resourceService.create(
|
||||
cid,
|
||||
type,
|
||||
req.getOriginalFilename(),
|
||||
req.getExtension(),
|
||||
req.getSize(),
|
||||
BackendConstant.STORAGE_DRIVER_MINIO,
|
||||
"",
|
||||
req.getFilename(),
|
||||
url
|
||||
);
|
||||
// 视频封面素材保存
|
||||
Resource posterResource = uploadService.storeBase64Image(req.getPoster(), 0);
|
||||
// 视频的封面素材改为[隐藏 && 属于视频的子素材]
|
||||
resourceService.changeParentId(posterResource.getId(), videoResource.getId());
|
||||
// 视频信息
|
||||
resourceService.storeResourceVideo(videoResource.getId(), req.getDuration(), posterResource.getUrl());
|
||||
|
||||
HashMap<String, Object> data = new HashMap<>();
|
||||
data.put("url", url);
|
||||
|
||||
return JsonResponse.data(data);
|
||||
}
|
||||
|
||||
@GetMapping("/minio/merge")
|
||||
public JsonResponse minioMerge(@RequestParam HashMap<String, Object> params) {
|
||||
String filename = MapUtils.getString(params, "filename");
|
||||
String uploadId = MapUtils.getString(params, "upload_id");
|
||||
|
||||
Reference in New Issue
Block a user