CollectionServiceTest.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package com.izouma.nineth.service;
  2. import com.alibaba.fastjson.JSON;
  3. import com.izouma.nineth.ApplicationTests;
  4. import com.izouma.nineth.domain.BlindBoxItem;
  5. import com.izouma.nineth.domain.Collection;
  6. import com.izouma.nineth.domain.FileObject;
  7. import com.izouma.nineth.dto.CreateBlindBox;
  8. import com.izouma.nineth.enums.CollectionType;
  9. import com.izouma.nineth.exception.BusinessException;
  10. import com.izouma.nineth.repo.CollectionRepo;
  11. import com.izouma.nineth.repo.PrivilegeOptionRepo;
  12. import com.izouma.nineth.repo.UserRepo;
  13. import com.izouma.nineth.service.storage.StorageService;
  14. import com.izouma.nineth.utils.ImageUtils;
  15. import org.apache.commons.io.FileUtils;
  16. import org.apache.commons.io.FilenameUtils;
  17. import org.apache.commons.lang3.RandomStringUtils;
  18. import org.bytedeco.javacv.FFmpegFrameGrabber;
  19. import org.bytedeco.javacv.Frame;
  20. import org.bytedeco.javacv.Java2DFrameConverter;
  21. import org.junit.jupiter.api.Test;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import javax.imageio.ImageIO;
  24. import java.awt.image.BufferedImage;
  25. import java.io.*;
  26. import java.nio.file.Files;
  27. import java.text.SimpleDateFormat;
  28. import java.time.LocalDateTime;
  29. import java.util.*;
  30. import java.util.concurrent.atomic.AtomicInteger;
  31. import java.util.regex.Matcher;
  32. import java.util.regex.Pattern;
  33. import java.util.stream.Collectors;
  34. class CollectionServiceTest extends ApplicationTests {
  35. @Autowired
  36. private CollectionService collectionService;
  37. @Autowired
  38. private CollectionRepo collectionRepo;
  39. @Autowired
  40. private StorageService storageService;
  41. @Autowired
  42. private UserRepo userRepo;
  43. @Autowired
  44. private PrivilegeOptionRepo privilegeOptionRepo;
  45. @Autowired
  46. private SysConfigService sysConfigService;
  47. @Test
  48. void toDTO() {
  49. Collection collection = collectionRepo.findById(951L).get();
  50. assert collection.getPic() != null;
  51. }
  52. @Test
  53. public void batchUpload() throws IOException {
  54. AtomicInteger num = new AtomicInteger(1);
  55. List<BlindBoxItem> items = new ArrayList<>();
  56. String jsonStr = FileUtils.readFileToString(new File("/Users/qiufangchao/Desktop/mj.json"), "UTF-8");
  57. Arrays.stream(new File("/Users/qiufangchao/Desktop/majiang").listFiles())
  58. .filter(f -> !f.getName().contains(".DS_Store"))
  59. .parallel().forEach(file -> {
  60. try {
  61. String name = file.getName();
  62. Pattern p = Pattern.compile("(\\d+)-(\\d+)");
  63. Matcher matcher = p.matcher(name);
  64. matcher.find();
  65. String findname = matcher.group();
  66. Collection collection = JSON.parseObject(jsonStr, Collection.class);
  67. collection.setId(null);
  68. collection.setName("MAHJONGMAN 麻将侠: 十三幺 " + findname);
  69. collection.setStock(1);
  70. collection.setTotal(1);
  71. collection.setSale(0);
  72. collection.setMinterId(7150L);
  73. collection.setOnShelf(false);
  74. collection.setSalable(false);
  75. String thumbPath = "https://cdn.raex.vip/thumb_image/mahjongman/" + file.getName()
  76. .substring(0, file.getName().lastIndexOf(".")) + ".jpg";
  77. collection.setPic(Collections.singletonList(new FileObject("", "https://cdn.raex.vip/video/mahjongman/" + file.getName(), thumbPath, "video/mp4")));
  78. collectionRepo.save(collection);
  79. System.out.println("保存成功" + collection.getId());
  80. items.add(BlindBoxItem
  81. .builder()
  82. .collectionId(collection.getId())
  83. .total(1)
  84. .stock(1)
  85. .rare(false)
  86. .build());
  87. } catch (Exception e) {
  88. }
  89. });
  90. }
  91. @Test
  92. public void createBlindBox() throws IOException {
  93. List<Collection> items = collectionRepo.findByNameLike("OASISPUNK.EVO #%").stream().filter(i -> {
  94. int num = Integer.parseInt(i.getName().substring("OASISPUNK.EVO #".length()));
  95. return num > 0 && num <= 280;
  96. }).collect(Collectors.toList());
  97. String jsonStr = FileUtils.readFileToString(new File("/Users/qiufangchao/Desktop/evo.json"), "UTF-8");
  98. Collection blindBox = JSON.parseObject(jsonStr, Collection.class);
  99. blindBox.setType(CollectionType.BLIND_BOX);
  100. blindBox.setId(null);
  101. blindBox.setOnShelf(false);
  102. blindBox.setSalable(false);
  103. blindBox.setMinterId(7150L);
  104. blindBox.setNoSoldOut(true);
  105. blindBox.setName("OASISPUNK.EVO绿洲朋克进化版限定盲盒S3");
  106. blindBox.setPic(Arrays.asList(new FileObject(null, "https://raex-meta.oss-cn-shenzhen.aliyuncs.com/image/evos3/fm.jpg", null, null)));
  107. collectionService.createBlindBox(new CreateBlindBox(blindBox, items.stream().map(i -> {
  108. BlindBoxItem item = new BlindBoxItem();
  109. item.setCollectionId(i.getId());
  110. item.setTotal(1);
  111. return item;
  112. }).collect(Collectors.toList())));
  113. }
  114. public FileObject uploadFile(File file) throws IOException {
  115. String ext = Optional.of(FilenameUtils.getExtension(file.getName())).orElse("")
  116. .toLowerCase().replace("jpeg", "jpg");
  117. String basePath = Optional.ofNullable(Files.probeContentType(file.toPath())).orElse("application")
  118. .split("/")[0];
  119. String path = basePath + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
  120. + RandomStringUtils.randomAlphabetic(8)
  121. + "." + ext;
  122. return new FileObject("", storageService.uploadFromInputStream(new FileInputStream(file), path), null, ext);
  123. }
  124. @Test
  125. public void test1() {
  126. Long collectionId = 2570204L;
  127. Collection collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("无藏品"));
  128. if (!collection.isOnShelf() || !collection.isSalable()) {
  129. collectionId = null;
  130. } else if (collection.isScheduleSale()) {
  131. if (collection.getStartTime().isAfter(LocalDateTime.now())) {
  132. collectionId = null;
  133. }
  134. }
  135. System.out.println(collectionId);
  136. }
  137. @Test
  138. public void savePoint() {
  139. // collectionRepo.findById(206985L).orElseThrow(new BusinessException("无藏品"));
  140. collectionService.savePoint(3749128L, LocalDateTime.of(2022, 3, 23, 15, 0, 0));
  141. }
  142. @Test
  143. public void uploadMp4() {
  144. Arrays.stream(new File("/Users/qiufangchao/Desktop/majiang").listFiles())
  145. .parallel()
  146. .filter(f -> !f.getName().contains(".DS_Store"))
  147. .forEach(file -> {
  148. System.out.println(file.getName());
  149. try {
  150. String url = storageService.uploadFromInputStream(new FileInputStream(file), "video/mahjongman/" + file.getName());
  151. String thumbPath = "thumb_image/mahjongman/" + file.getName()
  152. .substring(0, file.getName().lastIndexOf(".")) + ".jpg";
  153. FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(file);
  154. Java2DFrameConverter frameConverter = new Java2DFrameConverter();
  155. try {
  156. frameGrabber.start();
  157. Frame frame = null;
  158. while (frame == null) {
  159. frame = frameGrabber.grabKeyFrame();
  160. }
  161. Objects.requireNonNull(frame, "获取视频缩略图失败");
  162. BufferedImage thumbBi = frameConverter.convert(frame);
  163. BufferedImage thumbResized = ImageUtils.resizeJpg(thumbBi, 1000, 1000, false);
  164. ByteArrayOutputStream os = new ByteArrayOutputStream();
  165. ImageIO.write(thumbResized, "jpg", os);
  166. InputStream is = new ByteArrayInputStream(os.toByteArray());
  167. String thumbUrl = storageService.uploadFromInputStream(is, thumbPath);
  168. } catch (Exception e) {
  169. e.printStackTrace();
  170. } finally {
  171. frameGrabber.stop();
  172. }
  173. } catch (Exception e) {
  174. }
  175. });
  176. }
  177. @Test
  178. public void test2() {
  179. String name = "/Users/qiufangchao/Desktop/majiang/bai1-1.mp4";
  180. Pattern p = Pattern.compile("(\\d+)-(\\d+)");
  181. Matcher matcher = p.matcher(name);
  182. matcher.find();
  183. String firstHour = matcher.group();
  184. System.out.println(matcher.start());
  185. System.out.println(firstHour);
  186. }
  187. }