Switch to IncendoPaster "library"

This commit is contained in:
dordsor21 2020-12-19 16:20:44 +00:00
parent 5147d67318
commit e4a8459798
No known key found for this signature in database
GPG Key ID: 1E53E88969FFCF0B
5 changed files with 23 additions and 246 deletions

View File

@ -99,12 +99,14 @@ shadowJar {
include(dependency("org.bstats:bstats-bukkit:1.7")) include(dependency("org.bstats:bstats-bukkit:1.7"))
include(dependency("org.khelekore:prtree:1.7.0-SNAPSHOT")) include(dependency("org.khelekore:prtree:1.7.0-SNAPSHOT"))
include(dependency("com.sk89q:squirrelid:1.0.0-SNAPSHOT")) include(dependency("com.sk89q:squirrelid:1.0.0-SNAPSHOT"))
include(dependency("com.intellectualsites.paster:Paster:1.0-SNAPSHOT"))
} }
relocate('net.kyori.text', 'com.plotsquared.formatting.text') relocate('net.kyori.text', 'com.plotsquared.formatting.text')
relocate("io.papermc.lib", "com.plotsquared.bukkit.paperlib") relocate("io.papermc.lib", "com.plotsquared.bukkit.paperlib")
relocate("org.bstats", "com.plotsquared.metrics") relocate("org.bstats", "com.plotsquared.metrics")
relocate('com.sk89q.squirrelid', 'com.plotsquared.squirrelid') relocate('com.sk89q.squirrelid', 'com.plotsquared.squirrelid')
relocate('org.khelekore.prtree', 'com.plotsquared.prtree') relocate('org.khelekore.prtree', 'com.plotsquared.prtree')
relocate('com.intellectualsites.paster', 'com.plotsquared.core.paster')
archiveFileName = "${project.name}-${parent.version}.jar" archiveFileName = "${project.name}-${parent.version}.jar"
destinationDirectory = file "../target" destinationDirectory = file "../target"
} }

View File

