mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2025-01-28 03:02:20 +01:00
Compress editor/verbose/treeview data using gzip before uploading
This commit is contained in:
parent
f6c440c172
commit
cde2306079
@ -69,7 +69,7 @@ public class HolderEditor<T extends PermissionHolder> extends SubCommand<T> {
|
|||||||
JsonObject payload = WebEditor.formPayload(Collections.singletonList(holder), sender, label, plugin);
|
JsonObject payload = WebEditor.formPayload(Collections.singletonList(holder), sender, label, plugin);
|
||||||
|
|
||||||
// upload the payload data to gist
|
// upload the payload data to gist
|
||||||
String pasteId = StandardPastebin.BYTEBIN.postJson(payload).id();
|
String pasteId = StandardPastebin.BYTEBIN.postJson(payload, true).id();
|
||||||
if (pasteId == null) {
|
if (pasteId == null) {
|
||||||
Message.EDITOR_UPLOAD_FAILURE.send(sender);
|
Message.EDITOR_UPLOAD_FAILURE.send(sender);
|
||||||
return CommandResult.STATE_ERROR;
|
return CommandResult.STATE_ERROR;
|
||||||
|
@ -102,7 +102,7 @@ public class EditorCommand extends SingleCommand {
|
|||||||
JsonObject payload = WebEditor.formPayload(holders, sender, label, plugin);
|
JsonObject payload = WebEditor.formPayload(holders, sender, label, plugin);
|
||||||
|
|
||||||
// upload the payload data to gist
|
// upload the payload data to gist
|
||||||
String pasteId = StandardPastebin.BYTEBIN.postJson(payload).id();
|
String pasteId = StandardPastebin.BYTEBIN.postJson(payload, true).id();
|
||||||
if (pasteId == null) {
|
if (pasteId == null) {
|
||||||
Message.EDITOR_UPLOAD_FAILURE.send(sender);
|
Message.EDITOR_UPLOAD_FAILURE.send(sender);
|
||||||
return CommandResult.STATE_ERROR;
|
return CommandResult.STATE_ERROR;
|
||||||
|
@ -167,7 +167,7 @@ public class TreeView {
|
|||||||
)
|
)
|
||||||
.toJson();
|
.toJson();
|
||||||
|
|
||||||
return StandardPastebin.BYTEBIN.postJson(payload).id();
|
return StandardPastebin.BYTEBIN.postJson(payload, true).id();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -27,16 +27,53 @@ package me.lucko.luckperms.common.utils.web;
|
|||||||
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a pastebin service
|
||||||
|
*/
|
||||||
public interface Pastebin {
|
public interface Pastebin {
|
||||||
|
|
||||||
Paste postJson(JsonElement element);
|
/**
|
||||||
|
* Posts the given json to the pastebin
|
||||||
|
*
|
||||||
|
* @param element the json element to post
|
||||||
|
* @param compress whether to compress and post the data using gzip
|
||||||
|
* @return a paste
|
||||||
|
*/
|
||||||
|
Paste postJson(JsonElement element, boolean compress);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Posts "plain" content to the pastebin
|
||||||
|
*
|
||||||
|
* @param content the content
|
||||||
|
* @return a paste
|
||||||
|
*/
|
||||||
Paste postPlain(String content);
|
Paste postPlain(String content);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the raw url of a paste's data from an id
|
||||||
|
*
|
||||||
|
* @param id the id
|
||||||
|
* @return a url
|
||||||
|
*/
|
||||||
String getRawUrl(String id);
|
String getRawUrl(String id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encapsulates the properties of a specific "paste" entry
|
||||||
|
*/
|
||||||
interface Paste {
|
interface Paste {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the url of the paste
|
||||||
|
*
|
||||||
|
* @return the url
|
||||||
|
*/
|
||||||
String url();
|
String url();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the unique id of the paste
|
||||||
|
*
|
||||||
|
* @return the id
|
||||||
|
*/
|
||||||
String id();
|
String id();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,13 +37,15 @@ import okhttp3.Response;
|
|||||||
import okhttp3.ResponseBody;
|
import okhttp3.ResponseBody;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.io.Writer;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.zip.GZIPOutputStream;
|
||||||
|
|
||||||
public enum StandardPastebin implements Pastebin {
|
public enum StandardPastebin implements Pastebin {
|
||||||
|
|
||||||
@ -98,28 +100,44 @@ public enum StandardPastebin implements Pastebin {
|
|||||||
protected abstract String parseIdFromResult(BufferedReader reader);
|
protected abstract String parseIdFromResult(BufferedReader reader);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Pastebin.Paste postJson(JsonElement content) {
|
public Pastebin.Paste postJson(JsonElement content, boolean compress) {
|
||||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
||||||
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(bytes))) {
|
|
||||||
|
OutputStream outputStream;
|
||||||
|
if (compress) {
|
||||||
|
try {
|
||||||
|
outputStream = new GZIPOutputStream(byteOut);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
outputStream = byteOut;
|
||||||
|
}
|
||||||
|
|
||||||
|
try (Writer writer = new OutputStreamWriter(outputStream)) {
|
||||||
GSON.toJson(content, writer);
|
GSON.toJson(content, writer);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return post(RequestBody.create(JSON_TYPE, bytes.toByteArray()));
|
return post(RequestBody.create(JSON_TYPE, byteOut.toByteArray()), compress);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Pastebin.Paste postPlain(String content) {
|
public Pastebin.Paste postPlain(String content) {
|
||||||
return post(RequestBody.create(PLAIN_TYPE, content));
|
return post(RequestBody.create(PLAIN_TYPE, content), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Pastebin.Paste post(RequestBody body) {
|
private Pastebin.Paste post(RequestBody body, boolean compressed) {
|
||||||
Request request = new Request.Builder()
|
Request.Builder requestBuilder = new Request.Builder()
|
||||||
.url(getPostUrl())
|
.url(getPostUrl())
|
||||||
.post(body)
|
.post(body);
|
||||||
.build();
|
|
||||||
|
|
||||||
|
if (compressed) {
|
||||||
|
requestBuilder.header("Content-Encoding", "gzip");
|
||||||
|
}
|
||||||
|
|
||||||
|
Request request = requestBuilder.build();
|
||||||
try (Response response = HttpClient.makeCall(request)) {
|
try (Response response = HttpClient.makeCall(request)) {
|
||||||
try (ResponseBody responseBody = response.body()) {
|
try (ResponseBody responseBody = response.body()) {
|
||||||
if (responseBody == null) {
|
if (responseBody == null) {
|
||||||
|
@ -56,11 +56,11 @@ public class VerboseListener {
|
|||||||
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
|
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
|
||||||
|
|
||||||
// how much data should we store before stopping.
|
// how much data should we store before stopping.
|
||||||
private static final int DATA_TRUNCATION = 3000;
|
private static final int DATA_TRUNCATION = 10000;
|
||||||
// how many lines should we include in each stack trace send as a chat message
|
// how many lines should we include in each stack trace send as a chat message
|
||||||
private static final int STACK_TRUNCATION_CHAT = 15;
|
private static final int STACK_TRUNCATION_CHAT = 15;
|
||||||
// how many lines should we include in each stack trace in the web output
|
// how many lines should we include in each stack trace in the web output
|
||||||
private static final int STACK_TRUNCATION_WEB = 20;
|
private static final int STACK_TRUNCATION_WEB = 30;
|
||||||
|
|
||||||
private static final StackTracePrinter FILTERING_PRINTER = StackTracePrinter.builder()
|
private static final StackTracePrinter FILTERING_PRINTER = StackTracePrinter.builder()
|
||||||
.ignoreClassStartingWith("me.lucko.luckperms.")
|
.ignoreClassStartingWith("me.lucko.luckperms.")
|
||||||
@ -222,7 +222,7 @@ public class VerboseListener {
|
|||||||
.add("data", data)
|
.add("data", data)
|
||||||
.toJson();
|
.toJson();
|
||||||
|
|
||||||
return StandardPastebin.BYTEBIN.postJson(payload).id();
|
return StandardPastebin.BYTEBIN.postJson(payload, true).id();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getTristateColor(Tristate tristate) {
|
private static String getTristateColor(Tristate tristate) {
|
||||||
|
Loading…
Reference in New Issue
Block a user