DownloadTourVideoServiceImpl.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package com.fdkankan.scene.service.impl;
  2. import cn.hutool.core.exceptions.ExceptionUtil;
  3. import cn.hutool.core.lang.UUID;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.fdkankan.common.constant.ErrorCode;
  7. import com.fdkankan.common.exception.BusinessException;
  8. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  9. import com.fdkankan.model.constants.ConstantFilePath;
  10. import com.fdkankan.model.constants.UploadFilePath;
  11. import com.fdkankan.model.utils.CreateObjUtil;
  12. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  13. import com.fdkankan.scene.entity.DownloadTourVideo;
  14. import com.fdkankan.scene.mapper.IDownloadTourVideoMapper;
  15. import com.fdkankan.scene.service.IDownloadTourVideoService;
  16. import com.fdkankan.scene.vo.DownloadTourVideoVO;
  17. import com.fdkankan.web.response.ResultData;
  18. import java.io.File;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import java.util.Objects;
  22. import lombok.SneakyThrows;
  23. import org.apache.http.HttpHeaders;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.beans.factory.annotation.Value;
  26. import org.springframework.stereotype.Service;
  27. import org.springframework.web.multipart.MultipartFile;
  28. /**
  29. * <p>
  30. * 导览视频转换记录表 服务实现类
  31. * </p>
  32. *
  33. * @author
  34. * @since 2022-10-12
  35. */
  36. @Service
  37. public class DownloadTourVideoServiceImpl extends ServiceImpl<IDownloadTourVideoMapper, DownloadTourVideo> implements IDownloadTourVideoService {
  38. @Value("${queue.scene.transfer-tour-video}")
  39. private String downloadTourVideoQueue;
  40. @Value("${fyun.type}")
  41. private String type;
  42. @Value("${fyun.bucket:4dkankan}")
  43. private String bucket;
  44. @Autowired
  45. private RabbitMqProducer rabbitMqProducer;
  46. @Autowired
  47. private FYunFileServiceInterface fYunFileService;
  48. @Override
  49. public DownloadTourVideo getWaitingByNum(String num) {
  50. return this.getOne(
  51. new LambdaQueryWrapper<DownloadTourVideo>().eq(DownloadTourVideo::getNum, num)
  52. .eq(DownloadTourVideo::getState, 0));
  53. }
  54. @Override
  55. public void removeByNum(String num) {
  56. this.remove(new LambdaQueryWrapper<DownloadTourVideo>().eq(DownloadTourVideo::getNum, num));
  57. }
  58. @Override
  59. public ResultData uploadTourVideo(String num, MultipartFile file) throws Exception {
  60. //查询是否有任务正在执行,如果有,直接返回
  61. DownloadTourVideo waiting = this.getWaitingByNum(num);
  62. if(Objects.nonNull(waiting)){
  63. throw new BusinessException(ErrorCode.FAILURE_CODE_5064);
  64. }
  65. String uuid = UUID.randomUUID().toString();
  66. String fileName = file.getOriginalFilename();
  67. String extName = cn.hutool.core.io.FileUtil.extName(fileName);
  68. String tempFileName = uuid + "." + extName;
  69. String srcPath = ConstantFilePath.SCENE_V4_PATH + num + "/tour/" + tempFileName;
  70. File tempFile = new File(srcPath);
  71. if(!tempFile.getParentFile().exists()){
  72. tempFile.getParentFile().mkdirs();
  73. }
  74. file.transferTo(tempFile);
  75. //先将旧纪录置为无效
  76. this.removeByNum(num);
  77. //写入新记录
  78. DownloadTourVideo downloadTourVideo = new DownloadTourVideo();
  79. downloadTourVideo.setNum(num);
  80. downloadTourVideo.setFileName(fileName);
  81. downloadTourVideo.setLocalPath(srcPath);
  82. this.save(downloadTourVideo);
  83. //发送mq
  84. rabbitMqProducer.sendByWorkQueue(downloadTourVideoQueue, downloadTourVideo);
  85. return ResultData.ok();
  86. }
  87. @Override
  88. public void transferTourVideo(DownloadTourVideo downloadTourVideo) {
  89. String destPath = null;
  90. try {
  91. String destFileName = UUID.randomUUID().toString() + ".mp4";
  92. destPath = ConstantFilePath.SCENE_V4_PATH + downloadTourVideo.getNum() + "tour/" + destFileName;
  93. File destFile = new File(destPath);
  94. if(!destFile.getParentFile().exists()){
  95. destFile.getParentFile().mkdir();
  96. }
  97. String srcPath = downloadTourVideo.getLocalPath();
  98. CreateObjUtil.formatMp4(srcPath, destPath);
  99. //上传到oss
  100. String ossPath = String.format(UploadFilePath.DOWNLOADS_TOUR_VIDEO, downloadTourVideo.getNum()) + downloadTourVideo.getFileName();
  101. Map<String, String> headers = new HashMap<>();
  102. headers.put(HttpHeaders.CONTENT_TYPE, "application/octet-stream");
  103. fYunFileService.uploadFile(bucket, destPath, ossPath, headers);
  104. downloadTourVideo.setDownloadPath(ossPath);
  105. downloadTourVideo.setState(1);
  106. this.updateById(downloadTourVideo);
  107. }catch (Exception e) {
  108. log.error("导览视频转换失败,num:" + downloadTourVideo.getNum() + "源路径:" + downloadTourVideo.getLocalPath(), e);
  109. downloadTourVideo.setReason(ExceptionUtil.stacktraceToString(e, 3000));
  110. downloadTourVideo.setState(2);
  111. this.updateById(downloadTourVideo);
  112. }
  113. }
  114. @Override
  115. public ResultData downloadTourVideo(String num) {
  116. DownloadTourVideoVO result = new DownloadTourVideoVO();
  117. DownloadTourVideo downloadTourVideo = this.getOne(new LambdaQueryWrapper<DownloadTourVideo>().eq(DownloadTourVideo::getNum, num));
  118. if(Objects.isNull(downloadTourVideo)){
  119. result.setStatus(0);
  120. }else{
  121. result.setStatus(1);
  122. result.setTransferStatus(downloadTourVideo.getState());
  123. result.setPath(downloadTourVideo.getDownloadPath());
  124. }
  125. return ResultData.ok(result);
  126. }
  127. }