CaseImgServiceImpl.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.fdkankan.fusion.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  5. import com.fdkankan.fusion.common.FilePath;
  6. import com.fdkankan.fusion.common.ResultCode;
  7. import com.fdkankan.fusion.common.util.ShellUtil;
  8. import com.fdkankan.fusion.entity.CaseImg;
  9. import com.fdkankan.fusion.exception.BusinessException;
  10. import com.fdkankan.fusion.mapper.ICaseImgMapper;
  11. import com.fdkankan.fusion.request.CaseImgParam;
  12. import com.fdkankan.fusion.service.ICaseImgService;
  13. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.web.multipart.MultipartFile;
  19. import java.io.File;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.UUID;
  23. /**
  24. * <p>
  25. * 服务实现类
  26. * </p>
  27. *
  28. * @author
  29. * @since 2024-07-04
  30. */
  31. @Service
  32. @Slf4j
  33. public class CaseImgServiceImpl extends ServiceImpl<ICaseImgMapper, CaseImg> implements ICaseImgService {
  34. @Override
  35. public List<CaseImg> getByCaseId(Integer caseId,Integer type) {
  36. LambdaQueryWrapper<CaseImg> wrapper = new LambdaQueryWrapper<>();
  37. wrapper.eq(CaseImg::getCaseId,caseId);
  38. if(type != null){
  39. wrapper.eq(CaseImg::getType,type);
  40. }
  41. wrapper.orderByAsc(CaseImg::getSort);
  42. wrapper.orderByAsc(CaseImg::getId);
  43. return this.list(wrapper);
  44. }
  45. @Override
  46. public void updateSort(CaseImgParam param) {
  47. if(param.getParamList() == null || param.getParamList().isEmpty()){
  48. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  49. }
  50. for (CaseImgParam caseImgParam : param.getParamList()) {
  51. LambdaUpdateWrapper<CaseImg> wrapper = new LambdaUpdateWrapper<>();
  52. wrapper.eq(CaseImg::getId,caseImgParam.getId());
  53. wrapper.set(CaseImg::getSort,caseImgParam.getSort());
  54. this.update(wrapper);
  55. }
  56. }
  57. @Value("${upload.query-path}")
  58. private String queryPath;
  59. @Override
  60. public CaseImg ffmpegImage(MultipartFile[] files, Integer caseId) {
  61. if(files.length <=0 || caseId == null){
  62. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  63. }
  64. String filePath = FilePath.FFMPEG_IMG_PATH +"/"+ caseId+"/";
  65. try {
  66. List<String> localList = new ArrayList<>();
  67. String outFileName = UUID.randomUUID().toString().replace("-","") ;
  68. String outSuffixName = ".jpg";
  69. String outLocalPath = filePath + outFileName +outSuffixName;
  70. for (MultipartFile file : files) {
  71. String suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
  72. //重新生成文件名
  73. String fileName = UUID.randomUUID().toString().replace("-","") ;
  74. String localFilePath = filePath + fileName + suffixName;
  75. File file1 = new File(localFilePath);
  76. if(!file1.getParentFile().exists()){
  77. file1.getParentFile().mkdirs();
  78. }
  79. file.transferTo(file1);
  80. localList.add(localFilePath);
  81. }
  82. StringBuilder ffmpegCmd = new StringBuilder("ffmpeg ");
  83. if(files.length >1){
  84. for (String localPath : localList) {
  85. ffmpegCmd.append(" -i ").append(localPath);
  86. }
  87. ffmpegCmd.append(" -filter_complex hstack=inputs="+ files.length);
  88. ffmpegCmd.append(" " +outLocalPath);
  89. ShellUtil.execCmd(ffmpegCmd.toString());
  90. }else {
  91. outLocalPath = localList.get(0);
  92. }
  93. if(!FileUtil.exist(outLocalPath)){
  94. throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
  95. }
  96. ShellUtil.yunUpload(outLocalPath,outLocalPath.replace("/mnt/",""));
  97. List<CaseImg> caseImgList = this.getByCaseId(caseId, 1);
  98. CaseImg caseImg = null;
  99. if(caseImgList ==null || caseImgList.isEmpty()){
  100. caseImg = new CaseImg();
  101. }else {
  102. caseImg = caseImgList.get(0);
  103. }
  104. caseImg.setCaseId(caseId);
  105. caseImg.setImgUrl(queryPath + outLocalPath.replace("/mnt/",""));
  106. caseImg.setImgInfo("照片卷");
  107. caseImg.setType(1);
  108. this.saveOrUpdate(caseImg);
  109. return caseImg;
  110. }catch (Exception e){
  111. log.info("ffmpeg错误:{}",caseId,e);
  112. }finally {
  113. try {
  114. Thread.sleep(2000L);
  115. FileUtil.del(filePath);
  116. }catch (Exception e){
  117. log.info("删除失败:{}",e);
  118. }
  119. }
  120. return null;
  121. }
  122. }