Skip to content

Commit

Permalink
Change image download to async
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Jan 16, 2025
1 parent cdaeb44 commit 13b40fe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.zbx1425.worldcomment.data.network;

import cn.zbx1425.worldcomment.BuildConfig;
import cn.zbx1425.worldcomment.Main;
import cn.zbx1425.worldcomment.util.OffHeapAllocator;
import com.mojang.blaze3d.platform.NativeImage;
Expand All @@ -19,6 +20,8 @@
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
Expand All @@ -30,7 +33,6 @@
public class ImageDownload {

private static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient();
private static final Executor NETWORK_EXECUTOR = Executors.newSingleThreadExecutor();

private static final Map<String, ImageState> images = new HashMap<>();

Expand All @@ -51,32 +53,48 @@ public static AbstractTexture getTexture(ThumbImage image, boolean thumb) {
String targetUrl = (thumb && !image.thumbUrl.isEmpty()) ? image.thumbUrl : image.url;
if (!images.containsKey(targetUrl)) {
images.put(targetUrl, new ImageState());
NETWORK_EXECUTOR.execute(() -> downloadImage(targetUrl));
downloadImage(targetUrl);
}
return queryTexture(targetUrl);
}

private static void downloadImage(String url) {
try {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)).GET().build();
HttpResponse<byte[]> response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofByteArray());
if (response.statusCode() != 200) throw new IOException("HTTP Status " + response.statusCode());
byte[] imageData = response.body();
applyImageData(url, imageData);
} catch (Throwable ex) {
Main.LOGGER.warn("Cannot download image " + url, ex);
synchronized (images) {
images.get(url).failed = true;
}
}
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.header("User-Agent",
"Mozilla/5.0 WorldComment/" + BuildConfig.MOD_VERSION + " +https://www.zbx1425.cn")
.header("X-Minecraft-Username", Minecraft.getInstance().getUser().getName())
.header("X-Minecraft-UUID", Minecraft.getInstance().getUser().getProfileId().toString())
.timeout(Duration.of(10, ChronoUnit.SECONDS))
.GET()
.build();
HTTP_CLIENT.sendAsync(request, HttpResponse.BodyHandlers.ofByteArray())
.thenAccept(response -> {
if (response.statusCode() != 200) {
Main.LOGGER.warn("Cannot download image {}: HTTP {}", url, response.statusCode());
synchronized (images) {
images.get(url).failed = true;
}
return;
}
byte[] imageData = response.body();
applyImageData(url, imageData);
})
.exceptionally(ex -> {
Main.LOGGER.warn("Cannot download image {}", url, ex);
synchronized (images) {
images.get(url).failed = true;
}
return null;
});
}

private static byte[] getLocalImageData(String url) throws IOException {
Path imageBaseDir = Minecraft.getInstance().gameDirectory.toPath().resolve("worldcomment-images");
if (!Files.isDirectory(imageBaseDir)) return null;
try (Stream<Path> imageDirs = Files.list(imageBaseDir)) {
for (Path imageDir : imageDirs.toArray(Path[]::new)) {
Path imagePath = imageDir.resolve("url-sha1-" + DigestUtils.sha1Hex(url) + ".png");
Path imagePath = imageDir.resolve("url-sha1-" + DigestUtils.sha1Hex(url)
+ (url.endsWith(".jpg") ? ".jpg" : ".png"));
if (Files.exists(imagePath)) {
return Files.readAllBytes(imagePath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public static void handleDumpResponse(List<CommentEntry> comments, long nonce) {
CommentEntry comment = comments.get(i);
String targetUrl = comment.image.url;
if (targetUrl.isEmpty()) continue;
Path filePath = storeDir.resolve("url-sha1-" + DigestUtils.sha1Hex(targetUrl) + ".png");
Path filePath = storeDir.resolve("url-sha1-" + DigestUtils.sha1Hex(targetUrl)
+ (targetUrl.endsWith(".jpg") ? ".jpg" : ".png"));
if (!Files.exists(filePath)) {
try {
byte[] imageData = HTTP_CLIENT.send(
Expand Down

0 comments on commit 13b40fe

Please sign in to comment.