DownloadTourVideoServiceImpl.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package com.fdkankan.scene.service.impl;
  2. import cn.hutool.core.exceptions.ExceptionUtil;
  3. import cn.hutool.core.lang.UUID;
  4. import com.alibaba.fastjson.JSON;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.fdkankan.common.constant.ConstantFilePath;
  7. import com.fdkankan.common.constant.ErrorCode;
  8. import com.fdkankan.common.constant.UploadFilePath;
  9. import com.fdkankan.common.exception.BusinessException;
  10. import com.fdkankan.common.response.ResultData;
  11. import com.fdkankan.common.util.CreateObjUtil;
  12. import com.fdkankan.fyun.constant.StorageType;
  13. import com.fdkankan.fyun.oss.UploadToOssUtil;
  14. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  15. import com.fdkankan.scene.entity.DownloadTourVideo;
  16. import com.fdkankan.scene.mapper.IDownloadTourVideoMapper;
  17. import com.fdkankan.scene.service.IDownloadTourVideoService;
  18. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  19. import com.fdkankan.scene.vo.DownloadTourVideoVO;
  20. import java.io.File;
  21. import java.util.Date;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import java.util.Objects;
  25. import org.apache.http.HttpHeaders;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.beans.factory.annotation.Value;
  28. import org.springframework.stereotype.Service;
  29. import org.springframework.web.multipart.MultipartFile;
  30. /**
  31. * <p>
  32. * 导览视频转换记录表 服务实现类
  33. * </p>
  34. *
  35. * @author
  36. * @since 2022-10-12
  37. */
  38. @Service
  39. public class DownloadTourVideoServiceImpl extends ServiceImpl<IDownloadTourVideoMapper, DownloadTourVideo> implements IDownloadTourVideoService {
  40. @Value("${queue.scene.transfer-tour-video}")
  41. private String downloadTourVideoQueue;
  42. @Autowired
  43. private RabbitMqProducer rabbitMqProducer;
  44. @Autowired
  45. private UploadToOssUtil uploadToOssUtil;
  46. @Value("${upload.type}")
  47. private String type;
  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 = cn.hutool.core.lang.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().mkdir();
  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. if(StorageType.OSS.code().equals(type)){
  104. uploadToOssUtil.uploadOssWithHeaders(destPath, ossPath, headers);
  105. }else{
  106. uploadToOssUtil.uploadAwsWithHeaders(destPath, ossPath, headers);
  107. }
  108. downloadTourVideo.setDownloadPath(ossPath);
  109. downloadTourVideo.setState(1);
  110. this.updateById(downloadTourVideo);
  111. }catch (Exception e) {
  112. log.error("导览视频转换失败,num:" + downloadTourVideo.getNum() + "源路径:" + downloadTourVideo.getLocalPath(), e);
  113. downloadTourVideo.setReason(ExceptionUtil.stacktraceToString(e, 3000));
  114. downloadTourVideo.setState(2);
  115. this.updateById(downloadTourVideo);
  116. }
  117. }
  118. @Override
  119. public ResultData downloadTourVideo(String num) {
  120. DownloadTourVideoVO result = new DownloadTourVideoVO();
  121. DownloadTourVideo downloadTourVideo = this.getOne(new LambdaQueryWrapper<DownloadTourVideo>().eq(DownloadTourVideo::getNum, num));
  122. if(Objects.isNull(downloadTourVideo)){
  123. result.setStatus(0);
  124. }else{
  125. result.setStatus(1);
  126. result.setTransferStatus(downloadTourVideo.getState());
  127. result.setPath(downloadTourVideo.getDownloadPath());
  128. }
  129. return ResultData.ok(result);
  130. }
  131. }