package com.izouma.nineth.service; import com.alibaba.fastjson.JSON; import com.izouma.nineth.ApplicationTests; import com.izouma.nineth.domain.BlindBoxItem; import com.izouma.nineth.domain.Collection; import com.izouma.nineth.domain.FileObject; import com.izouma.nineth.dto.CreateBlindBox; import com.izouma.nineth.enums.CollectionType; import com.izouma.nineth.exception.BusinessException; import com.izouma.nineth.repo.CollectionRepo; import com.izouma.nineth.repo.PrivilegeOptionRepo; import com.izouma.nineth.repo.UserRepo; import com.izouma.nineth.service.storage.StorageService; import com.izouma.nineth.utils.ImageUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.RandomStringUtils; import org.bytedeco.javacv.FFmpegFrameGrabber; import org.bytedeco.javacv.Frame; import org.bytedeco.javacv.Java2DFrameConverter; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.nio.file.Files; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; class CollectionServiceTest extends ApplicationTests { @Autowired private CollectionService collectionService; @Autowired private CollectionRepo collectionRepo; @Autowired private StorageService storageService; @Autowired private UserRepo userRepo; @Autowired private PrivilegeOptionRepo privilegeOptionRepo; @Autowired private SysConfigService sysConfigService; @Test void toDTO() { Collection collection = collectionRepo.findById(951L).get(); assert collection.getPic() != null; } @Test public void batchUpload() throws IOException { AtomicInteger num = new AtomicInteger(1); List items = new ArrayList<>(); String jsonStr = FileUtils.readFileToString(new File("/Users/qiufangchao/Desktop/mugen_mu.json"), "UTF-8"); Arrays.stream(new File("/Users/qiufangchao/Desktop/mugen_mu").listFiles()) .filter(f -> !f.getName().contains(".DS_Store")) .parallel().forEach(file -> { try { // String name = file.getName(); // Pattern p = Pattern.compile("(\\d+)"); // Matcher matcher = p.matcher(name); // matcher.find(); // String findname = matcher.group(); Collection collection = JSON.parseObject(jsonStr, Collection.class); collection.setId(null); collection.setName("MUGEN无限:未央宗#" + num.getAndIncrement()); collection.setStock(1); collection.setTotal(1); collection.setSale(0); collection.setMinterId(5868950L); collection.setOnShelf(false); collection.setSalable(false); // String thumbPath = "https://cdn.raex.vip/thumb_image/mahjongman/" + file.getName() // .substring(0, file.getName().lastIndexOf(".")) + ".jpg"; collection.setPic(Collections.singletonList(new FileObject("", "https://cdn.raex.vip/nft/mugen_mu/" + file.getName(), null, "image/png"))); collectionRepo.save(collection); System.out.println("保存成功" + collection.getId()); items.add(BlindBoxItem .builder() .collectionId(collection.getId()) .total(1) .stock(1) .rare(false) .build()); } catch (Exception e) { } }); } @Test public void createBlindBox() throws IOException { List arr = Arrays.asList(6862110L, 6862511L, 6862516L, 6862533L, 6862689L, 6862792L, 6862867L, 6863009L, 6863047L, 6863188L, 6863438L, 6863449L, 6863588L, 6863608L, 6863671L, 6863745L, 6863760L, 6863938L, 6863954L, 6864008L, 6864081L); List items = collectionRepo.findByNameLike("MUGEN无限:未央宗#%").stream().filter(i -> { int num = Integer.parseInt(i.getName().substring("MUGEN无限:未央宗#".length())); return num > 0 && num <= 2000 && !arr.contains(i.getId()); }).limit(1879).collect(Collectors.toList()); String jsonStr = FileUtils.readFileToString(new File("/Users/qiufangchao/Desktop/mugen_mu.json"), "UTF-8"); Collection blindBox = JSON.parseObject(jsonStr, Collection.class); blindBox.setType(CollectionType.BLIND_BOX); blindBox.setId(null); blindBox.setOnShelf(false); blindBox.setSalable(false); blindBox.setMinterId(5868950L); blindBox.setNoSoldOut(true); blindBox.setMaxCount(1); blindBox.setName("MUGEN无限未央宗数字艺术品盲盒"); blindBox.setPic(Arrays.asList(new FileObject(null, "https://cdn.raex.vip/nft/2022-04-16-19-24-54WTJLmsmR.jpg", null, "image/jpeg"))); collectionService.createBlindBox(new CreateBlindBox(blindBox, items.stream().map(i -> { BlindBoxItem item = new BlindBoxItem(); item.setCollectionId(i.getId()); item.setTotal(1); return item; }).collect(Collectors.toList()))); } public FileObject uploadFile(File file) throws IOException { String ext = Optional.of(FilenameUtils.getExtension(file.getName())).orElse("") .toLowerCase().replace("jpeg", "jpg"); String basePath = Optional.ofNullable(Files.probeContentType(file.toPath())).orElse("application") .split("/")[0]; String path = basePath + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + RandomStringUtils.randomAlphabetic(8) + "." + ext; return new FileObject("", storageService.uploadFromInputStream(new FileInputStream(file), path), null, ext); } @Test public void test1() { Long collectionId = 2570204L; Collection collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("无藏品")); if (!collection.isOnShelf() || !collection.isSalable()) { collectionId = null; } else if (collection.isScheduleSale()) { if (collection.getStartTime().isAfter(LocalDateTime.now())) { collectionId = null; } } System.out.println(collectionId); } @Test public void savePoint() { // collectionRepo.findById(206985L).orElseThrow(new BusinessException("无藏品")); collectionService.savePoint(3749128L, LocalDateTime.of(2022, 3, 23, 15, 0, 0)); } @Test public void uploadMp4() { Arrays.stream(new File("/Users/qiufangchao/Desktop/majiang").listFiles()) .parallel() .filter(f -> !f.getName().contains(".DS_Store")) .forEach(file -> { System.out.println(file.getName()); try { String url = storageService.uploadFromInputStream(new FileInputStream(file), "video/mahjongman/" + file.getName()); String thumbPath = "thumb_image/mahjongman/" + file.getName() .substring(0, file.getName().lastIndexOf(".")) + ".jpg"; FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(file); Java2DFrameConverter frameConverter = new Java2DFrameConverter(); try { frameGrabber.start(); Frame frame = null; while (frame == null) { frame = frameGrabber.grabKeyFrame(); } Objects.requireNonNull(frame, "获取视频缩略图失败"); BufferedImage thumbBi = frameConverter.convert(frame); BufferedImage thumbResized = ImageUtils.resizeJpg(thumbBi, 1000, 1000, false); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(thumbResized, "jpg", os); InputStream is = new ByteArrayInputStream(os.toByteArray()); String thumbUrl = storageService.uploadFromInputStream(is, thumbPath); } catch (Exception e) { e.printStackTrace(); } finally { frameGrabber.stop(); } } catch (Exception e) { } }); } @Test public void test2() { String name = "/Users/qiufangchao/Desktop/majiang/bai1-1.mp4"; Pattern p = Pattern.compile("(\\d+)-(\\d+)"); Matcher matcher = p.matcher(name); matcher.find(); String firstHour = matcher.group(); System.out.println(matcher.start()); System.out.println(firstHour); } }