Compress editor/verbose/treeview data using gzip before uploading

This commit is contained in:
Luck 2018-03-09 16:21:45 +00:00
parent f6c440c172
commit cde2306079
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
6 changed files with 72 additions and 17 deletions

View File

@ -69,7 +69,7 @@ public class HolderEditor<T extends PermissionHolder> extends SubCommand<T> {
JsonObject payload = WebEditor.formPayload(Collections.singletonList(holder), sender, label, plugin);
// upload the payload data to gist
String pasteId = StandardPastebin.BYTEBIN.postJson(payload).id();
String pasteId = StandardPastebin.BYTEBIN.postJson(payload, true).id();
if (pasteId == null) {
Message.EDITOR_UPLOAD_FAILURE.send(sender);
return CommandResult.STATE_ERROR;

View File

@ -102,7 +102,7 @@ public class EditorCommand extends SingleCommand {
JsonObject payload = WebEditor.formPayload(holders, sender, label, plugin);
// upload the payload data to gist
String pasteId = StandardPastebin.BYTEBIN.postJson(payload).id();
String pasteId = StandardPastebin.BYTEBIN.postJson(payload, true).id();
if (pasteId == null) {
Message.EDITOR_UPLOAD_FAILURE.send(sender);
return CommandResult.STATE_ERROR;

View File

@ -167,7 +167,7 @@ public class TreeView {
)
.toJson();
return StandardPastebin.BYTEBIN.postJson(payload).id();
return StandardPastebin.BYTEBIN.postJson(payload, true).id();
}
}

View File

@ -27,16 +27,53 @@ package me.lucko.luckperms.common.utils.web;
import com.google.gson.JsonElement;
/**
* Represents a pastebin service
*/
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);
/**
* Gets the raw url of a paste's data from an id
*
* @param id the id
* @return a url
*/
String getRawUrl(String id);
/**
* Encapsulates the properties of a specific "paste" entry
*/
interface Paste {
/**
* Gets the url of the paste
*
* @return the url
*/
String url();
/**
* Gets the unique id of the paste
*
* @return the id
*/
String id();
}

View File

@ -37,13 +37,15 @@ import okhttp3.Response;
import okhttp3.ResponseBody;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPOutputStream;
public enum StandardPastebin implements Pastebin {
@ -98,28 +100,44 @@ public enum StandardPastebin implements Pastebin {
protected abstract String parseIdFromResult(BufferedReader reader);
@Override
public Pastebin.Paste postJson(JsonElement content) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(bytes))) {
public Pastebin.Paste postJson(JsonElement content, boolean compress) {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
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);
} catch (IOException e) {
throw new RuntimeException(e);
}
return post(RequestBody.create(JSON_TYPE, bytes.toByteArray()));
return post(RequestBody.create(JSON_TYPE, byteOut.toByteArray()), compress);
}
@Override
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) {
Request request = new Request.Builder()
private Pastebin.Paste post(RequestBody body, boolean compressed) {
Request.Builder requestBuilder = new Request.Builder()
.url(getPostUrl())
.post(body)
.build();
.post(body);
if (compressed) {
requestBuilder.header("Content-Encoding", "gzip");
}
Request request = requestBuilder.build();
try (Response response = HttpClient.makeCall(request)) {
try (ResponseBody responseBody = response.body()) {
if (responseBody == null) {

View File

@ -56,11 +56,11 @@ public class VerboseListener {
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
// 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
private static final int STACK_TRUNCATION_CHAT = 15;
// 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()
.ignoreClassStartingWith("me.lucko.luckperms.")
@ -222,7 +222,7 @@ public class VerboseListener {
.add("data", data)
.toJson();
return StandardPastebin.BYTEBIN.postJson(payload).id();
return StandardPastebin.BYTEBIN.postJson(payload, true).id();
}
private static String getTristateColor(Tristate tristate) {