mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-12-16 16:14:19 +08:00
上传仅使用minio
This commit is contained in:
@@ -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<String, Object> 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("系统错误");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user