Skip to content

Commit

Permalink
Switch to pastes.dev for /ess dump (#6011)
Browse files Browse the repository at this point in the history
Requires merge of EssentialsX/Website#106
  • Loading branch information
JRoy authored Jan 17, 2025
1 parent 667b0f7 commit 803771e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,14 @@ private void runDump(Server server, CommandSource sender, String commandLabel, S
final CompletableFuture<PasteUtil.PasteResult> future = PasteUtil.createPaste(files);
future.thenAccept(result -> {
if (result != null) {
final String dumpUrl = "https://essentialsx.net/dump.html?id=" + result.getPasteId();
final String dumpUrl = "https://essentialsx.net/dump.html?bytebin=" + result.getPasteId();
sender.sendTl("dumpUrl", dumpUrl);
sender.sendTl("dumpDeleteKey", result.getDeletionKey());
// pastes.dev doesn't support deletion keys
//sender.sendTl("dumpDeleteKey", result.getDeletionKey());
if (sender.isPlayer()) {
ess.getLogger().info(AdventureUtil.miniToLegacy(tlLiteral("dumpConsoleUrl", dumpUrl)));
ess.getLogger().info(AdventureUtil.miniToLegacy(tlLiteral("dumpDeleteKey", result.getDeletionKey())));
// pastes.dev doesn't support deletion keys
//ess.getLogger().info(AdventureUtil.miniToLegacy(tlLiteral("dumpDeleteKey", result.getDeletionKey())));
}
}
files.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.google.common.io.CharStreams;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand All @@ -17,10 +16,11 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.zip.GZIPOutputStream;

public final class PasteUtil {
private static final String PASTE_URL = "https://paste.gg/";
private static final String PASTE_UPLOAD_URL = "https://api.paste.gg/v1/pastes";
private static final String PASTE_URL = "https://pastes.dev/";
private static final String PASTE_UPLOAD_URL = "https://api.pastes.dev/post";
private static final ExecutorService PASTE_EXECUTOR_SERVICE = Executors.newSingleThreadExecutor();
private static final Gson GSON = new Gson();

Expand All @@ -42,7 +42,8 @@ public static CompletableFuture<PasteResult> createPaste(List<PasteFile> pages)
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("User-Agent", "EssentialsX plugin");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Type", "text/json");
connection.setRequestProperty("Content-Encoding", "gzip");
final JsonObject body = new JsonObject();
final JsonArray files = new JsonArray();
for (final PasteFile page : pages) {
Expand All @@ -56,24 +57,22 @@ public static CompletableFuture<PasteResult> createPaste(List<PasteFile> pages)
}
body.add("files", files);

try (final OutputStream os = connection.getOutputStream()) {
try (final OutputStream os = new GZIPOutputStream(connection.getOutputStream())) {
os.write(body.toString().getBytes(Charsets.UTF_8));
}

if (connection.getResponseCode() >= 400) {
//noinspection UnstableApiUsage
future.completeExceptionally(new Error(CharStreams.toString(new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8))));
return;
}

// Read URL
final JsonObject object = GSON.fromJson(new InputStreamReader(connection.getInputStream(), Charsets.UTF_8), JsonObject.class);
final String pasteId = object.get("result").getAsJsonObject().get("id").getAsString();
final String pasteId = object.get("key").getAsString();
final String pasteUrl = PASTE_URL + pasteId;
final JsonElement deletionKey = object.get("result").getAsJsonObject().get("deletion_key");
connection.disconnect();

final PasteResult result = new PasteResult(pasteId, pasteUrl, deletionKey != null ? deletionKey.getAsString() : null);
final PasteResult result = new PasteResult(pasteId, pasteUrl, null);
future.complete(result);
} catch (Exception e) {
future.completeExceptionally(e);
Expand Down

0 comments on commit 803771e

Please sign in to comment.