From e4a8459798c94fdff3966285f687a052a4f58268 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Sat, 19 Dec 2020 16:20:44 +0000 Subject: [PATCH] Switch to IncendoPaster "library" --- Bukkit/build.gradle | 2 + Core/build.gradle | 3 + Core/pom.xml | 12 + .../plotsquared/core/command/DebugPaste.java | 30 +-- .../core/util/net/IncendoPaster.java | 222 ------------------ 5 files changed, 23 insertions(+), 246 deletions(-) delete mode 100644 Core/src/main/java/com/plotsquared/core/util/net/IncendoPaster.java diff --git a/Bukkit/build.gradle b/Bukkit/build.gradle index ff2e398fd..c5336198f 100644 --- a/Bukkit/build.gradle +++ b/Bukkit/build.gradle @@ -99,12 +99,14 @@ shadowJar { include(dependency("org.bstats:bstats-bukkit:1.7")) include(dependency("org.khelekore:prtree:1.7.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("io.papermc.lib", "com.plotsquared.bukkit.paperlib") relocate("org.bstats", "com.plotsquared.metrics") relocate('com.sk89q.squirrelid', 'com.plotsquared.squirrelid') relocate('org.khelekore.prtree', 'com.plotsquared.prtree') + relocate('com.intellectualsites.paster', 'com.plotsquared.core.paster') archiveFileName = "${project.name}-${parent.version}.jar" destinationDirectory = file "../target" } diff --git a/Core/build.gradle b/Core/build.gradle index cb8301a47..4eab46f28 100644 --- a/Core/build.gradle +++ b/Core/build.gradle @@ -18,6 +18,7 @@ dependencies { implementation("org.jetbrains.kotlin:kotlin-stdlib:1.4.0") implementation("org.jetbrains:annotations:20.1.0") implementation("org.khelekore:prtree:1.7.0-SNAPSHOT") + implementation("com.intellectualsites.paster:Paster:1.0-SNAPSHOT"){ transitive = false } } sourceCompatibility = 1.8 @@ -76,11 +77,13 @@ shadowJar { include(dependency("net.kyori:text-serializer-legacy:3.0.2")) include(dependency("net.kyori:text-serializer-plain:3.0.2")) 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("org.json", "com.plotsquared.json") { exclude "org/json/simple/**" } + relocate('com.intellectualsites.paster', 'com.plotsquared.core.paster') } shadowJar.doLast { diff --git a/Core/pom.xml b/Core/pom.xml index 799c0a5a5..2669af8c1 100644 --- a/Core/pom.xml +++ b/Core/pom.xml @@ -98,6 +98,18 @@ 1.7.0-SNAPSHOT runtime + + com.intellectualsites.paster + Paster + 1.0-SNAPSHOT + runtime + + + * + * + + + junit junit diff --git a/Core/src/main/java/com/plotsquared/core/command/DebugPaste.java b/Core/src/main/java/com/plotsquared/core/command/DebugPaste.java index 3928b9fff..eb6062e4d 100644 --- a/Core/src/main/java/com/plotsquared/core/command/DebugPaste.java +++ b/Core/src/main/java/com/plotsquared/core/command/DebugPaste.java @@ -27,13 +27,13 @@ package com.plotsquared.core.command; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import com.intellectualsites.paster.IncendoPaster; import com.plotsquared.core.PlotSquared; import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.PremiumVerification; -import com.plotsquared.core.util.net.IncendoPaster; import com.plotsquared.core.util.task.TaskManager; import lombok.NonNull; @@ -59,22 +59,9 @@ import java.util.stream.Collectors; requiredType = RequiredType.NONE) public class DebugPaste extends SubCommand { - private static String readFile(@NonNull final File file) throws IOException { - final List 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 " @@ -128,28 +115,24 @@ public class DebugPaste extends SubCommand { if (Files.size(logFile.toPath()) > 14_000_000) { throw new IOException("Too big..."); } - incendoPaster - .addFile(new IncendoPaster.PasteFile("latest.log", readFile(logFile))); + incendoPaster.addFile(logFile); } catch (IOException ignored) { MainUtil .sendMessage(player, "&clatest.log is too big to be pasted, will ignore"); } try { - incendoPaster.addFile(new IncendoPaster.PasteFile("settings.yml", - readFile(PlotSquared.get().configFile))); + incendoPaster.addFile(PlotSquared.get().configFile); } catch (final IllegalArgumentException ignored) { MainUtil.sendMessage(player, "&cSkipping settings.yml because it's empty"); } try { - incendoPaster.addFile(new IncendoPaster.PasteFile("worlds.yml", - readFile(PlotSquared.get().worldsFile))); + incendoPaster.addFile(PlotSquared.get().worldsFile); } catch (final IllegalArgumentException ignored) { MainUtil.sendMessage(player, "&cSkipping worlds.yml because it's empty"); } try { - incendoPaster.addFile(new IncendoPaster.PasteFile("PlotSquared.use_THIS.yml", - readFile(PlotSquared.get().translationFile))); + incendoPaster.addFile(PlotSquared.get().translationFile); } catch (final IllegalArgumentException ignored) { MainUtil.sendMessage(player, "&cSkipping PlotSquared.use_THIS.yml because it's empty"); @@ -158,8 +141,7 @@ public class DebugPaste extends SubCommand { try { final File MultiverseWorlds = new File(PlotSquared.get().IMP.getDirectory(), "../Multiverse-Core/worlds.yml"); - incendoPaster.addFile(new IncendoPaster.PasteFile("MultiverseCore/worlds.yml", - readFile(MultiverseWorlds))); + incendoPaster.addFile(MultiverseWorlds); } catch (final IOException ignored) { MainUtil.sendMessage(player, "&cSkipping Multiverse worlds.yml because the plugin is not in use"); diff --git a/Core/src/main/java/com/plotsquared/core/util/net/IncendoPaster.java b/Core/src/main/java/com/plotsquared/core/util/net/IncendoPaster.java deleted file mode 100644 index 46ae63f7d..000000000 --- a/Core/src/main/java/com/plotsquared/core/util/net/IncendoPaster.java +++ /dev/null @@ -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 . - */ -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 VALID_APPLICATIONS = - Arrays.asList("plotsquared", "fastasyncworldedit", "incendopermissions", "kvantum"); - - private final Collection 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 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 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; - } - } - -}