SceneUploadServiceImpl.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package com.fdkankan.scene.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.fdkankan.common.constant.ConstantFilePath;
  6. import com.fdkankan.common.constant.ErrorCode;
  7. import com.fdkankan.common.constant.UploadFilePath;
  8. import com.fdkankan.common.exception.BusinessException;
  9. import com.fdkankan.common.util.BASE64DecodedMultipartFile;
  10. import com.fdkankan.common.util.FdfsUtil;
  11. import com.fdkankan.redis.constant.RedisKey;
  12. import com.fdkankan.scene.bean.ResultData;
  13. import com.fdkankan.scene.entity.Scene;
  14. import com.fdkankan.scene.entity.SceneFileMapping;
  15. import com.fdkankan.scene.service.FYunFileService;
  16. import com.fdkankan.scene.service.ISceneUploadService;
  17. import com.fdkankan.scene.service.SceneFileMappingService;
  18. import com.fdkankan.scene.service.SceneService;
  19. import com.fdkankan.scene.vo.DeleteFileParamVO;
  20. import lombok.extern.slf4j.Slf4j;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.stereotype.Service;
  24. import org.springframework.web.multipart.MultipartFile;
  25. import javax.annotation.Resource;
  26. import java.io.File;
  27. import java.util.*;
  28. /**
  29. * <p>
  30. * 服务实现类
  31. * </p>
  32. *
  33. * @author
  34. * @since 2022-01-19
  35. */
  36. @Slf4j
  37. @Service
  38. public class SceneUploadServiceImpl implements ISceneUploadService {
  39. @Autowired
  40. private SceneService sceneService;
  41. @Autowired
  42. private SceneFileMappingService sceneFileMappingService;
  43. // @Resource
  44. // private FdfsUtil fdfsUtil;
  45. @Resource FYunFileService fYunFileService;
  46. @Override
  47. public String uploads(String imgData,String fileName,String blzType,MultipartFile[] files,
  48. String sceneCode,Integer type, String uploadPath, Integer subgroup) throws Exception{
  49. List<MultipartFile> multipartFiles = new ArrayList<>();
  50. if(StringUtils.isNotBlank(imgData)){
  51. MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(imgData);
  52. multipartFiles.add(file);
  53. }
  54. if(files !=null && files.length >0){
  55. multipartFiles.addAll(Arrays.asList(files));
  56. }
  57. return this.uploadFiles(fileName,blzType,multipartFiles,sceneCode,type,uploadPath, subgroup);
  58. }
  59. @Override
  60. public ResultData delete(DeleteFileParamVO param) throws Exception {
  61. Scene scenePlus = sceneService.getByNum(param.getNum(), param.getSubgroup());
  62. if(Objects.isNull(scenePlus)){
  63. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  64. }
  65. String userEditPath = String.format(UploadFilePath.USER_VIEW_PATH, param.getNum());
  66. List<String> filePaths = new ArrayList<>();
  67. Set<String> hasDelete = new HashSet<>();
  68. for (String fileName : param.getFileNames()) {
  69. if(hasDelete.contains(fileName)){
  70. continue;
  71. }
  72. hasDelete.add(fileName);
  73. String key = userEditPath + fileName;
  74. filePaths.add(key);
  75. fYunFileService.deleteFile(param.getNum(), param.getSubgroup(), key);
  76. }
  77. return ResultData.ok();
  78. }
  79. public String uploadFiles(String sendFileName,String bizType,List<MultipartFile> files,
  80. String num, Integer type, String uploadPath, Integer subgroup) throws Exception{
  81. if (StrUtil.isEmpty(num))
  82. throw new BusinessException(ErrorCode.PARAM_REQUIRED, "num");
  83. if(CollUtil.isEmpty(files))
  84. throw new BusinessException(ErrorCode.PARAM_REQUIRED, "files");
  85. if(StrUtil.isEmpty(bizType))
  86. throw new BusinessException(ErrorCode.PARAM_REQUIRED, "bizType");
  87. Scene scene = sceneService.getByNum(num, subgroup);
  88. if(Objects.isNull(scene))
  89. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  90. List<String> urlList = new ArrayList<>();
  91. for (MultipartFile file : files) {
  92. String fileName = file.getOriginalFilename();
  93. // 获取文件后缀
  94. String prefix = fileName.substring(fileName.lastIndexOf("."));
  95. // String path = String.format(ConstantFilePath.SCENE_TMP_PATH_V4, num).concat(UUID.randomUUID().toString()).concat(prefix);
  96. File tempFile = File.createTempFile(UUID.randomUUID().toString() ,prefix);
  97. // File tempFile = FileUtil.createTempFile(prefix, true);
  98. String path = tempFile.getAbsolutePath();
  99. // FileUtil.mkParentDirs(path);
  100. file.transferTo(tempFile);
  101. String realFileName = fileName;
  102. if(files.size() ==1 && StringUtils.isNotBlank(sendFileName)){
  103. realFileName = sendFileName ;
  104. }
  105. String oldExtName = cn.hutool.core.io.FileUtil.extName(realFileName);
  106. String newExtName = oldExtName.toLowerCase();
  107. realFileName = realFileName.substring(0, realFileName.lastIndexOf(oldExtName)) + newExtName;
  108. String key = StrUtil.isNotBlank(uploadPath) ? uploadPath : (String.format(UploadFilePath.USER_VIEW_PATH , RedisKey.getNumStr(num, subgroup)) + realFileName);
  109. fYunFileService.uploadFile(num, subgroup, path, key);
  110. urlList.add(realFileName);
  111. FileUtil.del(path);
  112. //添加记录
  113. }
  114. StringBuilder returnString = new StringBuilder();
  115. for (String res : urlList) {
  116. if(StringUtils.isNotBlank(returnString)){
  117. returnString.append(",");
  118. }
  119. returnString.append(res);
  120. }
  121. return returnString.toString();
  122. }
  123. // private void updateFileByPreFix(String sceneCode, String blzType, String bucket) {
  124. // LambdaQueryWrapper<SceneUpload> queryWrapper = new LambdaQueryWrapper<>();
  125. // queryWrapper.eq(SceneUpload::getNum,sceneCode)
  126. // .eq(SceneUpload::getBizType,blzType);
  127. // List<SceneUpload> list = this.list(queryWrapper);
  128. //
  129. // if(list != null && list.size() >0){
  130. // for (SceneUpload sceneUpload : list) {
  131. // try {
  132. // fYunFileService.deleteFile(bucket, sceneUpload.getFilePath());
  133. // this.removeEntity(sceneUpload);
  134. // }catch (Exception e){
  135. // log.error(sceneUpload.getFilePath()+"删除oss文件失败",e);
  136. // }
  137. // }
  138. // }
  139. // }
  140. //
  141. // private void removeEntity(SceneUpload sceneUpload) {
  142. // sceneUpload.setRecStatus(RecStatus.VALID.code());
  143. // this.updateById(sceneUpload);
  144. // }
  145. //
  146. // private void saveData(String sceneCode, String ossPath, String bizType,Long userId) {
  147. // SceneUpload sceneUpload = new SceneUpload();
  148. // sceneUpload.setNum(sceneCode);
  149. // sceneUpload.setFilePath(ossPath);
  150. // sceneUpload.setBizType(bizType);
  151. // sceneUpload.setUploadUser(userId);
  152. // this.save(sceneUpload);
  153. // }
  154. //
  155. // @Override
  156. // public String uploadContent(UploadContentParamVO param) throws Exception {
  157. //
  158. // String ossPath = param.getOssPath();
  159. // String fileName = ossPath.substring(ossPath.lastIndexOf("/") + 1);
  160. // if(StrUtil.isEmpty(fileName)){
  161. // throw new BusinessException(ErrorCode.FAILURE_CODE_7012);
  162. // }
  163. // String suffix = fileName.substring(fileName.lastIndexOf("."));
  164. // if(StrUtil.isEmpty(suffix)){
  165. // throw new BusinessException(ErrorCode.FAILURE_CODE_7012);
  166. // }
  167. //
  168. // File tempFile = File.createTempFile(UUID.randomUUID().toString(), suffix);
  169. // cn.hutool.core.io.FileUtil.writeString(param.getContent(), tempFile, StandardCharsets.UTF_8);
  170. // fYunFileService.uploadFile(param.getBucket(), tempFile.getPath(), param.getOssPath());
  171. // tempFile.deleteOnExit();
  172. //
  173. // return this.ossUrlPrefix + param.getOssPath();
  174. // }
  175. }