@ -18,6 +18,7 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.4.0") implementation("org.jetbrains.kotlin:kotlin-stdlib:1.4.0")
implementation("org.jetbrains:annotations:20.1.0") implementation("org.jetbrains:annotations:20.1.0")
implementation("org.khelekore:prtree:1.7.0-SNAPSHOT") implementation("org.khelekore:prtree:1.7.0-SNAPSHOT")
implementation("com.intellectualsites.paster:Paster:1.0-SNAPSHOT"){ transitive = false }
} }
sourceCompatibility = 1.8 sourceCompatibility = 1.8
@ -76,11 +77,13 @@ shadowJar {
include(dependency("net.kyori:text-serializer-legacy:3.0.2")) include(dependency("net.kyori:text-serializer-legacy:3.0.2"))
include(dependency("net.kyori:text-serializer-plain:3.0.2")) include(dependency("net.kyori:text-serializer-plain:3.0.2"))
include(dependency("org.khelekore:prtree:1.7.0-SNAPSHOT")) include(dependency("org.khelekore:prtree:1.7.0-SNAPSHOT"))
include(dependency("com.intellectualsites.paster:Paster:1.0-SNAPSHOT"))
} }
relocate('net.kyori.text', 'com.plotsquared.formatting.text') relocate('net.kyori.text', 'com.plotsquared.formatting.text')
relocate("org.json", "com.plotsquared.json") { relocate("org.json", "com.plotsquared.json") {
exclude "org/json/simple/**" exclude "org/json/simple/**"
} }
relocate('com.intellectualsites.paster', 'com.plotsquared.core.paster')
} }
shadowJar.doLast { shadowJar.doLast {

View File

@ -98,6 +98,18 @@
<version>1.7.0-SNAPSHOT</version> <version>1.7.0-SNAPSHOT</version>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<groupId>com.intellectualsites.paster</groupId>
<artifactId>Paster</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>runtime</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>

View File

@ -27,13 +27,13 @@ package com.plotsquared.core.command;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.intellectualsites.paster.IncendoPaster;
import com.plotsquared.core.PlotSquared; import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.MainUtil;
import com.plotsquared.core.util.PremiumVerification; import com.plotsquared.core.util.PremiumVerification;
import com.plotsquared.core.util.net.IncendoPaster;
import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.util.task.TaskManager;
import lombok.NonNull; import lombok.NonNull;
@ -59,22 +59,9 @@ import java.util.stream.Collectors;
requiredType = RequiredType.NONE) requiredType = RequiredType.NONE)
public class DebugPaste extends SubCommand { public class DebugPaste extends SubCommand {
private static String readFile(@NonNull final File file) throws IOException {
final List<String> lines;
try (final BufferedReader reader = new BufferedReader(new FileReader(file))) {
lines = reader.lines().collect(Collectors.toList());
}
final StringBuilder content = new StringBuilder();
for (int i = Math.max(0, lines.size() - 1000); i < lines.size(); i++) {
content.append(lines.get(i)).append("\n");
}
return content.toString();
}
@Override public boolean onCommand(final PlotPlayer<?> player, String[] args) { @Override public boolean onCommand(final PlotPlayer<?> player, String[] args) {
TaskManager.runTaskAsync(() -> { TaskManager.runTaskAsync(() -> {
try { try {
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
b.append( b.append(
"# Welcome to this paste\n# It is meant to provide us at IntellectualSites with better information about your " "# Welcome to this paste\n# It is meant to provide us at IntellectualSites with better information about your "
@ -128,28 +115,24 @@ public class DebugPaste extends SubCommand {
if (Files.size(logFile.toPath()) > 14_000_000) { if (Files.size(logFile.toPath()) > 14_000_000) {
throw new IOException("Too big..."); throw new IOException("Too big...");
} }
incendoPaster incendoPaster.addFile(logFile);
.addFile(new IncendoPaster.PasteFile("latest.log", readFile(logFile)));
} catch (IOException ignored) { } catch (IOException ignored) {
MainUtil MainUtil
.sendMessage(player, "&clatest.log is too big to be pasted, will ignore"); .sendMessage(player, "&clatest.log is too big to be pasted, will ignore");
} }
try { try {
incendoPaster.addFile(new IncendoPaster.PasteFile("settings.yml", incendoPaster.addFile(PlotSquared.get().configFile);
readFile(PlotSquared.get().configFile)));
} catch (final IllegalArgumentException ignored) { } catch (final IllegalArgumentException ignored) {
MainUtil.sendMessage(player, "&cSkipping settings.yml because it's empty"); MainUtil.sendMessage(player, "&cSkipping settings.yml because it's empty");
} }
try { try {
incendoPaster.addFile(new IncendoPaster.PasteFile("worlds.yml", incendoPaster.addFile(PlotSquared.get().worldsFile);
readFile(PlotSquared.get().worldsFile)));
} catch (final IllegalArgumentException ignored) { } catch (final IllegalArgumentException ignored) {
MainUtil.sendMessage(player, "&cSkipping worlds.yml because it's empty"); MainUtil.sendMessage(player, "&cSkipping worlds.yml because it's empty");
} }
try { try {
incendoPaster.addFile(new IncendoPaster.PasteFile("PlotSquared.use_THIS.yml", incendoPaster.addFile(PlotSquared.get().translationFile);
readFile(PlotSquared.get().translationFile)));
} catch (final IllegalArgumentException ignored) { } catch (final IllegalArgumentException ignored) {
MainUtil.sendMessage(player, MainUtil.sendMessage(player,
"&cSkipping PlotSquared.use_THIS.yml because it's empty"); "&cSkipping PlotSquared.use_THIS.yml because it's empty");
@ -158,8 +141,7 @@ public class DebugPaste extends SubCommand {
try { try {
final File MultiverseWorlds = new File(PlotSquared.get().IMP.getDirectory(), final File MultiverseWorlds = new File(PlotSquared.get().IMP.getDirectory(),
"../Multiverse-Core/worlds.yml"); "../Multiverse-Core/worlds.yml");
incendoPaster.addFile(new IncendoPaster.PasteFile("MultiverseCore/worlds.yml", incendoPaster.addFile(MultiverseWorlds);
readFile(MultiverseWorlds)));
} catch (final IOException ignored) { } catch (final IOException ignored) {
MainUtil.sendMessage(player, MainUtil.sendMessage(player,
"&cSkipping Multiverse worlds.yml because the plugin is not in use"); "&cSkipping Multiverse worlds.yml because the plugin is not in use");

View File

@ -1,222 +0,0 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* PlotSquared plot management system for Minecraft
* Copyright (C) 2020 IntellectualSites
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.plotsquared.core.util.net;
import com.google.common.base.Charsets;
import com.plotsquared.core.PlotSquared;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Locale;
/**
* Single class paster for the Incendo paste service
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public final class IncendoPaster {
/**
* Upload service URL
*/
public static final String UPLOAD_PATH = "https://athion.net/ISPaster/paste/upload";
/**
* Valid paste applications
*/
public static final Collection<String> VALID_APPLICATIONS =
Arrays.asList("plotsquared", "fastasyncworldedit", "incendopermissions", "kvantum");
private final Collection<PasteFile> files = new ArrayList<>();
private final String pasteApplication;
/**
* Construct a new paster
*
* @param pasteApplication The application that is sending the paste
*/
public IncendoPaster(final String pasteApplication) {
if (pasteApplication == null || pasteApplication.isEmpty()) {
throw new IllegalArgumentException("paste application cannot be null, nor empty");
}
if (!VALID_APPLICATIONS.contains(pasteApplication.toLowerCase(Locale.ENGLISH))) {
throw new IllegalArgumentException(
String.format("Unknown application name: %s", pasteApplication));
}
this.pasteApplication = pasteApplication;
}
/**
* Get an immutable collection containing all the files that have been added to this paster
*
* @return Unmodifiable collection
*/
public final Collection<PasteFile> getFiles() {
return Collections.unmodifiableCollection(this.files);
}
/**
* Add a file to the paster
*
* @param file File to paste
*/
public void addFile(final PasteFile file) {
if (file == null) {
throw new IllegalArgumentException("File cannot be null");
}
// Check to see that no duplicate files are submitted
for (final PasteFile pasteFile : this.files) {
if (pasteFile.fileName.equalsIgnoreCase(file.getFileName())) {
throw new IllegalArgumentException(
String.format("Found duplicate file with name %s", file.getFileName()));
}
}
this.files.add(file);
}
/**
* Create a JSON string from the submitted information
*
* @return compiled JSON string
*/
private String toJsonString() {
final StringBuilder builder = new StringBuilder("{\n");
builder.append("\"paste_application\": \"").append(this.pasteApplication)
.append("\",\n\"files\": \"");
Iterator<PasteFile> fileIterator = this.files.iterator();
while (fileIterator.hasNext()) {
final PasteFile file = fileIterator.next();
builder.append(file.getFileName());
if (fileIterator.hasNext()) {
builder.append(",");
}
}
builder.append("\",\n");
fileIterator = this.files.iterator();
while (fileIterator.hasNext()) {
final PasteFile file = fileIterator.next();
builder.append("\"file-").append(file.getFileName()).append("\": \"")
.append(file.getContent().replaceAll("\"", "\\\\\"")).append("\"");
if (fileIterator.hasNext()) {
builder.append(",\n");
}
}
builder.append("\n}");
return builder.toString();
}
/**
* Upload the paste and return the status message
*
* @return Status message
* @throws Throwable any and all exceptions
*/
public final String upload() throws Throwable {
final URL url = new URL(UPLOAD_PATH);
final URLConnection connection = url.openConnection();
final HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
final byte[] content = toJsonString().getBytes(Charsets.UTF_8);
httpURLConnection.setFixedLengthStreamingMode(content.length);
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setRequestProperty("Accept", "*/*");
httpURLConnection.connect();
try (final OutputStream stream = httpURLConnection.getOutputStream()) {
stream.write(content);
}
if (!httpURLConnection.getResponseMessage().contains("OK")) {
if (httpURLConnection.getResponseCode() == 413) {
final long size = content.length;
PlotSquared.debug(String.format("Paste Too Big > Size: %dMB", size / 1_000_000));
}
throw new IllegalStateException(String
.format("Server returned status: %d %s", httpURLConnection.getResponseCode(),
httpURLConnection.getResponseMessage()));
}
final StringBuilder input = new StringBuilder();
try (final BufferedReader inputStream = new BufferedReader(
new InputStreamReader(httpURLConnection.getInputStream()))) {
String line;
while ((line = inputStream.readLine()) != null) {
input.append(line).append("\n");
}
}
return input.toString();
}
/**
* Simple class that represents a paste file
*/
public static class PasteFile {
private final String fileName;
private final String content;
/**
* Construct a new paste file
*
* @param fileName File name, cannot be empty, nor null
* @param content File content, cannot be empty, nor null
*/
public PasteFile(final String fileName, final String content) {
if (fileName == null || fileName.isEmpty()) {
throw new IllegalArgumentException("file name cannot be null, nor empty");
}
if (content == null || content.isEmpty()) {
throw new IllegalArgumentException("content cannot be null, nor empty");
}
this.fileName = fileName;
this.content = content;
}
/**
* Get the file name
*
* @return File name
*/
public String getFileName() {
return this.fileName;
}
/**
* Get the file content as a single string
*
* @return File content
*/
public String getContent() {
return this.content;
}
}
}