SceneUploadServiceImpl.java 8.2 KB

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