mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-27 11:37:41 +01:00
Separates the Hastebin upload part into an external class
Fixes a bug in the uploading to Hastebin
This commit is contained in:
parent
951432b504
commit
c831ccb9ec
@ -47,6 +47,7 @@ public class PlanCommand extends TreeCommand<Plan> {
|
||||
commands.add(new ReloadCommand(plugin));
|
||||
commands.add(new ManageCommand(plugin));
|
||||
commands.add(new StatusCommand<>(plugin, Permissions.MANAGE.getPermission()));
|
||||
|
||||
if (plugin.getUiServer().isEnabled()) {
|
||||
commands.add(new ListCommand());
|
||||
RegisterCommand registerCommand = new RegisterCommand(plugin);
|
||||
|
@ -1,25 +1,12 @@
|
||||
package main.java.com.djrapitops.plan.utilities.file.dump;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import main.java.com.djrapitops.plan.Log;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
* @since 3.7.0
|
||||
* @since 3.6.2
|
||||
*/
|
||||
public class DumpLog {
|
||||
|
||||
@ -99,66 +86,7 @@ public class DumpLog {
|
||||
* @return The link to the Dump Log
|
||||
*/
|
||||
String upload() {
|
||||
List<String> parts = ImmutableList.copyOf(split()).reverse();
|
||||
|
||||
String lastLink = null;
|
||||
for (String part : parts) {
|
||||
if (lastLink != null) {
|
||||
part += "\n" + lastLink;
|
||||
}
|
||||
|
||||
lastLink = upload(part);
|
||||
}
|
||||
|
||||
return lastLink;
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads the content to Hastebin using HTTPS and POST
|
||||
*
|
||||
* @param content The content
|
||||
* @return The link to the content
|
||||
*/
|
||||
private String upload(String content) {
|
||||
HttpsURLConnection connection = null;
|
||||
try {
|
||||
URL url = new URL("https://hastebin.com/documents");
|
||||
connection = (HttpsURLConnection) url.openConnection();
|
||||
|
||||
connection.setRequestProperty("Content-length", String.valueOf(content.length()));
|
||||
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
connection.setRequestProperty("User-Agent", "Mozilla/4.0");
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setDoInput(true);
|
||||
connection.setDoOutput(true);
|
||||
|
||||
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
|
||||
wr.writeBytes(this.toString());
|
||||
wr.flush();
|
||||
wr.close();
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject json = (JSONObject) parser.parse(reader.readLine());
|
||||
|
||||
return "https://hastebin.com/" + json.get("key");
|
||||
} catch (IOException | ParseException e) {
|
||||
Log.toLog("DumpLog.upload", e);
|
||||
return "Error";
|
||||
} finally {
|
||||
if (connection != null) {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits the content of the DumpLog into parts
|
||||
*
|
||||
* @return The splitted content
|
||||
*/
|
||||
private Iterable<String> split() {
|
||||
return Splitter.fixedLength(390000).split(this.toString());
|
||||
return Hastebin.safeUpload(this.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -23,10 +23,13 @@ import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
* @since 3.7.0
|
||||
* @since 3.6.2
|
||||
*/
|
||||
public class DumpUtils {
|
||||
|
||||
/**
|
||||
* Constructor used to hide the public constructor
|
||||
*/
|
||||
private DumpUtils() {
|
||||
throw new IllegalStateException("Utility Class");
|
||||
}
|
||||
|
@ -0,0 +1,127 @@
|
||||
package main.java.com.djrapitops.plan.utilities.file.dump;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import main.java.com.djrapitops.plan.Log;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
* @since 3.6.4
|
||||
*/
|
||||
class Hastebin {
|
||||
|
||||
/**
|
||||
* Constructor used to hide the public constructor
|
||||
*/
|
||||
private Hastebin() {
|
||||
throw new IllegalStateException("Utility Class");
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads the content safely to Hastebin.
|
||||
* Longer than allowed content is being uploaded too.
|
||||
*
|
||||
* @return The link to the Dump Log
|
||||
* @implNote Splits the content into parts of 390.000 chars each,
|
||||
* uploads the parts in reverse order and adds the last link (if present)
|
||||
* at each end of the following part, that's why the redundancy of 10.000 chars exists.
|
||||
* @see #split(String)
|
||||
*/
|
||||
static String safeUpload(String content) {
|
||||
List<String> parts = ImmutableList.copyOf(split(content)).reverse();
|
||||
|
||||
String lastLink = null;
|
||||
try {
|
||||
for (String part : parts) {
|
||||
if (lastLink != null) {
|
||||
part += "\n" + lastLink;
|
||||
}
|
||||
|
||||
lastLink = upload(part);
|
||||
}
|
||||
} catch (IOException | ParseException e) {
|
||||
Log.toLog("DumpLog.upload", e);
|
||||
}
|
||||
|
||||
return lastLink;
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads the content to Hastebin using HTTPS and POST
|
||||
*
|
||||
* @param content The content
|
||||
* @return The link to the content
|
||||
*/
|
||||
private static String upload(String content) throws IOException, ParseException {
|
||||
HttpsURLConnection connection = null;
|
||||
try {
|
||||
URL url = new URL("https://hastebin.com/documents");
|
||||
connection = (HttpsURLConnection) url.openConnection();
|
||||
|
||||
connection.setRequestProperty("Content-length", String.valueOf(content.length()));
|
||||
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
connection.setRequestProperty("User-Agent", "Mozilla/4.0");
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setDoInput(true);
|
||||
connection.setDoOutput(true);
|
||||
|
||||
writeData(connection.getOutputStream(), content);
|
||||
|
||||
return getHastebinLink(connection.getInputStream());
|
||||
} finally {
|
||||
if (connection != null) {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the data to the {@link OutputStream}
|
||||
*
|
||||
* @param outputStream The OutputStream that the data should be written to
|
||||
* @throws IOException when an error at the writing of the data happens
|
||||
*/
|
||||
private static void writeData(OutputStream outputStream, String content) throws IOException {
|
||||
try (DataOutputStream wr = new DataOutputStream(outputStream)) {
|
||||
wr.writeBytes(content);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Hastebin Link from the {@link InputStream}
|
||||
*
|
||||
* @param inputStream The InputStream in which the Hastebin Key is included (encoded in JSON)
|
||||
* @return The full Hastebin Link ({@code https://hastebin.com/ + key})
|
||||
* @throws IOException when an error at the reading of the InputStream happens
|
||||
* @throws ParseException when an error at the parsing of the line that was read happens
|
||||
*/
|
||||
private static String getHastebinLink(InputStream inputStream) throws IOException, ParseException {
|
||||
String key;
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject json = (JSONObject) parser.parse(reader.readLine());
|
||||
|
||||
key = (String) json.get("key");
|
||||
}
|
||||
|
||||
return "https://hastebin.com/" + key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits the content in parts of 390.000 chars each
|
||||
*
|
||||
* @return The content that was splitted
|
||||
*/
|
||||
private static Iterable<String> split(String content) {
|
||||
return Splitter.fixedLength(390000).split(content);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user