add paste.gg paste service

This commit is contained in:
Kermina Awad 2020-06-14 16:04:17 -04:00
parent 707eae92a8
commit e01c646562
4 changed files with 101 additions and 2 deletions

View File

@ -44,13 +44,13 @@ public class VersionCommand extends MultiverseCommand {
public VersionCommand(MultiverseCore plugin) {
super(plugin);
this.setName("Multiverse Version");
this.setCommandUsage("/mv version " + ChatColor.GOLD + "-[bh] [--include-plugin-list]");
this.setCommandUsage("/mv version " + ChatColor.GOLD + "-[bhp] [--include-plugin-list]");
this.setArgRange(0, 2);
this.addKey("mv version");
this.addKey("mvv");
this.addKey("mvversion");
this.setPermission("multiverse.core.version",
"Dumps version info to the console, optionally to pastebin.com with -b, or to hastebin.com using -h.", PermissionDefault.TRUE);
"Dumps version info to the console, optionally to pastebin.com with -b, to hastebin.com using -h, or to paste.gg with -p.", PermissionDefault.TRUE);
}
private String getLegacyString() {
@ -181,6 +181,9 @@ public class VersionCommand extends MultiverseCommand {
} else if (CommandHandler.hasFlag("-h", args)) {
// private post to hastebin
pasteUrl = postToService(PasteServiceType.HASTEBIN, true, data, files);
} else if (CommandHandler.hasFlag("-p", args)) {
// private post to paste.gg
pasteUrl = postToService(PasteServiceType.PASTEGG, true, data, files);
} else {
return;
}

View File

@ -0,0 +1,90 @@
package com.onarandombox.MultiverseCore.utils.webpaste;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* Pastes to {@code paste.gg}.
*/
class PasteGGPasteService extends PasteService {
private final boolean isPrivate;
private static final String PASTEGG_POST_REQUEST = "https://api.paste.gg/v1/pastes";
PasteGGPasteService(boolean isPrivate) {
super(PASTEGG_POST_REQUEST, null);
this.isPrivate = isPrivate;
}
/**
* {@inheritDoc}
*/
@Override
String encodeData(String data) {
Map<String, String> mapData = new HashMap<String, String>();
mapData.put("multiverse.txt", data);
return this.encodeData(mapData);
}
/**
* {@inheritDoc}
*/
@Override
String encodeData(Map<String, String> files) {
JSONObject root = new JSONObject();
root.put("name", "Multiverse-Core Debug Info");
root.put("visibility", this.isPrivate ? "unlisted" : "public");
JSONArray fileList = new JSONArray();
for (Map.Entry<String, String> entry : files.entrySet()) {
JSONObject fileObject = new JSONObject();
JSONObject contentObject = new JSONObject();
fileObject.put("name", entry.getKey());
fileObject.put("content", contentObject);
contentObject.put("format", "text");
contentObject.put("value", entry.getValue());
fileList.add(fileObject);
}
root.put("files", fileList);
return root.toJSONString();
}
/**
* {@inheritDoc}
*/
@Override
public String postData(String data) throws PasteFailedException {
try {
String stringJSON = this.exec(encodeData(data), ContentType.JSON);
return (String) ((JSONObject) ((JSONObject) new JSONParser().parse(stringJSON)).get("result")).get("id");
} catch (IOException | ParseException e) {
throw new PasteFailedException(e);
}
}
/**
* {@inheritDoc}
*/
@Override
public String postData(Map<String, String> data) throws PasteFailedException {
try {
String stringJSON = this.exec(encodeData(data), ContentType.JSON);
return "https://paste.gg/" + ((JSONObject) ((JSONObject) new JSONParser().parse(stringJSON)).get("result")).get("id");
} catch (IOException | ParseException e) {
throw new PasteFailedException(e);
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean supportsMultiFile() {
return true;
}
}

View File

@ -14,6 +14,8 @@ public class PasteServiceFactory {
*/
public static PasteService getService(PasteServiceType type, boolean isPrivate) {
switch(type) {
case PASTEGG:
return new PasteGGPasteService(isPrivate);
case PASTEBIN:
return new PastebinPasteService(isPrivate);
case HASTEBIN:

View File

@ -7,6 +7,10 @@ package com.onarandombox.MultiverseCore.utils.webpaste;
* @see PasteServiceFactory
*/
public enum PasteServiceType {
/**
* @see PasteGGPasteService
*/
PASTEGG,
/**
* @see PastebinPasteService
*/