FileMetaTest.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import cn.reghao.oss.api.dto.ObjectMeta;
  2. import cn.reghao.oss.sdk.model.dto.media.ImageInfo;
  3. import cn.reghao.oss.store.OssMediaApplication;
  4. import cn.reghao.oss.store.db.mapper.DataBlockMapper;
  5. import cn.reghao.oss.store.db.mapper.FileMetaMapper;
  6. import cn.reghao.oss.store.model.po.DataBlock;
  7. import com.drew.imaging.ImageMetadataReader;
  8. import com.drew.lang.GeoLocation;
  9. import com.drew.metadata.Metadata;
  10. import com.drew.metadata.exif.GpsDirectory;
  11. import com.fasterxml.jackson.databind.deser.std.StringArrayDeserializer;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.junit.Test;
  14. import org.junit.runner.RunWith;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.boot.test.context.SpringBootTest;
  17. import org.springframework.test.context.ActiveProfiles;
  18. import org.springframework.test.context.junit4.SpringRunner;
  19. import javax.swing.table.TableCellRenderer;
  20. import java.io.File;
  21. import java.util.Collection;
  22. import java.util.List;
  23. /**
  24. * @author reghao
  25. * @date 2023-07-16 19:19:12
  26. */
  27. @Slf4j
  28. @ActiveProfiles("dev")
  29. @SpringBootTest(classes = OssMediaApplication.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. @Test
  49. public void fileTest() {
  50. int pageSize = 10000;
  51. int nextId = 0;
  52. String prefix = "image/p/%";
  53. List<ObjectMeta> list = fileMetaMapper.findByPrefix(prefix, pageSize, nextId);
  54. while (!list.isEmpty()) {
  55. list.forEach(this::process);
  56. nextId = list.get(list.size()-1).getId();
  57. list = fileMetaMapper.findByPrefix(prefix, pageSize, nextId);
  58. log.info("nextId -> {}", nextId);
  59. }
  60. }
  61. private void process(ObjectMeta objectMeta) {
  62. String objectId = objectMeta.getObjectId();
  63. String absolutePath = objectMeta.getAbsolutePath();
  64. File file = new File(absolutePath);
  65. try {
  66. Metadata metadata = ImageMetadataReader.readMetadata(file);
  67. Collection<GpsDirectory> gpsDirectories = metadata.getDirectoriesOfType(GpsDirectory.class);
  68. for(GpsDirectory gps : gpsDirectories) {
  69. GeoLocation geoLocation = gps.getGeoLocation();
  70. if (geoLocation != null) {
  71. double longitude = geoLocation.getLongitude();
  72. double latitude = geoLocation.getLatitude();
  73. log.info("{}: ({}, {})", objectId, longitude, latitude);;
  74. }
  75. }
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. @Autowired
  81. DataBlockMapper dataBlockMapper;
  82. @Test
  83. public void dataBlockTest() {
  84. int pageSize = 10000;
  85. int nextId = 0;
  86. List<DataBlock> list = dataBlockMapper.findDataBlocks(pageSize, nextId);
  87. while (!list.isEmpty()) {
  88. // process DataBlocks
  89. nextId = list.get(list.size()-1).getId();
  90. list = dataBlockMapper.findDataBlocks(pageSize, nextId);
  91. log.info("nextId -> {}", nextId);
  92. }
  93. }
  94. }