FileMetaTest.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import cn.reghao.dfs.store.DfsStoreApplication;
  2. import cn.reghao.dfs.store.db.mapper.FileMetaMapper;
  3. import cn.reghao.dfs.store.model.po.FileMeta;
  4. import cn.reghao.jutil.jdk.db.Page;
  5. import cn.reghao.jutil.jdk.security.DigestUtil;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.apache.commons.io.FileUtils;
  8. import org.junit.Test;
  9. import org.junit.runner.RunWith;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.boot.test.context.SpringBootTest;
  12. import org.springframework.test.context.ActiveProfiles;
  13. import org.springframework.test.context.junit4.SpringRunner;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.nio.file.FileVisitResult;
  17. import java.nio.file.FileVisitor;
  18. import java.nio.file.Files;
  19. import java.nio.file.Path;
  20. import java.nio.file.attribute.BasicFileAttributes;
  21. import java.security.NoSuchAlgorithmException;
  22. import java.util.List;
  23. /**
  24. * @author reghao
  25. * @date 2023-03-22 16:19:12
  26. */
  27. @Slf4j
  28. @ActiveProfiles("dev")
  29. @SpringBootTest(classes = DfsStoreApplication.class)
  30. @RunWith(SpringRunner.class)
  31. public class FileMetaTest {
  32. @Autowired
  33. FileMetaMapper fileMetaMapper;
  34. // 正则表达式查询测试
  35. public void regexpTest() {
  36. String bucket = "";
  37. String prefix = "abc/d/";
  38. String startAfter = "abc/d/e";
  39. Integer maxKeys = 10;
  40. StringBuilder regex = new StringBuilder();
  41. regex.append("^").append(prefix).append("([^/])+/?$");
  42. if (startAfter.isBlank()) {
  43. fileMetaMapper.findAll0(bucket, maxKeys, regex.toString());
  44. } else {
  45. fileMetaMapper.findAll2(bucket, prefix, startAfter, maxKeys);
  46. }
  47. }
  48. /**
  49. * 分页处理
  50. *
  51. * @param
  52. * @return
  53. * @date 2023-05-30 00:26:07
  54. */
  55. @Test
  56. public void processByPage() {
  57. int pageSize = 10_000;
  58. int pageNumber = 1;
  59. Page page = new Page(pageNumber, pageSize);
  60. List<FileMeta> list = fileMetaMapper.findFileMetaByPage(page);
  61. while (!list.isEmpty()) {
  62. list.forEach(fileMeta -> {
  63. });
  64. pageNumber++;
  65. page = new Page(pageNumber, pageSize);
  66. list = fileMetaMapper.findFileMetaByPage(page);
  67. log.info("page -> {}", pageNumber);
  68. }
  69. }
  70. public void walkDir(Path path) throws IOException {
  71. Files.walkFileTree(path, new FileVisitor<>() {
  72. @Override
  73. public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
  74. return FileVisitResult.CONTINUE;
  75. }
  76. @Override
  77. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  78. String absolutePath = file.toString();
  79. process(absolutePath);
  80. return FileVisitResult.CONTINUE;
  81. }
  82. @Override
  83. public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
  84. return FileVisitResult.CONTINUE;
  85. }
  86. @Override
  87. public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
  88. return FileVisitResult.CONTINUE;
  89. }
  90. });
  91. }
  92. private void process(String absolutePath) {
  93. try {
  94. String sha256sum = DigestUtil.sha256sum(absolutePath);
  95. FileMeta fileMeta = fileMetaMapper.findBySha256sum(sha256sum);
  96. if (fileMeta != null) {
  97. FileUtils.deleteQuietly(new File(absolutePath));
  98. } else {
  99. log.info("{} 在 db 中不存在", absolutePath);
  100. }
  101. } catch (IOException | NoSuchAlgorithmException e) {
  102. e.printStackTrace();
  103. }
  104. }
  105. @Test
  106. public void deleteFile() throws IOException {
  107. String baseDir = "/home/reghao/mnt/porn/0.已完成";
  108. baseDir = "/run/media/reghao/7c43343d-c1e2-4e68-bc18-89bd3f61b0c8/spider/porn/";
  109. Path path = Path.of(baseDir);
  110. walkDir(path);
  111. }
  112. }