-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add loading image from local storage and dumping images to local storage
- Loading branch information
Showing
11 changed files
with
192 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
common/src/main/java/cn/zbx1425/worldcomment/data/network/ImageDump.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package cn.zbx1425.worldcomment.data.network; | ||
|
||
import cn.zbx1425.worldcomment.BuildConfig; | ||
import cn.zbx1425.worldcomment.Main; | ||
import cn.zbx1425.worldcomment.data.CommentEntry; | ||
import cn.zbx1425.worldcomment.data.ServerWorldData; | ||
import cn.zbx1425.worldcomment.network.PacketCollectionRequestC2S; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.network.chat.Component; | ||
import org.apache.commons.codec.digest.DigestUtils; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.Executors; | ||
|
||
public class ImageDump { | ||
|
||
private static long lastRequestNonce = 0; | ||
private static String lastRequestDirName = ""; | ||
|
||
private static final Executor NETWORK_EXECUTOR = Executors.newSingleThreadExecutor(); | ||
private static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient(); | ||
|
||
public static void requestDumpComments(String dirName) { | ||
lastRequestNonce = ServerWorldData.SNOWFLAKE.nextId(); | ||
lastRequestDirName = dirName; | ||
PacketCollectionRequestC2S.ClientLogics.sendLatest(0, Integer.MAX_VALUE, lastRequestNonce); | ||
} | ||
|
||
public static void handleDumpResponse(List<CommentEntry> comments, long nonce) { | ||
if (nonce != lastRequestNonce) return; | ||
Path storeDir = Minecraft.getInstance().gameDirectory.toPath() | ||
.resolve("worldcomment-images").resolve(lastRequestDirName); | ||
try { | ||
if (!Files.isDirectory(storeDir)) Files.createDirectories(storeDir); | ||
} catch (IOException ex) { | ||
Main.LOGGER.error("Cannot create image dump directory", ex); | ||
return; | ||
} | ||
|
||
NETWORK_EXECUTOR.execute(() -> { | ||
for (int i = 0; i < comments.size(); i++) { | ||
int finalI = i; | ||
Minecraft.getInstance().execute(() -> { | ||
if (Minecraft.getInstance().player != null) { | ||
Minecraft.getInstance().player.displayClientMessage( | ||
Component.literal("WorldComment: Downloading " + (finalI + 1) + "/" + comments.size()), | ||
true); | ||
} | ||
}); | ||
CommentEntry comment = comments.get(i); | ||
String targetUrl = comment.image.url; | ||
if (targetUrl.isEmpty()) continue; | ||
Path filePath = storeDir.resolve("url-sha1-" + DigestUtils.sha1Hex(targetUrl) + ".png"); | ||
if (!Files.exists(filePath)) { | ||
try { | ||
byte[] imageData = HTTP_CLIENT.send( | ||
HttpRequest.newBuilder(URI.create(targetUrl)) | ||
.GET() | ||
.header("User-Agent", | ||
"Mozilla/5.0 WorldComment/" + BuildConfig.MOD_VERSION + " +https://www.zbx1425.cn") | ||
.build(), | ||
HttpResponse.BodyHandlers.ofByteArray()).body(); | ||
Files.write(filePath, imageData); | ||
} catch (IOException | InterruptedException ex) { | ||
Main.LOGGER.warn("Cannot download image " + targetUrl, ex); | ||
} | ||
} | ||
} | ||
Minecraft.getInstance().execute(() -> { | ||
if (Minecraft.getInstance().player != null) { | ||
Minecraft.getInstance().player.displayClientMessage( | ||
Component.literal("WorldComment: Download finished"), | ||
true); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.1-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |