资源video的时长记录

This commit is contained in:
none 2023-03-02 15:17:30 +08:00
parent c126e47823
commit 5e08a55f5e
9 changed files with 230 additions and 24 deletions

View File

@ -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[] 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<String, String> RESOURCE_EXT_2_CONTENT_TYPE = new HashMap<>() {{ public final static HashMap<String, String> RESOURCE_EXT_2_CONTENT_TYPE = new HashMap<>() {{
put("png", "image/png"); put("png", "image/png");
put("jpg", "image/jpg"); put("jpg", "image/jpg");
@ -21,19 +27,19 @@ public class BackendConstant {
put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"); put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
}}; }};
public final static HashMap<String, String> RESOURCE_EXT_2_TYPE = new HashMap<>() {{ public final static HashMap<String, String> RESOURCE_EXT_2_TYPE = new HashMap<>() {{
put("png", "IMAGE"); put("png", RESOURCE_TYPE_IMAGE);
put("jpg", "IMAGE"); put("jpg", RESOURCE_TYPE_IMAGE);
put("jpeg", "IMAGE"); put("jpeg", RESOURCE_TYPE_IMAGE);
put("gif", "IMAGE"); put("gif", RESOURCE_TYPE_IMAGE);
put("pdf", "PDF"); put("pdf", RESOURCE_TYPE_PDF);
put("mp4", "VIDEO"); put("mp4", RESOURCE_TYPE_VIDEO);
put("doc", "WORD"); put("doc", RESOURCE_TYPE_WORD);
put("docx", "WORD"); put("docx", RESOURCE_TYPE_WORD);
put("ppt", "PPT"); put("ppt", RESOURCE_TYPE_PPT);
put("pptx", "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 = {"VIDEO"};
public final static String[] COURSE_HOUR_TYPE_WHITELIST_TEXT = {"视频"}; public final static String[] COURSE_HOUR_TYPE_WHITELIST_TEXT = {"视频"};

View File

@ -2,6 +2,7 @@ package xyz.playedu.api.controller.backend;
import org.apache.commons.collections4.MapUtils; import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.constant.BackendConstant; 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.request.backend.ResourceRequest;
import xyz.playedu.api.service.ResourceCategoryService; import xyz.playedu.api.service.ResourceCategoryService;
import xyz.playedu.api.service.ResourceService; import xyz.playedu.api.service.ResourceService;
import xyz.playedu.api.service.ResourceVideoService;
import xyz.playedu.api.types.JsonResponse; import xyz.playedu.api.types.JsonResponse;
import xyz.playedu.api.types.paginate.PaginationResult; import xyz.playedu.api.types.paginate.PaginationResult;
import xyz.playedu.api.types.paginate.ResourcePaginateFilter; import xyz.playedu.api.types.paginate.ResourcePaginateFilter;
@ -27,6 +29,9 @@ public class ResourceController {
@Autowired @Autowired
private ResourceService resourceService; private ResourceService resourceService;
@Autowired
private ResourceVideoService resourceVideoService;
@Autowired @Autowired
private ResourceCategoryService categoryService; private ResourceCategoryService categoryService;
@ -57,15 +62,46 @@ public class ResourceController {
} }
@PostMapping("/create") @PostMapping("/create")
@Transactional
public JsonResponse store(@RequestBody @Validated ResourceRequest req) { 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("资源分类不存在"); 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("存储磁盘参数错误"); 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); return JsonResponse.data(res);
} }

View File

@ -21,7 +21,7 @@ public class Resource implements Serializable {
* *
*/ */
@TableId(type = IdType.AUTO) @TableId(type = IdType.AUTO)
private Long id; private Integer id;
/** /**
* 分类id * 分类id

View File

@ -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();
}
}

View File

@ -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<ResourceVideo> {
}

View File

@ -1,6 +1,7 @@
package xyz.playedu.api.request.backend; package xyz.playedu.api.request.backend;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import lombok.Data; import lombok.Data;
import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.Length;
@ -12,32 +13,38 @@ import org.hibernate.validator.constraints.Length;
@Data @Data
public class ResourceRequest { public class ResourceRequest {
@NotNull(message = "请选择资源分类") @NotNull(message = "category_id参数不存在")
@JsonProperty("category_id") @JsonProperty("category_id")
private Integer categoryId; private Integer categoryId;
@NotNull(message = "资源名不能为空") @NotNull(message = "name参数不存在")
@NotBlank(message = "请输入资源名")
@Length(min = 1, max = 254, message = "资源名长度在1-254个字符之间") @Length(min = 1, max = 254, message = "资源名长度在1-254个字符之间")
private String name; private String name;
@NotNull(message = "请输入资源扩展") @NotNull(message = "extension参数不存在")
@NotBlank(message = "请输入资源扩展")
@Length(min = 1, max = 254, message = "资源扩展长度在1-20个字符之间") @Length(min = 1, max = 254, message = "资源扩展长度在1-20个字符之间")
private String extension; private String extension;
@NotNull(message = "请输入文件大小") @NotNull(message = "size参数不存在")
private Long size; private Long size;
@NotNull(message = "请输入文件存储磁盘") @NotNull(message = "disk参数不存在")
@NotBlank(message = "disk值不能为空")
private String disk; private String disk;
@NotNull(message = "请输入fileId") @NotNull(message = "file_id参数不存在")
@JsonProperty("file_id") @JsonProperty("file_id")
private String fileId; private String fileId;
@NotNull(message = "请输入存储路径") @NotNull(message = "path参数不存在")
@NotBlank(message = "path值不能为空")
private String path; private String path;
@NotNull(message = "请输入访问URL") @NotNull(message = "url参数不存在")
private String url; private String url;
private Integer duration;
} }

View File

@ -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<ResourceVideo> {
void create(Integer resourceId, Integer duration);
}

View File

@ -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<ResourceVideoMapper, ResourceVideo>
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);
}
}

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xyz.playedu.api.mapper.ResourceVideoMapper">
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.ResourceVideo">
<result property="rid" column="rid" jdbcType="INTEGER"/>
<result property="duration" column="duration" jdbcType="INTEGER"/>
<result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List">
rid,duration,created_at
</sql>
</mapper>