From 316530583853bf7ae2c388c1610d0fc636debe4a Mon Sep 17 00:00:00 2001 From: imndx Date: Wed, 26 Jul 2023 10:58:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=88=9B=E5=BB=BA=E5=A4=B4?= =?UTF-8?q?=E5=83=8F=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/avatar/AvatarServiceImpl.java | 15 +++++++++++++-- .../app/avatar/NameAvatarBuilder.java | 3 +-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/cn/wildfirechat/app/avatar/AvatarServiceImpl.java b/src/main/java/cn/wildfirechat/app/avatar/AvatarServiceImpl.java index 263d1a3..b15c6d5 100644 --- a/src/main/java/cn/wildfirechat/app/avatar/AvatarServiceImpl.java +++ b/src/main/java/cn/wildfirechat/app/avatar/AvatarServiceImpl.java @@ -8,6 +8,7 @@ import org.springframework.util.StreamUtils; import org.springframework.util.StringUtils; +import javax.annotation.PostConstruct; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; @@ -21,6 +22,16 @@ public class AvatarServiceImpl implements AvatarService { @Value("${avatar.bg.corlors}") String bgColors; + public static final String AVATAR_DIR = "./avatar"; + + @PostConstruct + public void init() { + File avatarDir = new File(AvatarServiceImpl.AVATAR_DIR); + if (!avatarDir.exists()) { + avatarDir.mkdirs(); + } + } + @Override public ResponseEntity avatar(String name) throws IOException { File file = nameAvatar(name); @@ -51,7 +62,7 @@ public ResponseEntity groupAvatar(GroupAvatarRequest request) throws IOE hashCode += info.getName().hashCode(); } } - File file = new File("./avatar/" + hashCode + "-group.png"); + File file = new File(AVATAR_DIR, hashCode + "-group.png"); if (!file.exists()) { GroupAvatarUtil.getCombinationOfHead(paths, file); } @@ -71,7 +82,7 @@ private File nameAvatar(String name) { String[] colors = bgColors.split(","); int len = colors.length; int hashCode = name.hashCode(); - File file = new File("./avatar/" + hashCode + ".png"); + File file = new File(AVATAR_DIR, hashCode + ".png"); if (!file.exists()) { String color = colors[Math.abs(name.hashCode() % len)]; // 最后一个字符 diff --git a/src/main/java/cn/wildfirechat/app/avatar/NameAvatarBuilder.java b/src/main/java/cn/wildfirechat/app/avatar/NameAvatarBuilder.java index 8fcea2b..0154f3b 100644 --- a/src/main/java/cn/wildfirechat/app/avatar/NameAvatarBuilder.java +++ b/src/main/java/cn/wildfirechat/app/avatar/NameAvatarBuilder.java @@ -17,7 +17,6 @@ public class NameAvatarBuilder { private String fullName; public NameAvatarBuilder(String bgRGB) { - // templateImage = ImageIO.read(new File("./avatar/headbg.jpg")); templateImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); templateG2D = templateImage.createGraphics(); templateWidth = templateImage.getWidth(); @@ -45,7 +44,7 @@ public NameAvatarBuilder name(String drawName, String fullName) { public File build() { templateG2D.dispose(); templateImage.flush(); - File file = new File("./avatar/" + this.fullName.hashCode() + ".png"); + File file = new File(AvatarServiceImpl.AVATAR_DIR, this.fullName.hashCode() + ".png"); try { ImageIO.write(templateImage, "png", file); } catch (IOException e) {