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 3165305 commit 6c23a42
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping(value = "/avatar")
Expand All @@ -21,7 +22,7 @@ public ResponseEntity<byte[]> avatar(@RequestParam("name") String name) throws I
}

@GetMapping("/group")
public ResponseEntity<byte[]> groupAvatar(@RequestParam("request") String request) throws IOException, URISyntaxException {
public CompletableFuture<ResponseEntity<byte[]>> groupAvatar(@RequestParam("request") String request) throws IOException, URISyntaxException {
ObjectMapper mapper = new ObjectMapper();
GroupAvatarRequest groupAvatarRequest = mapper.readValue(request, GroupAvatarRequest.class);
return avatarService.groupAvatar(groupAvatarRequest);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/cn/wildfirechat/app/avatar/AvatarService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.concurrent.CompletableFuture;

public interface AvatarService {
ResponseEntity<byte[]> avatar(String name) throws IOException;

ResponseEntity<byte[]> groupAvatar(GroupAvatarRequest requeset) throws IOException, URISyntaxException;
CompletableFuture<ResponseEntity<byte[]>> groupAvatar(GroupAvatarRequest requeset) throws IOException, URISyntaxException;
}
44 changes: 32 additions & 12 deletions src/main/java/cn/wildfirechat/app/avatar/AvatarServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

@Service
public class AvatarServiceImpl implements AvatarService {
Expand All @@ -35,7 +37,7 @@ public void init() {
@Override
public ResponseEntity<byte[]> avatar(String name) throws IOException {
File file = nameAvatar(name);
if (file.exists()) {
if (file != null && file.exists()) {
byte[] bytes = StreamUtils.copyToByteArray(Files.newInputStream(file.toPath()));
return ResponseEntity
.ok()
Expand All @@ -47,7 +49,7 @@ public ResponseEntity<byte[]> avatar(String name) throws IOException {
}

@Override
public ResponseEntity<byte[]> groupAvatar(GroupAvatarRequest request) throws IOException, URISyntaxException {
public CompletableFuture<ResponseEntity<byte[]>> groupAvatar(GroupAvatarRequest request) throws IOException, URISyntaxException {
List<GroupAvatarRequest.GroupMemberInfo> infos = request.getMembers();
List<URL> paths = new ArrayList<>();
long hashCode = 0;
Expand All @@ -58,27 +60,45 @@ public ResponseEntity<byte[]> groupAvatar(GroupAvatarRequest request) throws IOE
hashCode += info.getAvatarUrl().hashCode();
} else {
File file = nameAvatar(info.getName());
paths.add(file.toURI().toURL());
hashCode += info.getName().hashCode();
if (file != null && file.exists()) {
paths.add(file.toURI().toURL());
hashCode += info.getName().hashCode();
}
}
}
File file = new File(AVATAR_DIR, hashCode + "-group.png");
if (!file.exists()) {
GroupAvatarUtil.getCombinationOfHead(paths, file);
}
return CompletableFuture.supplyAsync(new Supplier<ResponseEntity<byte[]>>() {
@Override
public ResponseEntity<byte[]> get() {
try {
GroupAvatarUtil.getCombinationOfHead(paths, file);
if (file.exists()) {
byte[] bytes = StreamUtils.copyToByteArray(Files.newInputStream(file.toPath()));
return ResponseEntity.ok()
.contentType(MediaType.IMAGE_PNG)
.body(bytes);
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
} catch (IOException | URISyntaxException e) {
throw new RuntimeException(e);
}
}

if (file.exists()) {
});
} else {
byte[] bytes = StreamUtils.copyToByteArray(Files.newInputStream(file.toPath()));
return ResponseEntity
.ok()
return CompletableFuture.completedFuture(ResponseEntity.ok()
.contentType(MediaType.IMAGE_PNG)
.body(bytes);
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
.body(bytes));
}
}

private File nameAvatar(String name) {
if (StringUtils.isEmpty(name)) {
return null;
}
String[] colors = bgColors.split(",");
int len = colors.length;
int hashCode = name.hashCode();
Expand Down

0 comments on commit 6c23a42

Please sign in to comment.