Browse Source

测试包管理

master
zhouyl 1 year ago
parent
commit
62eead7a86
  1. 8
      tp-admin/src/main/java/com/inscloudtech/web/controller/system/ToolManageController.java
  2. 2
      tp-admin/src/main/resources/application-prod.yml
  3. 42
      tp-common/src/main/java/com/inscloudtech/common/utils/file/FileUploadUtils.java
  4. 53
      tp-system/src/main/java/com/inscloudtech/system/service/impl/ToolManageServiceImpl.java

8
tp-admin/src/main/java/com/inscloudtech/web/controller/system/ToolManageController.java

@ -92,14 +92,12 @@ public class ToolManageController extends BaseController {
*/
@Log(title = "测试包管理", businessType = BusinessType.INSERT)
@PostMapping(value = "/uploadToolAndTips", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public R uploadToolAndTips(@RequestPart("file") MultipartFile file,
@RequestPart("tipsFile") MultipartFile tipsFile,
public R uploadToolAndTips( MultipartFile file,
MultipartFile tipsFile,
@RequestPart("moduleName") String moduleName,
@RequestPart("toolName") String toolName,
@RequestPart("toolManageId") Long toolManageId) {
if (ObjectUtil.isNull(file) && ObjectUtil.isNull(tipsFile) ) {
return R.fail("上传文件不能为空!");
}
if (StrUtil.isEmpty(moduleName)) {
return R.fail("模块名不能为空!");
}

2
tp-admin/src/main/resources/application-prod.yml

@ -1,5 +1,5 @@
--- # 临时文件存储位置 避免临时文件被系统清理报错
spring.servlet.multipart.location: /app/testP/temp
spring.servlet.multipart.location: /app/testp/temp
--- # 数据源配置
spring:

42
tp-common/src/main/java/com/inscloudtech/common/utils/file/FileUploadUtils.java

@ -43,7 +43,7 @@ public class FileUploadUtils {
* Minio默认上传的地址
*/
@Getter
private static final String bucketName = "testP";
private static final String bucketName = "testp";
static {
// ClassPathResource cpr = new ClassPathResource("application.yml");
@ -220,6 +220,46 @@ public class FileUploadUtils {
}
}
/**
* 文件大小校验
*
* @param file 上传的文件
* @throws FileSizeLimitExceededException 如果超出最大大小
*/
public static boolean checkFileValid(MultipartFile file)
throws FileSizeLimitExceededException, InvalidExtensionException {
long size = file.getSize();
if (size > DEFAULT_MAX_SIZE) {
throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
}
String[] allowedExtension = MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION;
String fileName = file.getOriginalFilename();
String extension = getExtension(file);
if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) {
if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION) {
return false;
// throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
// fileName);
} else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION) {
// throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
// fileName);
return false;
} else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION) {
// throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
// fileName);
return false;
} else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION) {
// throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension,
// fileName);'
return false;
} else {
// throw new InvalidExtensionException(allowedExtension, extension, fileName);
return false;
}
}
return true;
}
/**
* 判断MIME类型是否是允许的MIME类型
*/

53
tp-system/src/main/java/com/inscloudtech/system/service/impl/ToolManageServiceImpl.java

@ -11,6 +11,9 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.inscloudtech.common.exception.ServiceException;
import com.inscloudtech.common.exception.file.InvalidExtensionException;
import com.inscloudtech.common.utils.file.FileUploadUtils;
import com.inscloudtech.common.utils.file.MimeTypeUtils;
import com.inscloudtech.system.domain.SysOss;
import com.inscloudtech.system.domain.vo.DownloadToolRequest;
import com.inscloudtech.system.domain.vo.SysOssVo;
@ -129,7 +132,7 @@ public class ToolManageServiceImpl implements IToolManageService {
ToolManage toolManage;
if(null != toolManageId && toolManageId != 0L){
toolManage = baseMapper.selectById(toolManageId);
if (ObjectUtil.isNotNull(toolManage)) {
if (ObjectUtil.isNull(toolManage)) {
throw new ServiceException("修改对象不存在!");
}
}else {
@ -139,19 +142,39 @@ public class ToolManageServiceImpl implements IToolManageService {
}
toolManage = new ToolManage();
}
SysOssVo toolVo = iSysOssService.upload(file);
SysOssVo tipsVo = iSysOssService.upload(tipsFile);
toolManage.setToolName(toolName);
toolManage.setModuleName(moduleName);
toolManage.setToolOssId(toolVo.getOssId());
toolManage.setToolPath(toolVo.getOriginalName());
toolManage.setTipsOssId(tipsVo.getOssId());
toolManage.setTipsPath(tipsVo.getOriginalName());
if(null != toolManageId && toolManageId != 0L){
baseMapper.updateById(toolManage);
}else {
baseMapper.insert(toolManage);
try {
if(FileUploadUtils.checkFileValid(file)){
SysOssVo toolVo = iSysOssService.upload(file);
toolManage.setToolOssId(toolVo.getOssId());
toolManage.setToolPath(toolVo.getOriginalName());
}else if(null != toolManageId && toolManageId != 0L){
toolManage.setToolOssId(0L);
toolManage.setToolPath("");
}
if(FileUploadUtils.checkFileValid(tipsFile)){
SysOssVo tipsVo = iSysOssService.upload(tipsFile);
toolManage.setTipsOssId(tipsVo.getOssId());
toolManage.setTipsPath(tipsVo.getOriginalName());
}else if(null != toolManageId && toolManageId != 0L){
toolManage.setTipsOssId(0L);
toolManage.setTipsPath("");
}
toolManage.setToolName(toolName);
toolManage.setModuleName(moduleName);
if(null != toolManageId && toolManageId != 0L){
baseMapper.updateById(toolManage);
}else {
baseMapper.insert(toolManage);
}
}catch (Exception e){
e.printStackTrace();
}
return true;
}
@ -162,6 +185,10 @@ public class ToolManageServiceImpl implements IToolManageService {
ToolManage dbObj = baseMapper.selectOne(new LambdaQueryWrapper<ToolManage>().eq(ToolManage::getToolName, request.getToolName())
.eq(ToolManage::getModuleName, request.getModuleName()));
if (ObjectUtil.isNull(dbObj)) {
throw new ServiceException("测试包信息不存在!");
}
if(dbObj.getToolOssId() == null || dbObj.getToolOssId() == 0L){
throw new ServiceException("文件数据不存在!");
}

Loading…
Cancel
Save