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.repo.CollectionRepo; import com.izouma.nineth.repo.PrivilegeOptionRepo; import com.izouma.nineth.repo.UserRepo; import com.izouma.nineth.service.storage.StorageService; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.RandomStringUtils; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.file.Files; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; 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(2001); List items = new ArrayList<>(); String jsonStr = FileUtils.readFileToString(new File("/Users/drew/Downloads/003.json"), "UTF-8"); Arrays.stream(new File("/Users/drew/Downloads/s3-1000").listFiles()) .filter(f -> Pattern.matches("^BJ.*\\.png$", f.getName())) .parallel().forEach(file -> { try { Collection collection = JSON.parseObject(jsonStr, Collection.class); collection.setId(null); collection.setName(String.format("MAYBEMAN #%05d", num.getAndIncrement())); collection.setStock(1); collection.setTotal(1); collection.setSale(0); collection.setMinterId(8666L); collection.setOnShelf(false); collection.setSalable(false); collection.setPic(Collections.singletonList(new FileObject("", "https://raex-meta.oss-cn-shenzhen.aliyuncs.com/image/s3/" + file.getName(), null, "png"))); collectionRepo.save(collection); items.add(BlindBoxItem .builder() .collectionId(collection.getId()) .total(1) .stock(1) .rare(false) .build()); } catch (Exception e) { } }); } @Test public void createBlindBox() throws IOException { List items = collectionRepo.findByNameLike("MAYBEMAN #%").stream().filter(i -> { int num = Integer.parseInt(i.getName().substring("MAYBEMAN #".length())); return num > 2000 && num <= 3000; }).collect(Collectors.toList()); String jsonStr = FileUtils.readFileToString(new File("/Users/drew/Downloads/003.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(8666L); blindBox.setName("MAYBEMAN潮流艺术限定盲盒S3"); blindBox.setPic(Arrays.asList(new FileObject(null, "https://raex-meta.oss-cn-shenzhen.aliyuncs.com/nft/2022-01-07-10-08-30DpIYYBOv.jpg", null, null))); 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); } }