From 5e08a55f5e1d6be2615ff4efe58309707462b5e8 Mon Sep 17 00:00:00 2001 From: none Date: Thu, 2 Mar 2023 15:17:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=84=E6=BA=90video=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E9=95=BF=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../playedu/api/constant/BackendConstant.java | 30 +++++--- .../backend/ResourceController.java | 42 +++++++++- .../java/xyz/playedu/api/domain/Resource.java | 2 +- .../xyz/playedu/api/domain/ResourceVideo.java | 76 +++++++++++++++++++ .../api/mapper/ResourceVideoMapper.java | 20 +++++ .../api/request/backend/ResourceRequest.java | 23 ++++-- .../api/service/ResourceVideoService.java | 14 ++++ .../impl/ResourceVideoServiceImpl.java | 31 ++++++++ .../resources/mapper/ResourceVideoMapper.xml | 16 ++++ 9 files changed, 230 insertions(+), 24 deletions(-) create mode 100644 src/main/java/xyz/playedu/api/domain/ResourceVideo.java create mode 100644 src/main/java/xyz/playedu/api/mapper/ResourceVideoMapper.java create mode 100644 src/main/java/xyz/playedu/api/service/ResourceVideoService.java create mode 100644 src/main/java/xyz/playedu/api/service/impl/ResourceVideoServiceImpl.java create mode 100644 src/main/resources/mapper/ResourceVideoMapper.xml diff --git a/src/main/java/xyz/playedu/api/constant/BackendConstant.java b/src/main/java/xyz/playedu/api/constant/BackendConstant.java index e521d10..f76ef92 100644 --- a/src/main/java/xyz/playedu/api/constant/BackendConstant.java +++ b/src/main/java/xyz/playedu/api/constant/BackendConstant.java @@ -7,7 +7,13 @@ 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_TYPE_WHITELIST = {"IMAGE", "PDF", "VIDEO", "WORD", "PPT"}; + public final static String RESOURCE_TYPE_VIDEO = "VIDEO"; + public final static String RESOURCE_TYPE_IMAGE = "IMAGE"; + public final static String RESOURCE_TYPE_PDF = "PDF"; + public final static String RESOURCE_TYPE_WORD = "WORD"; + public final static String RESOURCE_TYPE_PPT = "PPT"; + + public final static String[] RESOURCE_TYPE_WHITELIST = {RESOURCE_TYPE_IMAGE, RESOURCE_TYPE_PDF, RESOURCE_TYPE_VIDEO, RESOURCE_TYPE_WORD, RESOURCE_TYPE_PPT}; public final static HashMap RESOURCE_EXT_2_CONTENT_TYPE = new HashMap<>() {{ put("png", "image/png"); put("jpg", "image/jpg"); @@ -21,19 +27,19 @@ public class BackendConstant { put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"); }}; public final static HashMap 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"); + put("png", RESOURCE_TYPE_IMAGE); + put("jpg", RESOURCE_TYPE_IMAGE); + put("jpeg", RESOURCE_TYPE_IMAGE); + put("gif", RESOURCE_TYPE_IMAGE); + put("pdf", RESOURCE_TYPE_PDF); + put("mp4", RESOURCE_TYPE_VIDEO); + put("doc", RESOURCE_TYPE_WORD); + put("docx", RESOURCE_TYPE_WORD); + put("ppt", RESOURCE_TYPE_PPT); + put("pptx", RESOURCE_TYPE_PPT); }}; - public final static String[] RESOURCE_DISK_WHITELIST = {"MINIO"}; + public final static String[] RESOURCE_DISK_WHITELIST = {"minio"}; public final static String[] COURSE_HOUR_TYPE_WHITELIST = {"VIDEO"}; public final static String[] COURSE_HOUR_TYPE_WHITELIST_TEXT = {"视频"}; diff --git a/src/main/java/xyz/playedu/api/controller/backend/ResourceController.java b/src/main/java/xyz/playedu/api/controller/backend/ResourceController.java index cd46831..6ca99e6 100644 --- a/src/main/java/xyz/playedu/api/controller/backend/ResourceController.java +++ b/src/main/java/xyz/playedu/api/controller/backend/ResourceController.java @@ -2,6 +2,7 @@ package xyz.playedu.api.controller.backend; 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; @@ -10,6 +11,7 @@ import xyz.playedu.api.domain.ResourceCategory; import xyz.playedu.api.request.backend.ResourceRequest; import xyz.playedu.api.service.ResourceCategoryService; import xyz.playedu.api.service.ResourceService; +import xyz.playedu.api.service.ResourceVideoService; import xyz.playedu.api.types.JsonResponse; import xyz.playedu.api.types.paginate.PaginationResult; import xyz.playedu.api.types.paginate.ResourcePaginateFilter; @@ -27,6 +29,9 @@ public class ResourceController { @Autowired private ResourceService resourceService; + @Autowired + private ResourceVideoService resourceVideoService; + @Autowired private ResourceCategoryService categoryService; @@ -57,15 +62,46 @@ public class ResourceController { } @PostMapping("/create") + @Transactional public JsonResponse store(@RequestBody @Validated ResourceRequest req) { - if (categoryService.getById(req.getCategoryId()) == null) { + Integer categoryId = req.getCategoryId(); + if (categoryService.getById(categoryId) == null) { return JsonResponse.error("资源分类不存在"); } - if (!Arrays.asList(BackendConstant.RESOURCE_DISK_WHITELIST).contains(req.getDisk())) { + 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(); + boolean isVideoType = BackendConstant.RESOURCE_TYPE_VIDEO.equals(type); + if (isVideoType) { + if (duration == null || duration == 0) { + return JsonResponse.error("duration参数必须存在且大于0"); + } + } + + Resource res = resourceService.create( + categoryId, + req.getName(), + extension, + req.getSize(), + disk, + req.getFileId(), + req.getPath(), + req.getUrl() + ); + + if (isVideoType) { + resourceVideoService.create(res.getId(), duration); + } - Resource res = resourceService.create(req.getCategoryId(), req.getName(), req.getExtension(), req.getSize(), req.getDisk(), req.getFileId(), req.getPath(), req.getUrl()); return JsonResponse.data(res); } diff --git a/src/main/java/xyz/playedu/api/domain/Resource.java b/src/main/java/xyz/playedu/api/domain/Resource.java index 2d46f8d..984fe7d 100644 --- a/src/main/java/xyz/playedu/api/domain/Resource.java +++ b/src/main/java/xyz/playedu/api/domain/Resource.java @@ -21,7 +21,7 @@ public class Resource implements Serializable { * */ @TableId(type = IdType.AUTO) - private Long id; + private Integer id; /** * 分类id diff --git a/src/main/java/xyz/playedu/api/domain/ResourceVideo.java b/src/main/java/xyz/playedu/api/domain/ResourceVideo.java new file mode 100644 index 0000000..df29897 --- /dev/null +++ b/src/main/java/xyz/playedu/api/domain/ResourceVideo.java @@ -0,0 +1,76 @@ +package xyz.playedu.api.domain; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import java.io.Serializable; +import java.util.Date; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +/** + * + * @TableName resource_videos + */ +@TableName(value ="resource_videos") +@Data +public class ResourceVideo implements Serializable { + /** + * + */ + private Integer rid; + + /** + * 视频时长[s] + */ + private Integer duration; + + @JsonProperty("created_at") + private Date createdAt; + + @TableField(exist = false) + private static final long serialVersionUID = 1L; + + @Override + public boolean equals(Object that) { + if (this == that) { + return true; + } + if (that == null) { + return false; + } + if (getClass() != that.getClass()) { + return false; + } + ResourceVideo other = (ResourceVideo) that; + return (this.getRid() == null ? other.getRid() == null : this.getRid().equals(other.getRid())) + && (this.getDuration() == null ? other.getDuration() == null : this.getDuration().equals(other.getDuration())) + && (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt())); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getRid() == null) ? 0 : getRid().hashCode()); + result = prime * result + ((getDuration() == null) ? 0 : getDuration().hashCode()); + result = prime * result + ((getCreatedAt() == null) ? 0 : getCreatedAt().hashCode()); + return result; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()); + sb.append(" ["); + sb.append("Hash = ").append(hashCode()); + sb.append(", rid=").append(rid); + sb.append(", duration=").append(duration); + sb.append(", createdAt=").append(createdAt); + sb.append(", serialVersionUID=").append(serialVersionUID); + sb.append("]"); + return sb.toString(); + } +} \ No newline at end of file diff --git a/src/main/java/xyz/playedu/api/mapper/ResourceVideoMapper.java b/src/main/java/xyz/playedu/api/mapper/ResourceVideoMapper.java new file mode 100644 index 0000000..db850e9 --- /dev/null +++ b/src/main/java/xyz/playedu/api/mapper/ResourceVideoMapper.java @@ -0,0 +1,20 @@ +package xyz.playedu.api.mapper; + +import org.apache.ibatis.annotations.Mapper; +import xyz.playedu.api.domain.ResourceVideo; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; + +/** +* @author tengteng +* @description 针对表【resource_videos】的数据库操作Mapper +* @createDate 2023-03-02 15:13:03 +* @Entity xyz.playedu.api.domain.ResourceVideo +*/ +@Mapper +public interface ResourceVideoMapper extends BaseMapper { + +} + + + + diff --git a/src/main/java/xyz/playedu/api/request/backend/ResourceRequest.java b/src/main/java/xyz/playedu/api/request/backend/ResourceRequest.java index 2f8e458..067abe0 100644 --- a/src/main/java/xyz/playedu/api/request/backend/ResourceRequest.java +++ b/src/main/java/xyz/playedu/api/request/backend/ResourceRequest.java @@ -1,6 +1,7 @@ package xyz.playedu.api.request.backend; import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import lombok.Data; import org.hibernate.validator.constraints.Length; @@ -12,32 +13,38 @@ import org.hibernate.validator.constraints.Length; @Data public class ResourceRequest { - @NotNull(message = "请选择资源分类") + @NotNull(message = "category_id参数不存在") @JsonProperty("category_id") private Integer categoryId; - @NotNull(message = "资源名不能为空") + @NotNull(message = "name参数不存在") + @NotBlank(message = "请输入资源名") @Length(min = 1, max = 254, message = "资源名长度在1-254个字符之间") private String name; - @NotNull(message = "请输入资源扩展") + @NotNull(message = "extension参数不存在") + @NotBlank(message = "请输入资源扩展") @Length(min = 1, max = 254, message = "资源扩展长度在1-20个字符之间") private String extension; - @NotNull(message = "请输入文件大小") + @NotNull(message = "size参数不存在") private Long size; - @NotNull(message = "请输入文件存储磁盘") + @NotNull(message = "disk参数不存在") + @NotBlank(message = "disk值不能为空") private String disk; - @NotNull(message = "请输入fileId") + @NotNull(message = "file_id参数不存在") @JsonProperty("file_id") private String fileId; - @NotNull(message = "请输入存储路径") + @NotNull(message = "path参数不存在") + @NotBlank(message = "path值不能为空") private String path; - @NotNull(message = "请输入访问URL") + @NotNull(message = "url参数不存在") private String url; + private Integer duration; + } diff --git a/src/main/java/xyz/playedu/api/service/ResourceVideoService.java b/src/main/java/xyz/playedu/api/service/ResourceVideoService.java new file mode 100644 index 0000000..44e5f73 --- /dev/null +++ b/src/main/java/xyz/playedu/api/service/ResourceVideoService.java @@ -0,0 +1,14 @@ +package xyz.playedu.api.service; + +import xyz.playedu.api.domain.ResourceVideo; +import com.baomidou.mybatisplus.extension.service.IService; + +/** +* @author tengteng +* @description 针对表【resource_videos】的数据库操作Service +* @createDate 2023-03-02 15:13:03 +*/ +public interface ResourceVideoService extends IService { + + void create(Integer resourceId, Integer duration); +} diff --git a/src/main/java/xyz/playedu/api/service/impl/ResourceVideoServiceImpl.java b/src/main/java/xyz/playedu/api/service/impl/ResourceVideoServiceImpl.java new file mode 100644 index 0000000..78c7a86 --- /dev/null +++ b/src/main/java/xyz/playedu/api/service/impl/ResourceVideoServiceImpl.java @@ -0,0 +1,31 @@ +package xyz.playedu.api.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import xyz.playedu.api.domain.ResourceVideo; +import xyz.playedu.api.service.ResourceVideoService; +import xyz.playedu.api.mapper.ResourceVideoMapper; +import org.springframework.stereotype.Service; + +import java.util.Date; + +/** +* @author tengteng +* @description 针对表【resource_videos】的数据库操作Service实现 +* @createDate 2023-03-02 15:13:03 +*/ +@Service +public class ResourceVideoServiceImpl extends ServiceImpl + implements ResourceVideoService{ + @Override + public void create(Integer resourceId, Integer duration) { + ResourceVideo video = new ResourceVideo(); + video.setRid(resourceId); + video.setDuration(duration); + video.setCreatedAt(new Date()); + save(video); + } +} + + + + diff --git a/src/main/resources/mapper/ResourceVideoMapper.xml b/src/main/resources/mapper/ResourceVideoMapper.xml new file mode 100644 index 0000000..5e65725 --- /dev/null +++ b/src/main/resources/mapper/ResourceVideoMapper.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + rid,duration,created_at + +