SceneServiceImpl.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.fdkankan.scene.service.impl;
  2. import cn.hutool.core.img.ImgUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.lang.UUID;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.alibaba.fastjson.JSON;
  7. import com.fdkankan.common.constant.CommonOperStatus;
  8. import com.fdkankan.common.constant.ErrorCode;
  9. import com.fdkankan.common.constant.SceneConstant;
  10. import com.fdkankan.common.exception.BusinessException;
  11. import com.fdkankan.common.util.DateExtUtil;
  12. import com.fdkankan.common.util.FileUtils;
  13. import com.fdkankan.model.constants.ConstantFilePath;
  14. import com.fdkankan.redis.constant.RedisKey;
  15. import com.fdkankan.redis.util.RedisUtil;
  16. import com.fdkankan.scene.bean.BodySegmentStatusBean;
  17. import com.fdkankan.scene.entity.ScenePlus;
  18. import com.fdkankan.scene.oss.OssUtil;
  19. import com.fdkankan.scene.service.IScenePlusService;
  20. import com.fdkankan.scene.service.ISceneService;
  21. import com.fdkankan.scene.util.OssBodySegmentUtil;
  22. import com.fdkankan.web.response.ResultData;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.beans.factory.annotation.Value;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.web.multipart.MultipartFile;
  29. import javax.imageio.ImageIO;
  30. import java.awt.*;
  31. import java.awt.image.BufferedImage;
  32. import java.io.File;
  33. import java.io.IOException;
  34. import java.math.BigDecimal;
  35. import java.util.*;
  36. import java.util.List;
  37. @Slf4j
  38. @Service("sceneService")
  39. public class SceneServiceImpl implements ISceneService {
  40. @Value("${queue.bodySegment:body-segment}")
  41. private String queueName;
  42. @Value("${oss.bodySegment.bucket:4dkankan-huadong}")
  43. private String bodySegmentBucket;
  44. @Value("${oss.bodySegment.point:oss-cn-shanghai.aliyuncs.com}")
  45. private String bodySegmentHost;
  46. @Autowired
  47. private OssBodySegmentUtil ossBodySegmentUtil;
  48. @Autowired
  49. private RedisUtil redisUtil;
  50. @Autowired
  51. private OssUtil ossUtil;
  52. @Autowired
  53. private IScenePlusService scenePlusService;
  54. @Override
  55. public ResultData uploadBodySegment(MultipartFile file, Integer rotate) throws Exception {
  56. if(!FileUtils.checkFileSizeIsLimit(file.getSize(), 10, "M")){
  57. throw new BusinessException(ErrorCode.FAILURE_CODE_4003, "10M");
  58. }
  59. String uuid = UUID.randomUUID().toString();
  60. String fileName = file.getOriginalFilename();
  61. String extName = fileName.substring(fileName.lastIndexOf("."));
  62. File tempFile = File.createTempFile(uuid, extName);
  63. file.transferTo(tempFile);
  64. //判断是否需要旋转
  65. if(Objects.nonNull(rotate)){
  66. Image rotateImg = ImgUtil.rotate(ImageIO.read(tempFile), rotate);
  67. File tempRotateFile = File.createTempFile(uuid + "-rotate", extName);
  68. ImgUtil.write(rotateImg, tempRotateFile);
  69. tempFile = tempRotateFile;
  70. }
  71. //校验像素
  72. BufferedImage bufferedImage = ImgUtil.read(tempFile.getPath());
  73. Float scale = 1F;
  74. Float widthScale = 1F;
  75. Float heightScale = 1F;
  76. int width = bufferedImage.getWidth();
  77. int height = bufferedImage.getHeight();
  78. if(width > 2000){
  79. widthScale = new BigDecimal(2000).divide(new BigDecimal(width),5, BigDecimal.ROUND_DOWN).floatValue();
  80. }
  81. if(height > 2000){
  82. heightScale = new BigDecimal(2000).divide(new BigDecimal(height),5, BigDecimal.ROUND_DOWN).floatValue();
  83. }
  84. scale = widthScale > heightScale ? heightScale : widthScale;
  85. ImgUtil.scale(new File(tempFile.getPath()), new File(tempFile.getPath()), scale);
  86. String orgImgOssPath = "body_segment/original/" + tempFile.getName();
  87. ossBodySegmentUtil.uploadOss(tempFile.getPath(), orgImgOssPath);
  88. // fYunFileService.uploadFile(bodySegmentBucket, tempFile.getPath(), orgImgOssPath);
  89. BodySegmentStatusBean bodySegmentStatusBean = BodySegmentStatusBean.builder().uuid(uuid).status(CommonOperStatus.WAITING.code()).build();
  90. redisUtil.set(String.format(RedisKey.SCENE_BODY_SEGMENT, uuid), JSON.toJSONString(bodySegmentStatusBean), RedisKey.CAMERA_EXPIRE_7_TIME);
  91. Map<String, String> map = new HashMap<>();
  92. map.put("uuid", uuid);
  93. map.put("imgUrl", "https://" + bodySegmentBucket + "." + bodySegmentHost + "/" + orgImgOssPath);
  94. // rabbitMqProducer.sendByWorkQueue(queueName, map);
  95. return ResultData.ok(uuid);
  96. }
  97. public static void main(String[] args) throws IOException {
  98. Image rotateImg = ImgUtil.rotate(ImageIO.read(FileUtil.file("C:\\Users\\dsx\\Desktop\\IMG_6633.HEIC.JPG")), 90);
  99. ImgUtil.write(rotateImg, FileUtil.file("C:\\Users\\dsx\\Desktop\\IMG_6633.HEIC_2.JPG"));
  100. }
  101. @Override
  102. public void bodySegmentHandler(String imgUrl, String uuid) {
  103. String progress = redisUtil.hget(RedisKey.SCENE_BODY_SEGMENT, uuid);
  104. BodySegmentStatusBean bodySegmentStatusBean = null;
  105. try {
  106. if(StrUtil.isEmpty(progress)){
  107. bodySegmentStatusBean = JSON.parseObject(progress, BodySegmentStatusBean.class);
  108. }
  109. if(Objects.isNull(bodySegmentStatusBean)){
  110. bodySegmentStatusBean = new BodySegmentStatusBean();
  111. bodySegmentStatusBean.setUuid(uuid);
  112. }
  113. String dir = ConstantFilePath.BASE_PATH + "/bodySegment/" +
  114. DateExtUtil.format(Calendar.getInstance().getTime(), DateExtUtil.dateStyle6);
  115. String fileName = uuid + ".png";
  116. String imgPath = dir + "/" + fileName;
  117. ossBodySegmentUtil.extracted(imgUrl, dir, fileName);
  118. if(!FileUtil.exist(imgPath)){
  119. throw new Exception("提取图片失败");
  120. }
  121. String targetOssImgPath = "body_segment/segment/" + uuid + ".png";
  122. ossUtil.uploadFile(targetOssImgPath, imgPath, false);
  123. bodySegmentStatusBean.setStatus(CommonOperStatus.SUCCESS.code());
  124. // bodySegmentStatusBean.setImageUrl(fYunFileConfig.getHost() + targetOssImgPath);
  125. redisUtil.set(String.format(RedisKey.SCENE_BODY_SEGMENT, uuid), JSON.toJSONString(bodySegmentStatusBean), RedisKey.CAMERA_EXPIRE_7_TIME);
  126. } catch (Exception e) {
  127. bodySegmentStatusBean.setStatus(CommonOperStatus.FAILD.code());
  128. redisUtil.set(String.format(RedisKey.SCENE_BODY_SEGMENT, uuid), JSON.toJSONString(bodySegmentStatusBean), RedisKey.CAMERA_EXPIRE_7_TIME);
  129. }finally {
  130. try {
  131. //免费版qps不能大于2,故休眠一秒
  132. Thread.sleep(1000L);
  133. } catch (InterruptedException e) {
  134. e.printStackTrace();
  135. }
  136. }
  137. }
  138. @Override
  139. public ResultData getBodySegmentStatus(String uuid) {
  140. String progress = redisUtil.get(String.format(RedisKey.SCENE_BODY_SEGMENT, uuid));
  141. if(StrUtil.isEmpty(progress)){
  142. throw new BusinessException(ErrorCode.FAILURE_CODE_5038);
  143. }
  144. BodySegmentStatusBean bodySegmentStatusBean = JSON.parseObject(progress, BodySegmentStatusBean.class);
  145. return ResultData.ok(bodySegmentStatusBean);
  146. }
  147. @Override
  148. public void delete(String sceneNum,Long userId) {
  149. if(StringUtils.isEmpty(sceneNum)){
  150. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  151. }
  152. String[] nums = sceneNum.split(",");
  153. List<String> numList = Arrays.asList(nums);
  154. List<ScenePlus> plusList = scenePlusService.getListByNums(numList);
  155. scenePlusService.deleteByList(plusList,userId);
  156. }
  157. @Override
  158. public void copyScene(String sceneNum,String userName) throws Exception {
  159. if(StringUtils.isEmpty(sceneNum)){
  160. throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
  161. }
  162. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(sceneNum);
  163. if(scenePlus== null){
  164. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  165. }
  166. String newNum = scene3dNumService.generateSceneNum(detailEntity.getType());
  167. Long sceneId = scenePlus.getId();
  168. Long newSceneId = null;
  169. if(scenePlus != null){ //v4场景复制
  170. log.info("场景复制--V4--OldNum:{},oldTitle:{},newNum:{}", scenePlus.getNum(),scenePlus.getTitle(),newNum);
  171. newSceneId = scenePlusService.copyV4Scene(scenePlus,newNum,detailEntity);
  172. }
  173. log.info("场景复制--完成--sceneId:{}",newSceneId);
  174. }
  175. }