From 0c949aacf90cd8026b124cf67ae216ccf1a8e992 Mon Sep 17 00:00:00 2001 From: jhqwqmc <2110242767@qq.com> Date: Mon, 21 Apr 2025 19:43:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9=20Git?= =?UTF-8?q?Lab=20=E7=9A=84=E8=B5=84=E6=BA=90=E5=8C=85=E6=89=98=E7=AE=A1?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/pack/host/impl/GitLabHost.java | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/net/momirealms/craftengine/core/pack/host/impl/GitLabHost.java b/core/src/main/java/net/momirealms/craftengine/core/pack/host/impl/GitLabHost.java index e1e47cd87..7f5d6830f 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/pack/host/impl/GitLabHost.java +++ b/core/src/main/java/net/momirealms/craftengine/core/pack/host/impl/GitLabHost.java @@ -11,7 +11,6 @@ import net.momirealms.craftengine.core.util.HashUtils; import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.core.util.MiscUtils; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -115,10 +114,12 @@ public class GitLabHost implements ResourcePackHost { this.sha1 = HashUtils.calculateLocalFileSha1(resourcePackPath); this.uuid = UUID.nameUUIDFromBytes(this.sha1.getBytes(StandardCharsets.UTF_8)); try (HttpClient client = HttpClient.newBuilder().proxy(this.proxy).build()) { + String boundary = UUID.randomUUID().toString(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(this.gitlabUrl + "/api/v4/projects/" + this.projectId + "/uploads")) .header("PRIVATE-TOKEN", this.accessToken) - .POST(HttpRequest.BodyPublishers.ofFile(resourcePackPath)) + .header("Content-Type", "multipart/form-data; boundary=" + boundary) + .POST(buildMultipartBody(resourcePackPath, boundary)) .build(); long uploadStart = System.currentTimeMillis(); CraftEngine.instance().logger().info( @@ -131,7 +132,7 @@ public class GitLabHost implements ResourcePackHost { if (response.statusCode() == 200 || response.statusCode() == 201) { Map json = GsonHelper.parseJsonToMap(response.body()); if (json.containsKey("full_path")) { - this.url = (String) json.get("full_path"); + this.url = this.gitlabUrl + json.get("full_path"); future.complete(null); saveCacheToDisk(); return; @@ -146,7 +147,7 @@ public class GitLabHost implements ResourcePackHost { future.completeExceptionally(ex); return null; }); - } catch (FileNotFoundException e) { + } catch (IOException e) { CraftEngine.instance().logger().warn( "[GitLab] Failed to upload resource pack: " + e.getMessage()); } @@ -154,6 +155,22 @@ public class GitLabHost implements ResourcePackHost { return future; } + private HttpRequest.BodyPublisher buildMultipartBody(Path filePath, String boundary) throws IOException { + List parts = new ArrayList<>(); + String filePartHeader = "--" + boundary + "\r\n" + + "Content-Disposition: form-data; name=\"file\"; filename=\"" + filePath.getFileName() + "\"\r\n" + + "Content-Type: application/octet-stream\r\n\r\n"; + parts.add(filePartHeader.getBytes()); + + parts.add(Files.readAllBytes(filePath)); + parts.add("\r\n".getBytes()); + + String endBoundary = "--" + boundary + "--\r\n"; + parts.add(endBoundary.getBytes()); + + return HttpRequest.BodyPublishers.ofByteArrays(parts); + } + public static class Factory implements ResourcePackHostFactory { @Override