CollectionServiceTest.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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/mugen_mu.json"), "UTF-8");
  57. Arrays.stream(new File("/Users/qiufangchao/Desktop/mugen_mu").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+)");
  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("MUGEN无限:未央宗#" + num.getAndIncrement());
  69. collection.setStock(1);
  70. collection.setTotal(1);
  71. collection.setSale(0);
  72. collection.setMinterId(5868950L);
  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/nft/mugen_mu/" + file.getName(), null, "image/png")));
  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<Long> arr = Arrays.asList(6862110L, 6862511L, 6862516L, 6862533L, 6862689L, 6862792L, 6862867L, 6863009L, 6863047L,
  94. 6863188L, 6863438L, 6863449L, 6863588L, 6863608L, 6863671L, 6863745L, 6863760L, 6863938L, 6863954L, 6864008L, 6864081L);
  95. List<Collection> items = collectionRepo.findByNameLike("MUGEN无限:未央宗#%").stream().filter(i -> {
  96. int num = Integer.parseInt(i.getName().substring("MUGEN无限:未央宗#".length()));
  97. return num > 0 && num <= 2000 && !arr.contains(i.getId());
  98. }).limit(1879).collect(Collectors.toList());
  99. String jsonStr = FileUtils.readFileToString(new File("/Users/qiufangchao/Desktop/mugen_mu.json"), "UTF-8");
  100. Collection blindBox = JSON.parseObject(jsonStr, Collection.class);
  101. blindBox.setType(CollectionType.BLIND_BOX);
  102. blindBox.setId(null);
  103. blindBox.setOnShelf(false);
  104. blindBox.setSalable(false);
  105. blindBox.setMinterId(5868950L);
  106. blindBox.setNoSoldOut(true);
  107. blindBox.setMaxCount(1);
  108. blindBox.setName("MUGEN无限未央宗数字艺术品盲盒");
  109. blindBox.setPic(Arrays.asList(new FileObject(null, "https://cdn.raex.vip/nft/2022-04-16-19-24-54WTJLmsmR.jpg", null, "image/jpeg")));
  110. collectionService.createBlindBox(new CreateBlindBox(blindBox, items.stream().map(i -> {
  111. BlindBoxItem item = new BlindBoxItem();
  112. item.setCollectionId(i.getId());
  113. item.setTotal(1);
  114. return item;
  115. }).collect(Collectors.toList())));
  116. }
  117. public FileObject uploadFile(File file) throws IOException {
  118. String ext = Optional.of(FilenameUtils.getExtension(file.getName())).orElse("")
  119. .toLowerCase().replace("jpeg", "jpg");
  120. String basePath = Optional.ofNullable(Files.probeContentType(file.toPath())).orElse("application")
  121. .split("/")[0];
  122. String path = basePath + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
  123. + RandomStringUtils.randomAlphabetic(8)
  124. + "." + ext;
  125. return new FileObject("", storageService.uploadFromInputStream(new FileInputStream(file), path), null, ext);
  126. }
  127. @Test
  128. public void test1() {
  129. Long collectionId = 2570204L;
  130. Collection collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("无藏品"));
  131. if (!collection.isOnShelf() || !collection.isSalable()) {
  132. collectionId = null;
  133. } else if (collection.isScheduleSale()) {
  134. if (collection.getStartTime().isAfter(LocalDateTime.now())) {
  135. collectionId = null;
  136. }
  137. }
  138. System.out.println(collectionId);
  139. }
  140. @Test
  141. public void savePoint() {
  142. // collectionRepo.findById(206985L).orElseThrow(new BusinessException("无藏品"));
  143. collectionService.savePoint(3749128L, LocalDateTime.of(2022, 3, 23, 15, 0, 0));
  144. }
  145. @Test
  146. public void uploadMp4() {
  147. Arrays.stream(new File("/Users/qiufangchao/Desktop/majiang").listFiles())
  148. .parallel()
  149. .filter(f -> !f.getName().contains(".DS_Store"))
  150. .forEach(file -> {
  151. System.out.println(file.getName());
  152. try {
  153. String url = storageService.uploadFromInputStream(new FileInputStream(file), "video/mahjongman/" + file.getName());
  154. String thumbPath = "thumb_image/mahjongman/" + file.getName()
  155. .substring(0, file.getName().lastIndexOf(".")) + ".jpg";
  156. FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(file);
  157. Java2DFrameConverter frameConverter = new Java2DFrameConverter();
  158. try {
  159. frameGrabber.start();
  160. Frame frame = null;
  161. while (frame == null) {
  162. frame = frameGrabber.grabKeyFrame();
  163. }
  164. Objects.requireNonNull(frame, "获取视频缩略图失败");
  165. BufferedImage thumbBi = frameConverter.convert(frame);
  166. BufferedImage thumbResized = ImageUtils.resizeJpg(thumbBi, 1000, 1000, false);
  167. ByteArrayOutputStream os = new ByteArrayOutputStream();
  168. ImageIO.write(thumbResized, "jpg", os);
  169. InputStream is = new ByteArrayInputStream(os.toByteArray());
  170. String thumbUrl = storageService.uploadFromInputStream(is, thumbPath);
  171. } catch (Exception e) {
  172. e.printStackTrace();
  173. } finally {
  174. frameGrabber.stop();
  175. }
  176. } catch (Exception e) {
  177. }
  178. });
  179. }
  180. @Test
  181. public void test2() {
  182. String name = "/Users/qiufangchao/Desktop/majiang/bai1-1.mp4";
  183. Pattern p = Pattern.compile("(\\d+)-(\\d+)");
  184. Matcher matcher = p.matcher(name);
  185. matcher.find();
  186. String firstHour = matcher.group();
  187. System.out.println(matcher.start());
  188. System.out.println(firstHour);
  189. }
  190. }