Skip to content

Commit

Permalink
自动创建头像目录
Browse files Browse the repository at this point in the history
  • Loading branch information
imndx committed Jul 26, 2023
1 parent cf2092c commit 3165305
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/main/java/cn/wildfirechat/app/avatar/AvatarServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<byte[]> avatar(String name) throws IOException {
File file = nameAvatar(name);
Expand Down Expand Up @@ -51,7 +62,7 @@ public ResponseEntity<byte[]> 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);
}
Expand All @@ -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)];
// 最后一个字符
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 3165305

Please sign in to comment.