资源管理微调
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package com.njcn.gather.system.config;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* @author caozehui
|
||||
* @data 2026-05-28
|
||||
*/
|
||||
@Component
|
||||
public class PathConfig {
|
||||
|
||||
private String appPath;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
// 获取程序运行目录
|
||||
appPath = System.getProperty("user.dir");
|
||||
|
||||
// 或者获取jar包所在目录
|
||||
// appPath = new File(getClass().getProtectionDomain()
|
||||
// .getCodeSource().getLocation().getPath()).getParent();
|
||||
}
|
||||
|
||||
public String getAppPath() {
|
||||
return appPath;
|
||||
}
|
||||
|
||||
public String getLogPath() {
|
||||
return appPath + File.separator + "logs";
|
||||
}
|
||||
|
||||
public String getDataPath() {
|
||||
return appPath + File.separator + "data";
|
||||
}
|
||||
|
||||
public String getReportTemplatePath() {
|
||||
return this.getDataPath() + File.separator + "template";
|
||||
}
|
||||
|
||||
public String getReportPath() {
|
||||
return this.getDataPath() + File.separator + "report";
|
||||
}
|
||||
|
||||
public String getResourcePath() {
|
||||
return this.getDataPath() + File.separator + "resource";
|
||||
}
|
||||
}
|
||||
@@ -65,6 +65,20 @@ public class ResourceManageController extends BaseController {
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
|
||||
}
|
||||
|
||||
@OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
|
||||
@PostMapping("/update")
|
||||
@ApiOperation("编辑资源信息")
|
||||
@ApiImplicitParam(name = "updateParam", value = "资源参数", required = true)
|
||||
public HttpResult<Boolean> update(@RequestBody @Validated ResourceManageParam.UpdateParam updateParam) {
|
||||
String methodDescribe = getMethodDescribe("update");
|
||||
LogUtil.njcnDebug(log, "{},编辑资源参数为:{}", methodDescribe, updateParam);
|
||||
boolean result = resourceManageService.update(updateParam);
|
||||
if (result) {
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
|
||||
}
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
|
||||
}
|
||||
|
||||
@OperateInfo(info = LogEnum.SYSTEM_COMMON)
|
||||
@GetMapping("/play")
|
||||
@ApiOperation("获取视频播放地址")
|
||||
|
||||
@@ -19,7 +19,8 @@ public enum ResourceManageResponseEnum {
|
||||
PLAY_TOKEN_INVALID("A013009", "播放授权无效"),
|
||||
PLAY_TOKEN_EXPIRED("A013010", "播放授权已过期"),
|
||||
RANGE_INVALID("A013011", "视频请求范围非法"),
|
||||
DISK_SPACE_NOT_ENOUGH("A013012", "磁盘空间不足");
|
||||
DISK_SPACE_NOT_ENOUGH("A013012", "磁盘空间不足"),
|
||||
ID_NOT_BLANK("A013013", "资源id不能为空");
|
||||
|
||||
private final String code;
|
||||
private final String message;
|
||||
|
||||
@@ -21,6 +21,18 @@ public class ResourceManageParam {
|
||||
@ApiModelProperty(value = "视频文件", required = true)
|
||||
private MultipartFile file;
|
||||
|
||||
@Data
|
||||
public static class UpdateParam {
|
||||
@ApiModelProperty(value = "资源id", required = true)
|
||||
private String id;
|
||||
|
||||
@ApiModelProperty(value = "资源名称", required = true)
|
||||
private String name;
|
||||
|
||||
@ApiModelProperty(value = "备注", required = true)
|
||||
private String remark;
|
||||
}
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public static class QueryParam extends BaseParam {
|
||||
|
||||
@@ -18,6 +18,8 @@ public interface IResourceManageService extends IService<ResourceManage> {
|
||||
|
||||
boolean add(ResourceManageParam resourceManageParam);
|
||||
|
||||
boolean update(ResourceManageParam.UpdateParam updateParam);
|
||||
|
||||
PlayVO play(String id);
|
||||
|
||||
void stream(String id, String token, String rangeHeader, HttpServletResponse response);
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.njcn.common.pojo.enums.common.DataStateEnum;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.gather.system.config.PathConfig;
|
||||
import com.njcn.gather.system.resource.mapper.ResourceManageMapper;
|
||||
import com.njcn.gather.system.resource.pojo.enums.ResourceManageResponseEnum;
|
||||
import com.njcn.gather.system.resource.pojo.param.ResourceManageParam;
|
||||
@@ -19,7 +20,6 @@ import com.njcn.web.utils.RequestUtil;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@@ -53,8 +53,9 @@ public class ResourceManageServiceImpl extends ServiceImpl<ResourceManageMapper,
|
||||
|
||||
private static final ConcurrentHashMap<String, PlayToken> PLAY_TOKEN_CACHE = new ConcurrentHashMap<>();
|
||||
|
||||
@Value("${resource.videoDir:D:/data/resources/videos}")
|
||||
private String videoDir;
|
||||
// @Value("${resource.videoDir:D:/data/resources/videos}")
|
||||
// private String videoDir;
|
||||
private final PathConfig pathConfig;
|
||||
|
||||
@Override
|
||||
public Page<ResourceManageVO> list(ResourceManageParam.QueryParam queryParam) {
|
||||
@@ -93,7 +94,7 @@ public class ResourceManageServiceImpl extends ServiceImpl<ResourceManageMapper,
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
String dateDir = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
||||
String savedFileName = UUID.randomUUID().toString().replace("-", "") + MP4_SUFFIX;
|
||||
Path rootPath = Paths.get(videoDir).toAbsolutePath().normalize();
|
||||
Path rootPath = Paths.get(pathConfig.getResourcePath()).toAbsolutePath().normalize();
|
||||
Path targetDir = rootPath.resolve(dateDir).normalize();
|
||||
Path targetFile = targetDir.resolve(savedFileName).normalize();
|
||||
ensurePathInRoot(rootPath, targetFile);
|
||||
@@ -134,6 +135,19 @@ public class ResourceManageServiceImpl extends ServiceImpl<ResourceManageMapper,
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean update(ResourceManageParam.UpdateParam updateParam) {
|
||||
validateUpdateParam(updateParam);
|
||||
getEnabledResource(updateParam.getId());
|
||||
return this.lambdaUpdate()
|
||||
.set(ResourceManage::getName, updateParam.getName().trim())
|
||||
.set(ResourceManage::getRemark, updateParam.getRemark().trim())
|
||||
.eq(ResourceManage::getId, updateParam.getId())
|
||||
.eq(ResourceManage::getState, DataStateEnum.ENABLE.getCode())
|
||||
.update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayVO play(String id) {
|
||||
ResourceManage resourceManage = getEnabledResource(id);
|
||||
@@ -217,6 +231,18 @@ public class ResourceManageServiceImpl extends ServiceImpl<ResourceManageMapper,
|
||||
}
|
||||
}
|
||||
|
||||
private void validateUpdateParam(ResourceManageParam.UpdateParam param) {
|
||||
if (param == null || StrUtil.isBlank(param.getId())) {
|
||||
throw new BusinessException(ResourceManageResponseEnum.ID_NOT_BLANK);
|
||||
}
|
||||
if (StrUtil.isBlank(param.getName())) {
|
||||
throw new BusinessException(ResourceManageResponseEnum.NAME_NOT_BLANK);
|
||||
}
|
||||
if (StrUtil.isBlank(param.getRemark())) {
|
||||
throw new BusinessException(ResourceManageResponseEnum.REMARK_NOT_BLANK);
|
||||
}
|
||||
}
|
||||
|
||||
private ResourceManage getEnabledResource(String id) {
|
||||
ResourceManage resourceManage = this.lambdaQuery()
|
||||
.eq(ResourceManage::getId, id)
|
||||
@@ -229,7 +255,7 @@ public class ResourceManageServiceImpl extends ServiceImpl<ResourceManageMapper,
|
||||
}
|
||||
|
||||
private Path resolveResourcePath(ResourceManage resourceManage) {
|
||||
Path rootPath = Paths.get(videoDir).toAbsolutePath().normalize();
|
||||
Path rootPath = Paths.get(pathConfig.getResourcePath()).toAbsolutePath().normalize();
|
||||
String relativePath = resourceManage.getRelativePath().replace("\\", "/");
|
||||
String prefix = RELATIVE_VIDEO_ROOT + "/";
|
||||
if (!relativePath.startsWith(prefix)) {
|
||||
|
||||
Reference in New Issue
Block a user