123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package com.fdkankan.fusion.service.impl;
- import cn.hutool.core.io.FileUtil;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.fdkankan.fusion.common.FilePath;
- import com.fdkankan.fusion.common.ResultCode;
- import com.fdkankan.fusion.common.util.ShellUtil;
- import com.fdkankan.fusion.entity.CaseImg;
- import com.fdkankan.fusion.exception.BusinessException;
- import com.fdkankan.fusion.mapper.ICaseImgMapper;
- import com.fdkankan.fusion.request.CaseImgParam;
- import com.fdkankan.fusion.service.ICaseImgService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.UUID;
- /**
- * <p>
- * 服务实现类
- * </p>
- *
- * @author
- * @since 2024-07-04
- */
- @Service
- @Slf4j
- public class CaseImgServiceImpl extends ServiceImpl<ICaseImgMapper, CaseImg> implements ICaseImgService {
- @Override
- public List<CaseImg> getByCaseId(Integer caseId,Integer type) {
- LambdaQueryWrapper<CaseImg> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(CaseImg::getCaseId,caseId);
- if(type != null){
- wrapper.eq(CaseImg::getType,type);
- }
- wrapper.orderByAsc(CaseImg::getSort);
- wrapper.orderByAsc(CaseImg::getId);
- return this.list(wrapper);
- }
- @Override
- public void updateSort(CaseImgParam param) {
- if(param.getParamList() == null || param.getParamList().isEmpty()){
- throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
- }
- for (CaseImgParam caseImgParam : param.getParamList()) {
- LambdaUpdateWrapper<CaseImg> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(CaseImg::getId,caseImgParam.getId());
- wrapper.set(CaseImg::getSort,caseImgParam.getSort());
- this.update(wrapper);
- }
- }
- @Value("${upload.query-path}")
- private String queryPath;
- @Override
- public CaseImg ffmpegImage(MultipartFile[] files, Integer caseId) {
- if(files.length <=0 || caseId == null){
- throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
- }
- String filePath = FilePath.FFMPEG_IMG_PATH +"/"+ caseId+"/";
- try {
- List<String> localList = new ArrayList<>();
- String outFileName = UUID.randomUUID().toString().replace("-","") ;
- String outSuffixName = ".jpg";
- String outLocalPath = filePath + outFileName +outSuffixName;
- for (MultipartFile file : files) {
- String suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
- //重新生成文件名
- String fileName = UUID.randomUUID().toString().replace("-","") ;
- String localFilePath = filePath + fileName + suffixName;
- File file1 = new File(localFilePath);
- if(!file1.getParentFile().exists()){
- file1.getParentFile().mkdirs();
- }
- file.transferTo(file1);
- localList.add(localFilePath);
- }
- StringBuilder ffmpegCmd = new StringBuilder("ffmpeg ");
- if(files.length >1){
- for (String localPath : localList) {
- ffmpegCmd.append(" -i ").append(localPath);
- }
- ffmpegCmd.append(" -filter_complex hstack=inputs="+ files.length);
- ffmpegCmd.append(" " +outLocalPath);
- ShellUtil.execCmd(ffmpegCmd.toString());
- }else {
- outLocalPath = localList.get(0);
- }
- if(!FileUtil.exist(outLocalPath)){
- throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
- }
- ShellUtil.yunUpload(outLocalPath,outLocalPath.replace("/mnt/",""));
- List<CaseImg> caseImgList = this.getByCaseId(caseId, 1);
- CaseImg caseImg = null;
- if(caseImgList ==null || caseImgList.isEmpty()){
- caseImg = new CaseImg();
- }else {
- caseImg = caseImgList.get(0);
- }
- caseImg.setCaseId(caseId);
- caseImg.setImgUrl(queryPath + outLocalPath.replace("/mnt/",""));
- caseImg.setImgInfo("照片卷");
- caseImg.setType(1);
- this.saveOrUpdate(caseImg);
- return caseImg;
- }catch (Exception e){
- log.info("ffmpeg错误:{}",caseId,e);
- }finally {
- try {
- Thread.sleep(2000L);
- FileUtil.del(filePath);
- }catch (Exception e){
- log.info("删除失败:{}",e);
- }
- }
- return null;
- }
- }
|