mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2024-11-21 20:56:45 +01:00
switch to InvendoPaster "library"
This commit is contained in:
parent
4da7aa38ea
commit
14fa738fbe
@ -66,6 +66,7 @@ dependencies {
|
||||
// Our libraries
|
||||
implementation("com.intellectualsites.arkitektonika:Arkitektonika-Client:2.0-SNAPSHOT")
|
||||
implementation("com.intellectualsites.http:HTTP4J:1.1-SNAPSHOT")
|
||||
implementation("com.intellectualsites.paster:Paster:1.0-SNAPSHOT")
|
||||
|
||||
// Adventure
|
||||
implementation("net.kyori:adventure-platform-bukkit:4.0.0-SNAPSHOT")
|
||||
@ -97,6 +98,7 @@ tasks.named<ShadowJar>("shadowJar") {
|
||||
relocate("com.intellectualsites.services", "com.plotsquared.core.services")
|
||||
relocate("com.intellectualsites.arkitektonika", "com.plotsquared.core.arkitektonika")
|
||||
relocate("com.intellectualsites.http", "com.plotsquared.core.http")
|
||||
relocate("com.intellectualsites.paster", "com.plotsquared.core.paster")
|
||||
|
||||
// Get rid of all the libs which are 100% unused.
|
||||
minimize()
|
||||
|
@ -46,6 +46,7 @@ dependencies {
|
||||
exclude(group = "com.google.guava")
|
||||
}
|
||||
api("com.intellectualsites.arkitektonika:Arkitektonika-Client:2.0-SNAPSHOT")
|
||||
api("com.intellectualsites.paster:Paster:1.0-SNAPSHOT")
|
||||
}
|
||||
|
||||
tasks.processResources {
|
||||
|
@ -28,6 +28,7 @@ package com.plotsquared.core.command;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.inject.Inject;
|
||||
import com.intellectualsites.paster.IncendoPaster;
|
||||
import com.plotsquared.core.PlotSquared;
|
||||
import com.plotsquared.core.configuration.Settings;
|
||||
import com.plotsquared.core.configuration.caption.TranslatableCaption;
|
||||
@ -35,21 +36,16 @@ import com.plotsquared.core.inject.annotations.ConfigFile;
|
||||
import com.plotsquared.core.inject.annotations.WorldFile;
|
||||
import com.plotsquared.core.player.PlotPlayer;
|
||||
import com.plotsquared.core.util.PremiumVerification;
|
||||
import com.plotsquared.core.util.net.IncendoPaster;
|
||||
import com.plotsquared.core.util.task.TaskManager;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.RuntimeMXBean;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@CommandDeclaration(command = "debugpaste",
|
||||
aliases = "dp",
|
||||
@ -69,22 +65,9 @@ public class DebugPaste extends SubCommand {
|
||||
this.worldfile = worldFile;
|
||||
}
|
||||
|
||||
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) {
|
||||
TaskManager.runTaskAsync(() -> {
|
||||
try {
|
||||
|
||||
StringBuilder b = new StringBuilder();
|
||||
b.append(
|
||||
"# Welcome to this paste\n# It is meant to provide us at IntellectualSites with better information about your "
|
||||
@ -139,7 +122,7 @@ public class DebugPaste extends SubCommand {
|
||||
throw new IOException("The latest.log is larger than 14MB. Please reboot your server and submit a new paste.");
|
||||
}
|
||||
incendoPaster
|
||||
.addFile(new IncendoPaster.PasteFile("latest.log", readFile(logFile)));
|
||||
.addFile(logFile);
|
||||
} catch (IOException ignored) {
|
||||
player.sendMessage(
|
||||
TranslatableCaption.of("debugpaste.latest_log"),
|
||||
@ -148,15 +131,13 @@ public class DebugPaste extends SubCommand {
|
||||
}
|
||||
|
||||
try {
|
||||
incendoPaster.addFile(new IncendoPaster.PasteFile("settings.yml",
|
||||
readFile(this.configFile)));
|
||||
incendoPaster.addFile(this.configFile);
|
||||
} catch (final IllegalArgumentException ignored) {
|
||||
player.sendMessage(TranslatableCaption.of("debugpaste.empty_file"),
|
||||
Template.of("file", "settings.yml"));
|
||||
}
|
||||
try {
|
||||
incendoPaster.addFile(new IncendoPaster.PasteFile("worlds.yml",
|
||||
readFile(this.worldfile)));
|
||||
incendoPaster.addFile(this.worldfile);
|
||||
} catch (final IllegalArgumentException ignored) {
|
||||
player.sendMessage(TranslatableCaption.of("debugpaste.empty_file"),
|
||||
Template.of("file", "worlds.yml"));
|
||||
@ -165,8 +146,7 @@ public class DebugPaste extends SubCommand {
|
||||
try {
|
||||
final File MultiverseWorlds = new File(PlotSquared.platform().getDirectory(),
|
||||
"../Multiverse-Core/worlds.yml");
|
||||
incendoPaster.addFile(new IncendoPaster.PasteFile("MultiverseCore/worlds.yml",
|
||||
readFile(MultiverseWorlds)));
|
||||
incendoPaster.addFile(MultiverseWorlds);
|
||||
} catch (final IOException ignored) {
|
||||
player.sendMessage(TranslatableCaption.of("debugpaste.skip_multiverse"),
|
||||
Template.of("file", "worlds.yml"));
|
||||
|
@ -1,225 +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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.plotsquared.core.util.net;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
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 {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger("P2/" + IncendoPaster.class);
|
||||
|
||||
/**
|
||||
* 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;
|
||||
logger.error("Paste too big > size: {}MB", 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -41,7 +41,7 @@ allprojects {
|
||||
}
|
||||
|
||||
maven {
|
||||
name = "IntellectualSites Repository"
|
||||
name = "IntellectualSites Snapshots Repository"
|
||||
url = uri("https://mvn.intellectualsites.com/content/repositories/snapshots")
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user