CaseImgServiceImpl.java 5.5 KB

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