TmpFileUrlService.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package cn.reghao.tnb.file.app.service;
  2. import cn.reghao.tnb.file.app.config.PathUrl;
  3. import cn.reghao.tnb.file.app.db.mapper.FileUrlMapper;
  4. import cn.reghao.tnb.file.app.db.mapper.FileUserMapper;
  5. import cn.reghao.tnb.file.app.model.po.FileUrl;
  6. import cn.reghao.tnb.file.app.model.vo.TmpFile;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.apache.commons.io.FileUtils;
  9. import org.springframework.stereotype.Service;
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.nio.file.*;
  13. import java.nio.file.attribute.BasicFileAttributes;
  14. import java.util.List;
  15. /**
  16. * @author reghao
  17. * @date 2022-05-23 23:42:06
  18. */
  19. @Slf4j
  20. @Service
  21. @Deprecated
  22. public class TmpFileUrlService {
  23. private final FileUserMapper fileUserMapper;
  24. private final FileUrlMapper fileUrlMapper;
  25. private final FileUrlService fileUrlService;
  26. public TmpFileUrlService(FileUserMapper fileUserMapper, FileUrlMapper fileUrlMapper, FileUrlService fileUrlService) {
  27. this.fileUserMapper = fileUserMapper;
  28. this.fileUrlMapper = fileUrlMapper;
  29. this.fileUrlService = fileUrlService;
  30. }
  31. public void start() {
  32. List<TmpFile> list = fileUserMapper.findTmpFile();
  33. for (TmpFile tmpFile : list) {
  34. String sha256sum = tmpFile.getSha256sum();
  35. long fileSize = tmpFile.getSize();
  36. String uploadId = tmpFile.getUploadId();
  37. String suffix = tmpFile.getSuffix();
  38. String fileId = tmpFile.getFileId();
  39. try {
  40. PathUrl pathUrl = fileUrlService.genVideoPathAndUrl(sha256sum, uploadId, fileSize, fileId, suffix);
  41. String blockId = pathUrl.getBlockId();
  42. String filePath = pathUrl.getFilePath();
  43. String url = pathUrl.getUrl();
  44. String path = pathUrl.getPath();
  45. List<FileUrl> list1 = fileUrlMapper.findByFileId(fileId);
  46. if (list1.isEmpty()) {
  47. continue;
  48. }
  49. FileUrl fileUrl = list1.get(0);
  50. String url1 = fileUrl.getUrl();
  51. String filePath1;
  52. if (url.contains("group")) {
  53. filePath1 = url1.replace("//static.reghao.cn/group0/node0", "/root/mnt");
  54. } else {
  55. filePath1 = url1.replace("//static.reghao.cn", "/root/mnt");
  56. }
  57. File source = new File(filePath1);
  58. File dest = new File(filePath);
  59. if (source.exists()) {
  60. FileUtils.copyFile(source, new File(filePath), true);
  61. fileUrlMapper.updateSetFileUrl(fileId, blockId, filePath);
  62. } else {
  63. log.error("source file {} not exist", filePath);
  64. }
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. log.info("start 执行完成...");
  70. }
  71. public void start1() {
  72. fileUrlMapper.findAll().forEach(fileUrl -> {
  73. String url = fileUrl.getUrl();
  74. String filePath;
  75. if (url.contains("group")) {
  76. filePath = url.replace("//static.reghao.cn/group0/node0", "/root/mnt");
  77. } else {
  78. filePath = url.replace("//static.reghao.cn", "/root/mnt");
  79. }
  80. File file = new File(filePath);
  81. if (!file.exists()) {
  82. log.error("{} not exist", filePath);
  83. } else {
  84. log.info("{} exist", filePath);
  85. }
  86. });
  87. log.info("start1 执行完成...");
  88. }
  89. }