mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-28 03:19:14 +00:00
refactor(pack): 重构资源包主机类的 JSON 解析和 SHA1 计算
This commit is contained in:
@@ -2,12 +2,12 @@ package net.momirealms.craftengine.core.pack.host.impl;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackDownloadData;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackHost;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackHostFactory;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.util.GsonHelper;
|
||||
import net.momirealms.craftengine.core.util.HashUtils;
|
||||
import net.momirealms.craftengine.core.util.MiscUtils;
|
||||
import net.momirealms.craftengine.core.util.Pair;
|
||||
|
||||
@@ -137,7 +137,7 @@ public class AlistHost implements ResourcePackHost {
|
||||
.thenAccept(response -> {
|
||||
long uploadTime = System.currentTimeMillis() - requestStart;
|
||||
if (response.statusCode() == 200) {
|
||||
cacheSha1 = calculateLocalFileSha1(finalResourcePackPath);
|
||||
cacheSha1 = HashUtils.calculateLocalFileSha1(finalResourcePackPath);
|
||||
saveCacheToDisk();
|
||||
CraftEngine.instance().logger().info("[Alist] Upload resource pack success after " + uploadTime + "ms");
|
||||
future.complete(null);
|
||||
@@ -174,7 +174,7 @@ public class AlistHost implements ResourcePackHost {
|
||||
CraftEngine.instance().logger().warn("[Alist] Failed to get JWT token: " + response.body());
|
||||
return null;
|
||||
}
|
||||
JsonObject jsonData = parseJson(response.body());
|
||||
JsonObject jsonData = GsonHelper.parseJsonToJsonObject(response.body());
|
||||
JsonElement code = jsonData.get("code");
|
||||
if (code.isJsonPrimitive() && code.getAsJsonPrimitive().isNumber() && code.getAsJsonPrimitive().getAsInt() == 200) {
|
||||
JsonElement data = jsonData.get("data");
|
||||
@@ -216,7 +216,7 @@ public class AlistHost implements ResourcePackHost {
|
||||
private void handleResourcePackDownloadLinkResponse(
|
||||
HttpResponse<String> response, CompletableFuture<List<ResourcePackDownloadData>> future) {
|
||||
if (response.statusCode() == 200) {
|
||||
JsonObject json = parseJson(response.body());
|
||||
JsonObject json = GsonHelper.parseJsonToJsonObject(response.body());
|
||||
JsonElement code = json.get("code");
|
||||
if (code.isJsonPrimitive() && code.getAsJsonPrimitive().isNumber() && code.getAsJsonPrimitive().getAsInt() == 200) {
|
||||
JsonElement data = json.get("data");
|
||||
@@ -262,32 +262,6 @@ public class AlistHost implements ResourcePackHost {
|
||||
new RuntimeException("Failed to request resource pack download link: " + response.body()));
|
||||
}
|
||||
|
||||
private JsonObject parseJson(String json) {
|
||||
try {
|
||||
return GsonHelper.get().fromJson(
|
||||
json,
|
||||
JsonObject.class
|
||||
);
|
||||
} catch (JsonSyntaxException e) {
|
||||
throw new RuntimeException("Invalid JSON response: " + json, e);
|
||||
}
|
||||
}
|
||||
|
||||
private String calculateLocalFileSha1(Path filePath) {
|
||||
try (InputStream is = Files.newInputStream(filePath)) {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-1");
|
||||
byte[] buffer = new byte[8192];
|
||||
int len;
|
||||
while ((len = is.read(buffer)) != -1) {
|
||||
md.update(buffer, 0, len);
|
||||
}
|
||||
byte[] digest = md.digest();
|
||||
return HexFormat.of().formatHex(digest);
|
||||
} catch (IOException | NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("Failed to calculate SHA1", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Factory implements ResourcePackHostFactory {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package net.momirealms.craftengine.core.pack.host.impl;
|
||||
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackDownloadData;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackHost;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackHostFactory;
|
||||
@@ -10,7 +8,6 @@ import net.momirealms.craftengine.core.util.GsonHelper;
|
||||
import net.momirealms.craftengine.core.util.MiscUtils;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.net.Authenticator;
|
||||
import java.net.ProxySelector;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
@@ -49,7 +46,7 @@ public class CustomApiHost implements ResourcePackHost {
|
||||
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
|
||||
.thenAccept(response -> {
|
||||
if (response.statusCode() == 200) {
|
||||
Map<String, Object> jsonData = parseJson(response.body());
|
||||
Map<String, Object> jsonData = GsonHelper.parseJsonToMap(response.body());
|
||||
String url = (String) jsonData.get("url");
|
||||
String sha1 = (String) jsonData.get("sha1");
|
||||
UUID uuid = UUID.fromString(sha1);
|
||||
@@ -105,17 +102,6 @@ public class CustomApiHost implements ResourcePackHost {
|
||||
return future;
|
||||
}
|
||||
|
||||
private Map<String, Object> parseJson(String json) {
|
||||
try {
|
||||
return GsonHelper.get().fromJson(
|
||||
json,
|
||||
new TypeToken<Map<String, Object>>() {}.getType()
|
||||
);
|
||||
} catch (JsonSyntaxException e) {
|
||||
throw new RuntimeException("Invalid JSON response: " + json, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Factory implements ResourcePackHostFactory {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,13 +2,13 @@ package net.momirealms.craftengine.core.pack.host.impl;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackDownloadData;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackHost;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackHostFactory;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.util.GsonHelper;
|
||||
import net.momirealms.craftengine.core.util.HashUtils;
|
||||
import net.momirealms.craftengine.core.util.MiscUtils;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
@@ -24,8 +24,6 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@@ -107,7 +105,7 @@ public class DropboxHost implements ResourcePackHost {
|
||||
Path finalResourcePackPath = resourcePackPath;
|
||||
|
||||
CraftEngine.instance().scheduler().executeAsync(() -> {
|
||||
String sha1 = calculateLocalFileSha1(finalResourcePackPath);
|
||||
String sha1 = HashUtils.calculateLocalFileSha1(finalResourcePackPath);
|
||||
try (HttpClient client = HttpClient.newBuilder().proxy(proxy).build()) {
|
||||
JsonObject apiArg = new JsonObject();
|
||||
apiArg.addProperty("path", uploadPath);
|
||||
@@ -179,7 +177,7 @@ public class DropboxHost implements ResourcePackHost {
|
||||
.build();
|
||||
HttpResponse<String> listResponse = client.send(listLinksRequest, HttpResponse.BodyHandlers.ofString());
|
||||
if (listResponse.statusCode() == 200) {
|
||||
JsonObject responseJson = parseJson(listResponse.body());
|
||||
JsonObject responseJson = GsonHelper.parseJsonToJsonObject(listResponse.body());
|
||||
JsonArray links = responseJson.getAsJsonArray("links");
|
||||
if (!links.isEmpty()) {
|
||||
return links.get(0).getAsJsonObject().get("url").getAsString().replace("dl=0", "dl=1");
|
||||
@@ -189,7 +187,7 @@ public class DropboxHost implements ResourcePackHost {
|
||||
CraftEngine.instance().logger().warn("[DropBox] Failed to get download url: " + response.body());
|
||||
return null;
|
||||
}
|
||||
JsonObject jsonData = parseJson(response.body());
|
||||
JsonObject jsonData = GsonHelper.parseJsonToJsonObject(response.body());
|
||||
return jsonData.getAsJsonPrimitive("url").getAsString().replace("dl=0", "dl=1");
|
||||
} catch (IOException | InterruptedException e) {
|
||||
CraftEngine.instance().logger().warn("[DropBox] Failed to get download url: " + e.getMessage());
|
||||
@@ -198,32 +196,6 @@ public class DropboxHost implements ResourcePackHost {
|
||||
}
|
||||
}
|
||||
|
||||
private JsonObject parseJson(String json) {
|
||||
try {
|
||||
return GsonHelper.get().fromJson(
|
||||
json,
|
||||
JsonObject.class
|
||||
);
|
||||
} catch (JsonSyntaxException e) {
|
||||
throw new RuntimeException("Invalid JSON response: " + json, e);
|
||||
}
|
||||
}
|
||||
|
||||
private String calculateLocalFileSha1(Path filePath) {
|
||||
try (InputStream is = Files.newInputStream(filePath)) {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-1");
|
||||
byte[] buffer = new byte[8192];
|
||||
int len;
|
||||
while ((len = is.read(buffer)) != -1) {
|
||||
md.update(buffer, 0, len);
|
||||
}
|
||||
byte[] digest = md.digest();
|
||||
return HexFormat.of().formatHex(digest);
|
||||
} catch (IOException | NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("Failed to calculate SHA1", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Factory implements ResourcePackHostFactory {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package net.momirealms.craftengine.core.pack.host.impl;
|
||||
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackDownloadData;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackHost;
|
||||
@@ -12,7 +11,6 @@ import net.momirealms.craftengine.core.util.MiscUtils;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.Authenticator;
|
||||
import java.net.ProxySelector;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
@@ -225,7 +223,7 @@ public class LobFileHost implements ResourcePackHost {
|
||||
) {
|
||||
try {
|
||||
if (response.statusCode() == 200) {
|
||||
Map<String, Object> json = parseJson(response.body());
|
||||
Map<String, Object> json = GsonHelper.parseJsonToMap(response.body());
|
||||
if (Boolean.TRUE.equals(json.get("success"))) {
|
||||
this.url = (String) json.get("url");
|
||||
this.sha1 = localSha1;
|
||||
@@ -261,17 +259,6 @@ public class LobFileHost implements ResourcePackHost {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private Map<String, Object> parseJson(String json) {
|
||||
try {
|
||||
return GsonHelper.get().fromJson(
|
||||
json,
|
||||
new TypeToken<Map<String, Object>>() {}.getType()
|
||||
);
|
||||
} catch (JsonSyntaxException e) {
|
||||
throw new RuntimeException("Invalid JSON response: " + json, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Factory implements ResourcePackHostFactory {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package net.momirealms.craftengine.core.pack.host.impl;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackDownloadData;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackHost;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackHostFactory;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.util.GsonHelper;
|
||||
import net.momirealms.craftengine.core.util.HashUtils;
|
||||
import net.momirealms.craftengine.core.util.MiscUtils;
|
||||
import net.momirealms.craftengine.core.util.Tuple;
|
||||
|
||||
@@ -25,8 +25,6 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@@ -122,7 +120,7 @@ public class OneDriveHost implements ResourcePackHost {
|
||||
future.completeExceptionally(new IOException("Failed to request resource pack download link: " + response.body()));
|
||||
return;
|
||||
}
|
||||
String downloadUrl = parseJson(response.body()).get("@microsoft.graph.downloadUrl").getAsString();
|
||||
String downloadUrl = GsonHelper.parseJsonToJsonObject(response.body()).get("@microsoft.graph.downloadUrl").getAsString();
|
||||
future.complete(List.of(new ResourcePackDownloadData(
|
||||
downloadUrl,
|
||||
UUID.nameUUIDFromBytes(sha1.getBytes(StandardCharsets.UTF_8)),
|
||||
@@ -145,7 +143,7 @@ public class OneDriveHost implements ResourcePackHost {
|
||||
if (this.localFilePath != null) resourcePackPath = this.localFilePath;
|
||||
Path finalResourcePackPath = resourcePackPath;
|
||||
CraftEngine.instance().scheduler().executeAsync(() -> {
|
||||
sha1 = calculateLocalFileSha1(finalResourcePackPath);
|
||||
sha1 = HashUtils.calculateLocalFileSha1(finalResourcePackPath);
|
||||
String accessToken = getOrRefreshJwtToken();
|
||||
try (HttpClient client = HttpClient.newBuilder().proxy(proxy).build()) {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
@@ -160,7 +158,7 @@ public class OneDriveHost implements ResourcePackHost {
|
||||
.thenAccept(response -> {
|
||||
if (response.statusCode() == 200 || response.statusCode() == 201) {
|
||||
CraftEngine.instance().logger().info("[OneDrive] Uploaded resource pack in " + (System.currentTimeMillis() - uploadStart) + "ms");
|
||||
fileId = parseJson(response.body()).get("id").getAsString();
|
||||
fileId = GsonHelper.parseJsonToJsonObject(response.body()).get("id").getAsString();
|
||||
saveCacheToDisk();
|
||||
future.complete(null);
|
||||
} else {
|
||||
@@ -204,7 +202,7 @@ public class OneDriveHost implements ResourcePackHost {
|
||||
return refreshToken != null ? refreshToken.mid() : "";
|
||||
}
|
||||
|
||||
JsonObject jsonData = parseJson(response.body());
|
||||
JsonObject jsonData = GsonHelper.parseJsonToJsonObject(response.body());
|
||||
if (jsonData.has("error")) {
|
||||
CraftEngine.instance().logger().warn("[OneDrive] Token refresh error: " + jsonData);
|
||||
throw new RuntimeException("Token refresh failed: " + jsonData);
|
||||
@@ -224,32 +222,6 @@ public class OneDriveHost implements ResourcePackHost {
|
||||
return refreshToken.mid();
|
||||
}
|
||||
|
||||
private JsonObject parseJson(String json) {
|
||||
try {
|
||||
return GsonHelper.get().fromJson(
|
||||
json,
|
||||
JsonObject.class
|
||||
);
|
||||
} catch (JsonSyntaxException e) {
|
||||
throw new RuntimeException("Invalid JSON response: " + json, e);
|
||||
}
|
||||
}
|
||||
|
||||
private String calculateLocalFileSha1(Path filePath) {
|
||||
try (InputStream is = Files.newInputStream(filePath)) {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-1");
|
||||
byte[] buffer = new byte[8192];
|
||||
int len;
|
||||
while ((len = is.read(buffer)) != -1) {
|
||||
md.update(buffer, 0, len);
|
||||
}
|
||||
byte[] digest = md.digest();
|
||||
return HexFormat.of().formatHex(digest);
|
||||
} catch (IOException | NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("Failed to calculate SHA1", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Factory implements ResourcePackHostFactory {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,6 +4,7 @@ import net.momirealms.craftengine.core.pack.host.ResourcePackDownloadData;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackHost;
|
||||
import net.momirealms.craftengine.core.pack.host.ResourcePackHostFactory;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.util.HashUtils;
|
||||
import net.momirealms.craftengine.core.util.MiscUtils;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
@@ -22,17 +23,15 @@ import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
|
||||
import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Duration;
|
||||
import java.util.*;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
|
||||
@@ -112,7 +111,7 @@ public class S3Host implements ResourcePackHost {
|
||||
public CompletableFuture<Void> upload(Path resourcePackPath) {
|
||||
if (this.localFilePath != null) resourcePackPath = this.localFilePath;
|
||||
String objectKey = uploadPath;
|
||||
String sha1 = calculateLocalFileSha1(resourcePackPath);
|
||||
String sha1 = HashUtils.calculateLocalFileSha1(resourcePackPath);
|
||||
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
|
||||
.bucket(bucket)
|
||||
.key(objectKey)
|
||||
@@ -147,21 +146,6 @@ public class S3Host implements ResourcePackHost {
|
||||
+ (originalUrl.getQuery() != null ? "?" + originalUrl.getQuery() : "");
|
||||
}
|
||||
|
||||
private String calculateLocalFileSha1(Path filePath) {
|
||||
try (InputStream is = Files.newInputStream(filePath)) {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-1");
|
||||
byte[] buffer = new byte[8192];
|
||||
int len;
|
||||
while ((len = is.read(buffer)) != -1) {
|
||||
md.update(buffer, 0, len);
|
||||
}
|
||||
byte[] digest = md.digest();
|
||||
return HexFormat.of().formatHex(digest);
|
||||
} catch (IOException | NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("Failed to calculate SHA1", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Factory implements ResourcePackHostFactory {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.momirealms.craftengine.core.util;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
@@ -78,4 +79,26 @@ public class GsonHelper {
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
public static JsonObject parseJsonToJsonObject(String json) {
|
||||
try {
|
||||
return get().fromJson(
|
||||
json,
|
||||
JsonObject.class
|
||||
);
|
||||
} catch (JsonSyntaxException e) {
|
||||
throw new RuntimeException("Invalid JSON response: " + json, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, Object> parseJsonToMap(String json) {
|
||||
try {
|
||||
return GsonHelper.get().fromJson(
|
||||
json,
|
||||
new TypeToken<Map<String, Object>>() {}.getType()
|
||||
);
|
||||
} catch (JsonSyntaxException e) {
|
||||
throw new RuntimeException("Invalid JSON response: " + json, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package net.momirealms.craftengine.core.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HexFormat;
|
||||
|
||||
public class HashUtils {
|
||||
public static String calculateLocalFileSha1(Path filePath) {
|
||||
try (InputStream is = Files.newInputStream(filePath)) {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-1");
|
||||
byte[] buffer = new byte[8192];
|
||||
int len;
|
||||
while ((len = is.read(buffer)) != -1) {
|
||||
md.update(buffer, 0, len);
|
||||
}
|
||||
byte[] digest = md.digest();
|
||||
return HexFormat.of().formatHex(digest);
|
||||
} catch (IOException | NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("Failed to calculate SHA1", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user