Skip to content

Commit

Permalink
feat: schematic share system, add missing Clipboard method for api co…
Browse files Browse the repository at this point in the history
…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
PierreSchwang and me4502 authored Jun 2, 2024
1 parent 7635eec commit 261ebfa
Show file tree
Hide file tree
Showing 17 changed files with 764 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,13 @@ public static class WEB {
@Comment({"The web interface for clipboards", " - All schematics are anonymous and private", " - Downloads can be deleted by the user", " - Supports clipboard uploads, downloads and saves",})
public String URL = "https://schem.intellectualsites.com/fawe/";

@Comment({"The url of the backend server (Arkitektonika)"})
public String ARKITEKTONIKA_BACKEND_URL = "https://api.schematic.cloud/";
@Comment({"The url used to generate a download link from.", "{key} will be replaced with the generated key"})
public String ARKITEKTONIKA_DOWNLOAD_URL = "https://schematic.cloud/download/{key}";
@Comment({"The url used to generate a deletion link from.", "{key} will be replaced with the generated key"})
public String ARKITEKTONIKA_DELETE_URL = "https://schematic.cloud/delete/{key}";

@Comment("The maximum amount of time in seconds the plugin can attempt to load images for.")
public int MAX_IMAGE_LOAD_TIME = 5;

Expand Down
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) {
}
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()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ private void createCopy(
aliases = {"/download"},
desc = "Downloads your clipboard through the configured web interface"
)
@Deprecated
@Deprecated(forRemoval = true, since = "TODO")
@CommandPermissions({"worldedit.clipboard.download"})
public void download(
final Actor actor,
Expand Down Expand Up @@ -402,10 +402,7 @@ public void run(OutputStream out) {
final Clipboard target;
// If we have a transform, bake it into the copy
if (!transform.isIdentity()) {
final FlattenedClipboardTransform result = FlattenedClipboardTransform.transform(clipboard, transform);
target = new BlockArrayClipboard(result.getTransformedRegion(), actor.getUniqueId());
target.setOrigin(clipboard.getOrigin());
Operations.completeLegacy(result.copyTo(target));
target = clipboard.transform(transform);
} else {
target = clipboard;
}
Expand Down
Loading

0 comments on commit 261ebfa

Please sign in to comment.