资源分类整改

This commit is contained in:
none
2023-03-08 17:20:39 +08:00
parent e8455a3a58
commit 64309a078a
22 changed files with 372 additions and 188 deletions

View File

@@ -6,7 +6,6 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.PlayEduBackendThreadLocal;
import xyz.playedu.api.constant.BPermissionConstant;
import xyz.playedu.api.constant.BackendConstant;
import xyz.playedu.api.domain.ResourceCategory;
import xyz.playedu.api.event.ResourceCategoryDestroyEvent;
import xyz.playedu.api.exception.NotFoundException;
@@ -15,10 +14,8 @@ import xyz.playedu.api.request.backend.ResourceCategoryRequest;
import xyz.playedu.api.service.ResourceCategoryService;
import xyz.playedu.api.types.JsonResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
* @Author 杭州白书科技有限公司
@@ -29,62 +26,59 @@ import java.util.List;
public class ResourceCategoryController {
@Autowired
private ResourceCategoryService resourceCategoryService;
private ResourceCategoryService categoryService;
@Autowired
private ApplicationContext ctx;
@GetMapping("/index")
public JsonResponse index(@RequestParam(name = "type") String type) {
List<ResourceCategory> categories = resourceCategoryService.getByType(type);
public JsonResponse index() {
Map<Integer, List<ResourceCategory>> categories = categoryService.all().stream().collect(Collectors.groupingBy(ResourceCategory::getParentId));
HashMap<String, Object> data = new HashMap<>();
data.put("data", categories);
data.put("categories", categories);
return JsonResponse.data(data);
}
@BackendPermissionMiddleware(slug = BPermissionConstant.RESOURCE_CATEGORY)
@GetMapping("/create")
public JsonResponse create() {
Map<Integer, List<ResourceCategory>> categories = categoryService.all().stream().collect(Collectors.groupingBy(ResourceCategory::getParentId));
HashMap<String, Object> data = new HashMap<>();
data.put("types", BackendConstant.RESOURCE_TYPE_WHITELIST);
data.put("categories", categories);
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())) {
return JsonResponse.error("资源类型不支持");
}
resourceCategoryService.create(req.getType(), req.getSort(), req.getName());
public JsonResponse store(@RequestBody @Validated ResourceCategoryRequest req) throws NotFoundException {
categoryService.create(req.getName(), req.getParentId(), req.getSort());
return JsonResponse.success();
}
@BackendPermissionMiddleware(slug = BPermissionConstant.RESOURCE_CATEGORY)
@GetMapping("/{id}")
public JsonResponse edit(@PathVariable(name = "id") Integer id) throws NotFoundException {
ResourceCategory category = resourceCategoryService.findOrFail(id);
public JsonResponse edit(@PathVariable Integer id) throws NotFoundException {
ResourceCategory category = categoryService.findOrFail(id);
return JsonResponse.data(category);
}
@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())) {
return JsonResponse.error("资源类型不支持");
}
ResourceCategory category = resourceCategoryService.findOrFail(id);
resourceCategoryService.update(category, req.getSort(), req.getName());
public JsonResponse update(@PathVariable Integer id, @RequestBody ResourceCategoryRequest req) throws NotFoundException {
ResourceCategory category = categoryService.findOrFail(id);
categoryService.update(category, req.getName(), req.getParentId(), req.getSort());
return JsonResponse.success();
}
@BackendPermissionMiddleware(slug = BPermissionConstant.RESOURCE_CATEGORY)
@DeleteMapping("/{id}")
public JsonResponse destroy(@PathVariable(name = "id") Integer id) {
resourceCategoryService.removeById(id);
ctx.publishEvent(new ResourceCategoryDestroyEvent(this, PlayEduBackendThreadLocal.getAdminUserID(), id, new Date()));
public JsonResponse destroy(@PathVariable Integer id) throws NotFoundException {
ResourceCategory category = categoryService.findOrFail(id);
categoryService.deleteById(category.getId());
ctx.publishEvent(new ResourceCategoryDestroyEvent(this, PlayEduBackendThreadLocal.getAdminUserID(), category.getId(), new Date()));
return JsonResponse.success();
}

View File

@@ -1,19 +1,14 @@
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.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
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;
import xyz.playedu.api.types.JsonResponse;
@@ -28,7 +23,6 @@ import java.util.stream.Collectors;
* @create 2023/2/23 10:50
*/
@RestController
@Slf4j
@RequestMapping("/backend/v1/resource")
public class ResourceController {
@@ -38,9 +32,6 @@ public class ResourceController {
@Autowired
private ResourceVideoService resourceVideoService;
@Autowired
private ResourceCategoryService categoryService;
@Autowired
private MinioService minioService;
@@ -61,7 +52,6 @@ public class ResourceController {
ResourcePaginateFilter filter = new ResourcePaginateFilter();
filter.setSortAlgo(sortAlgo);
filter.setSortField(sortField);
filter.setType(type);
if (name != null && name.length() > 0) {
filter.setName(name);
}
@@ -84,60 +74,6 @@ public class ResourceController {
return JsonResponse.data(data);
}
@GetMapping("/create")
public JsonResponse create(@RequestParam(name = "type") String type) {
List<ResourceCategory> categories = categoryService.getByType(type);
HashMap<String, Object> data = new HashMap<>();
data.put("categories", categories);
return JsonResponse.data(data);
}
@PostMapping("/create")
@Transactional
public JsonResponse store(@RequestBody @Validated ResourceRequest req) {
String disk = req.getDisk();
if (!Arrays.asList(BackendConstant.RESOURCE_DISK_WHITELIST).contains(disk)) {
return JsonResponse.error("存储磁盘参数错误");
}
String extension = req.getExtension().toLowerCase();
String type = BackendConstant.RESOURCE_EXT_2_TYPE.get(extension);
if (type == null) {
return JsonResponse.error("格式不支持");
}
// 如果是视频则必须传递duration参数
Integer duration = req.getDuration();
String poster = req.getPoster();
boolean isVideoType = BackendConstant.RESOURCE_TYPE_VIDEO.equals(type);
if (isVideoType) {
if (duration == null || duration == 0) {
return JsonResponse.error("duration参数必须存在且大于0");
}
if (poster == null || poster.trim().length() == 0) {
return JsonResponse.error("poster参数值不能为空");
}
}
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);
}
return JsonResponse.data(res);
}
@DeleteMapping("/{id}")
@Transactional
public JsonResponse destroy(@PathVariable(name = "id") Integer id) throws NotFoundException {

View File

@@ -11,12 +11,12 @@ 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.HashMap;
/**
@@ -27,9 +27,6 @@ import java.util.HashMap;
@Slf4j
@RequestMapping("/backend/v1/upload")
public class UploadController {
@Autowired
private ResourceCategoryService resourceCategoryService;
@Autowired
private MinioService minioService;
@@ -40,9 +37,9 @@ public class UploadController {
private ResourceService resourceService;
@PostMapping("/file")
public JsonResponse image(@RequestParam HashMap<String, Object> params, MultipartFile file) throws ServiceException {
Integer cid = MapUtils.getInteger(params, "category_id");
Resource res = uploadService.storeMinio(file, cid);
public JsonResponse file(@RequestParam HashMap<String, Object> params, MultipartFile file) throws ServiceException {
String categoryIds = MapUtils.getString(params, "category_ids");
Resource res = uploadService.storeMinio(file, categoryIds);
return JsonResponse.data(res);
}
@@ -85,21 +82,16 @@ public class UploadController {
@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,
req.getCategoryIds(),
type,
req.getOriginalFilename(),
req.getExtension(),
@@ -110,7 +102,7 @@ public class UploadController {
url
);
// 视频封面素材保存
Resource posterResource = uploadService.storeBase64Image(req.getPoster(), 0);
Resource posterResource = uploadService.storeBase64Image(req.getPoster(), null);
// 视频的封面素材改为[隐藏 && 属于视频的子素材]
resourceService.changeParentId(posterResource.getId(), videoResource.getId());
// 视频信息