mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-07-25 02:39:31 +08:00
资源操作抽到service
This commit is contained in:
parent
2ea4166fab
commit
65ec26c545
@ -1,20 +1,18 @@
|
||||
package xyz.playedu.api.controller.backend;
|
||||
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.RemoveObjectArgs;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xyz.playedu.api.config.MinioConfig;
|
||||
import xyz.playedu.api.constant.BackendConstant;
|
||||
import xyz.playedu.api.domain.Resource;
|
||||
import xyz.playedu.api.domain.ResourceCategory;
|
||||
import xyz.playedu.api.domain.ResourceVideo;
|
||||
import xyz.playedu.api.exception.NotFoundException;
|
||||
import xyz.playedu.api.request.backend.ResourceRequest;
|
||||
import xyz.playedu.api.service.MinioService;
|
||||
import xyz.playedu.api.service.ResourceCategoryService;
|
||||
import xyz.playedu.api.service.ResourceService;
|
||||
import xyz.playedu.api.service.ResourceVideoService;
|
||||
@ -44,10 +42,7 @@ public class ResourceController {
|
||||
private ResourceCategoryService categoryService;
|
||||
|
||||
@Autowired
|
||||
private MinioClient minioClient;
|
||||
|
||||
@Autowired
|
||||
private MinioConfig minioConfig;
|
||||
private MinioService minioService;
|
||||
|
||||
@GetMapping("/index")
|
||||
public JsonResponse index(@RequestParam HashMap<String, Object> params) {
|
||||
@ -134,17 +129,15 @@ public class ResourceController {
|
||||
@Transactional
|
||||
public JsonResponse destroy(@PathVariable(name = "id") Integer id) throws NotFoundException {
|
||||
Resource resource = resourceService.findOrFail(id);
|
||||
try {
|
||||
minioClient.removeObject(RemoveObjectArgs.builder().bucket(minioConfig.getBucket()).object(resource.getPath()).build());
|
||||
if (resource.getType().equals(BackendConstant.RESOURCE_TYPE_VIDEO)) {
|
||||
resourceVideoService.removeByRid(resource.getId());
|
||||
}
|
||||
resourceService.removeById(resource.getId());
|
||||
return JsonResponse.success();
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
return JsonResponse.error("系统错误");
|
||||
// 删除文件
|
||||
minioService.removeByPath(resource.getPath());
|
||||
// 如果是视频资源文件则删除对应的时长关联记录
|
||||
if (resource.getType().equals(BackendConstant.RESOURCE_TYPE_VIDEO)) {
|
||||
resourceVideoService.removeByRid(resource.getId());
|
||||
}
|
||||
// 删除资源记录
|
||||
resourceService.removeById(resource.getId());
|
||||
return JsonResponse.success();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,26 +1,20 @@
|
||||
package xyz.playedu.api.controller.backend;
|
||||
|
||||
import com.google.common.collect.Multimap;
|
||||
import io.minio.*;
|
||||
import io.minio.http.Method;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.checkerframework.checker.units.qual.A;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import xyz.playedu.api.config.MinioConfig;
|
||||
import xyz.playedu.api.constant.BackendConstant;
|
||||
import xyz.playedu.api.domain.Resource;
|
||||
import xyz.playedu.api.service.MinioService;
|
||||
import xyz.playedu.api.service.ResourceCategoryService;
|
||||
import xyz.playedu.api.service.ResourceService;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
import xyz.playedu.api.util.HelperUtil;
|
||||
import xyz.playedu.api.vendor.PlayEduMinioClient;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
@ -38,13 +32,7 @@ public class UploadController {
|
||||
private ResourceCategoryService resourceCategoryService;
|
||||
|
||||
@Autowired
|
||||
private MinioConfig minioConfig;
|
||||
|
||||
@Autowired
|
||||
private MinioClient minioClient;
|
||||
|
||||
@Autowired
|
||||
private PlayEduMinioClient playEduMinioClient;
|
||||
private MinioService minioService;
|
||||
|
||||
@PostMapping("/image")
|
||||
public JsonResponse image(@RequestParam HashMap<String, Object> params, MultipartFile file) {
|
||||
@ -57,8 +45,8 @@ public class UploadController {
|
||||
return JsonResponse.error("格式不支持");
|
||||
}
|
||||
|
||||
Integer categoryId = MapUtils.getInteger(params, "category_id");
|
||||
if (categoryId != null && !categoryId.equals(0) && resourceCategoryService.getById(categoryId) == null) {
|
||||
Integer cid = MapUtils.getInteger(params, "category_id");
|
||||
if (cid != null && !cid.equals(0) && resourceCategoryService.getById(cid) == null) {
|
||||
return JsonResponse.error("分类不存在");
|
||||
}
|
||||
|
||||
@ -72,18 +60,12 @@ public class UploadController {
|
||||
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();
|
||||
// 保存文件
|
||||
String url = minioService.saveFile(file, savePath, contentType);
|
||||
// 上传记录
|
||||
Resource res = resourceService.create(cid, BackendConstant.RESOURCE_TYPE_IMAGE, oldFilename, ext, file.getSize(), "minio", "", savePath, url);
|
||||
|
||||
minioClient.putObject(objectArgs);
|
||||
|
||||
String url = minioConfig.getDomain() + minioConfig.getBucket() + "/" + savePath;
|
||||
|
||||
Resource res = resourceService.create(categoryId, BackendConstant.RESOURCE_TYPE_IMAGE, oldFilename, ext, file.getSize(), "minio", "", savePath, url);
|
||||
return JsonResponse.data(res);
|
||||
} catch (Exception e) {
|
||||
return JsonResponse.error("系统错误");
|
||||
}
|
||||
return JsonResponse.data(res);
|
||||
}
|
||||
|
||||
@GetMapping("/minio-upload-id")
|
||||
@ -96,23 +78,17 @@ public class UploadController {
|
||||
if (contentType == null) {
|
||||
return JsonResponse.error("该格式不支持上传");
|
||||
}
|
||||
String resourceType = BackendConstant.RESOURCE_EXT_2_TYPE.get(extension.toLowerCase());
|
||||
|
||||
try {
|
||||
String filename = HelperUtil.randomString(32) + "." + extension;
|
||||
String path = BackendConstant.UPLOAD_VIDEO_DIR + filename;
|
||||
String uploadId = playEduMinioClient.uploadId(minioConfig.getBucket(), path);
|
||||
String filename = HelperUtil.randomString(32) + "." + extension;//文件名
|
||||
String path = BackendConstant.UPLOAD_VIDEO_DIR + filename;//存储路径
|
||||
String uploadId = minioService.uploadId(path);
|
||||
|
||||
HashMap<String, String> data = new HashMap<>();
|
||||
data.put("resource_type", resourceType);
|
||||
data.put("upload_id", uploadId);
|
||||
data.put("filename", path);
|
||||
HashMap<String, String> data = new HashMap<>();
|
||||
data.put("resource_type", BackendConstant.RESOURCE_EXT_2_TYPE.get(extension.toLowerCase()));
|
||||
data.put("upload_id", uploadId);
|
||||
data.put("filename", path);
|
||||
|
||||
return JsonResponse.data(data);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
return JsonResponse.error("系统错误");
|
||||
}
|
||||
return JsonResponse.data(data);
|
||||
}
|
||||
|
||||
@GetMapping("/minio-pre-sign-url")
|
||||
@ -121,21 +97,12 @@ public class UploadController {
|
||||
Integer partNumber = MapUtils.getInteger(params, "part_number");
|
||||
String filename = MapUtils.getString(params, "filename");
|
||||
|
||||
try {
|
||||
Map<String, String> extraQueryParams = new HashMap<>();
|
||||
extraQueryParams.put("partNumber", partNumber + "");
|
||||
extraQueryParams.put("uploadId", uploadId);
|
||||
String url = minioService.chunkPreSignUrl(filename, partNumber + "", uploadId);
|
||||
|
||||
String url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(minioConfig.getBucket()).object(filename).method(Method.PUT).expiry(60 * 60 * 24).extraQueryParams(extraQueryParams).build());
|
||||
HashMap<String, String> data = new HashMap<>();
|
||||
data.put("url", url);
|
||||
|
||||
HashMap<String, String> data = new HashMap<>();
|
||||
data.put("url", url);
|
||||
|
||||
return JsonResponse.data(data);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
return JsonResponse.error("系统错误");
|
||||
}
|
||||
return JsonResponse.data(data);
|
||||
}
|
||||
|
||||
@GetMapping("/minio-merge")
|
||||
@ -148,10 +115,11 @@ public class UploadController {
|
||||
if (uploadId == null || uploadId.trim().length() == 0) {
|
||||
return JsonResponse.error("uploadId必填");
|
||||
}
|
||||
playEduMinioClient.merge(minioConfig.getBucket(), filename, uploadId);
|
||||
|
||||
String url = minioService.merge(filename, uploadId);
|
||||
|
||||
HashMap<String, Object> data = new HashMap<>();
|
||||
data.put("url", minioConfig.getDomain() + minioConfig.getBucket() + "/" + filename);
|
||||
data.put("url", url);
|
||||
|
||||
return JsonResponse.data(data);
|
||||
}
|
||||
|
23
src/main/java/xyz/playedu/api/service/MinioService.java
Normal file
23
src/main/java/xyz/playedu/api/service/MinioService.java
Normal file
@ -0,0 +1,23 @@
|
||||
package xyz.playedu.api.service;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/3/7 13:29
|
||||
*/
|
||||
public interface MinioService {
|
||||
|
||||
String url(String path);
|
||||
|
||||
String saveFile(MultipartFile file, String savePath, String contentType);
|
||||
|
||||
String uploadId(String path);
|
||||
|
||||
String chunkPreSignUrl(String filename, String partNumber, String uploadId);
|
||||
|
||||
String merge(String filename, String uploadId);
|
||||
|
||||
void removeByPath(String path);
|
||||
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
package xyz.playedu.api.service.impl;
|
||||
|
||||
import io.minio.GetPresignedObjectUrlArgs;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.PutObjectArgs;
|
||||
import io.minio.RemoveObjectArgs;
|
||||
import io.minio.http.Method;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import xyz.playedu.api.config.MinioConfig;
|
||||
import xyz.playedu.api.service.MinioService;
|
||||
import xyz.playedu.api.vendor.PlayEduMinioClient;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author 杭州白书科技有限公司
|
||||
* @create 2023/3/7 13:29
|
||||
*/
|
||||
@Service
|
||||
public class MinioServiceImpl implements MinioService {
|
||||
|
||||
@Autowired
|
||||
private MinioConfig c;
|
||||
|
||||
@Autowired
|
||||
private MinioClient client;
|
||||
|
||||
@Autowired
|
||||
private PlayEduMinioClient playEduMinioClient;
|
||||
|
||||
@Override
|
||||
public String url(String path) {
|
||||
return c.getDomain() + c.getBucket() + path;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public String saveFile(MultipartFile file, String savePath, String contentType) {
|
||||
PutObjectArgs objectArgs = PutObjectArgs.builder()
|
||||
.bucket(c.getBucket())
|
||||
.object(savePath)
|
||||
.stream(file.getInputStream(), file.getSize(), -1)
|
||||
.contentType(contentType)
|
||||
.build();
|
||||
client.putObject(objectArgs);
|
||||
|
||||
return url(savePath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadId(String path) {
|
||||
return playEduMinioClient.uploadId(c.getBucket(), path);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public String chunkPreSignUrl(String filename, String partNumber, String uploadId) {
|
||||
Map<String, String> extraQueryParams = new HashMap<>();
|
||||
extraQueryParams.put("partNumber", partNumber + "");
|
||||
extraQueryParams.put("uploadId", uploadId);
|
||||
|
||||
return client.getPresignedObjectUrl(
|
||||
GetPresignedObjectUrlArgs.builder()
|
||||
.bucket(c.getBucket())
|
||||
.object(filename)
|
||||
.method(Method.PUT)
|
||||
.expiry(60 * 60 * 24)
|
||||
.extraQueryParams(extraQueryParams)
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String merge(String filename, String uploadId) {
|
||||
playEduMinioClient.merge(c.getBucket(), filename, uploadId);
|
||||
return url(filename);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public void removeByPath(String path) {
|
||||
client.removeObject(RemoveObjectArgs.builder().bucket(c.getBucket()).object(path).build());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user