-
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: schematic share system, add missing Clipboard method for api co…
…mpat (#2745) * Allow plugins to register new clipboard share destinations (#1707) * Allow plugins to register new clipboard share destinations * Rename file, as per request * Don't use the base enginehub name for EH_pastebin * Address review comments * Fixed wrong usage * Use a second metadata class for clipboard shares * Newline * Address comments * Improve docs * Apply suggestions from code review Co-authored-by: Octavia Togami <octavia.togami@gmail.com> * Use a consumer so that we handle serialization * Update worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/Clipboard.java Co-authored-by: Octavia Togami <octavia.togami@gmail.com> * Update worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/share/ClipboardShareDestination.java Co-authored-by: Octavia Togami <octavia.togami@gmail.com> * Update worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/share/ShareOutputConsumer.java Co-authored-by: Octavia Togami <octavia.togami@gmail.com> * Update worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/share/ShareOutputConsumer.java Co-authored-by: Octavia Togami <octavia.togami@gmail.com> * Fixed a lot of random comments * Return a consumer from share rather than a URL, allows the share destination to control output Co-authored-by: Octavia Togami <octavia.togami@gmail.com> (cherry picked from commit 6e2b0a1df8a6077c3cf8193e38dc9817038bcbe9) * chore: cleanup cherry-pick remainders * chore/feat: add ark as (default) schematic paster / sharing endpoint * chore: default to fast schematic writer in share * chore: re-format strings.json (seems to adjusted indentation when merging) * chore: hopefully fixing strings.json (again) --------- Co-authored-by: Maddy Miller <mnmiller1@me.com>
- Loading branch information
1 parent
7635eec
commit 261ebfa
Showing
17 changed files
with
764 additions
and
149 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
4 changes: 4 additions & 0 deletions
4
...e/src/main/java/com/fastasyncworldedit/core/util/arkitektonika/ArkitektonikaResponse.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,4 @@ | ||
package com.fastasyncworldedit.core.util.arkitektonika; | ||
|
||
public record ArkitektonikaResponse(String downloadKey, String deletionKey) { | ||
} |
58 changes: 58 additions & 0 deletions
58
...n/java/com/fastasyncworldedit/core/util/arkitektonika/ArkitektonikaSchematicUploader.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,58 @@ | ||
package com.fastasyncworldedit.core.util.arkitektonika; | ||
|
||
import com.fastasyncworldedit.core.internal.exception.FaweException; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import com.sk89q.worldedit.extent.clipboard.io.share.ClipboardShareMetadata; | ||
import com.sk89q.worldedit.extent.clipboard.io.share.ShareOutputProvider; | ||
import com.sk89q.worldedit.util.formatting.text.TextComponent; | ||
import com.sk89q.worldedit.util.formatting.text.format.TextColor; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
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.util.UUID; | ||
|
||
public class ArkitektonikaSchematicUploader { | ||
|
||
private static final String BOUNDARY_IDENTIFIER = "--"; | ||
private static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient(); | ||
private final String apiUrl; | ||
|
||
public ArkitektonikaSchematicUploader(String apiUrl) { | ||
this.apiUrl = apiUrl.endsWith("/") ? apiUrl.substring(0, apiUrl.length() - 1) : apiUrl; | ||
} | ||
|
||
public ArkitektonikaResponse uploadBlocking(ClipboardShareMetadata meta, ShareOutputProvider provider) throws IOException, | ||
InterruptedException { | ||
String boundary = UUID.randomUUID().toString(); | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
provider.writeTo(outputStream); | ||
|
||
final HttpRequest.BodyPublisher bodyPublisher = HttpRequest.BodyPublishers.concat( | ||
HttpRequest.BodyPublishers.ofString(BOUNDARY_IDENTIFIER + boundary + "\r\n"), | ||
HttpRequest.BodyPublishers.ofString("Content-Disposition: form-data; name=\"schematic\"; filename=\"" + meta.name() + "." + meta.format().getPrimaryFileExtension() + "\"\r\n\r\n"), | ||
HttpRequest.BodyPublishers.ofByteArray(outputStream.toByteArray()), | ||
HttpRequest.BodyPublishers.ofString("\r\n" + BOUNDARY_IDENTIFIER + boundary + BOUNDARY_IDENTIFIER) | ||
); | ||
|
||
final HttpResponse<String> response = HTTP_CLIENT.send(HttpRequest.newBuilder() | ||
.uri(URI.create(this.apiUrl + "/upload")) | ||
.header("Content-Type", "multipart/form-data; boundary=\"" + boundary + "\"") | ||
.POST(bodyPublisher).build(), HttpResponse.BodyHandlers.ofString()); | ||
if (response.statusCode() != 200) { | ||
throw new FaweException(TextComponent | ||
.of("Arkitektonika returned status code " + response.statusCode()) | ||
.color(TextColor.RED)); | ||
} | ||
JsonObject json = JsonParser.parseString(response.body()).getAsJsonObject(); | ||
return new ArkitektonikaResponse( | ||
json.get("download_key").getAsString(), | ||
json.get("delete_key").getAsString() | ||
); | ||
} | ||
|
||
} |
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
Oops, something went wrong.