DownloadTourVideoServiceImpl.java 5.0 KB

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