From d9998982d1137035eee882c3f2c856e0183e4fcb Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Thu, 14 Nov 2019 21:20:55 +0000 Subject: [PATCH] Remove IncendoPaste --- .../com/boydti/fawe/util/IncendoPaster.java | 20 +- .../util/paste/ActorCallbackPaste.java | 3 +- .../worldedit/util/paste/IncendoPaste.java | 275 ------------------ 3 files changed, 21 insertions(+), 277 deletions(-) delete mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/util/paste/IncendoPaste.java diff --git a/worldedit-core/src/main/java/com/boydti/fawe/util/IncendoPaster.java b/worldedit-core/src/main/java/com/boydti/fawe/util/IncendoPaster.java index 7c9128453..b2d89863f 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/util/IncendoPaster.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/util/IncendoPaster.java @@ -4,6 +4,7 @@ import com.boydti.fawe.Fawe; import com.google.common.base.Charsets; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import com.sk89q.worldedit.util.paste.Paster; import java.io.*; import java.net.HttpURLConnection; @@ -11,6 +12,7 @@ import java.net.URL; import java.net.URLConnection; import java.nio.file.Files; import java.util.*; +import java.util.concurrent.Callable; /** * Single class paster for the Incendo paste service @@ -18,7 +20,7 @@ import java.util.*; * @author Sauilitired */ @SuppressWarnings({"unused", "WeakerAccess"}) -public final class IncendoPaster { +public final class IncendoPaster implements Paster { /** * Upload service URL @@ -50,6 +52,22 @@ public final class IncendoPaster { this.pasteApplication = pasteApplication; } + @Override + public Callable paste(String content) { + return new PasteTask(content); + } + + private final class PasteTask implements Callable { + + private PasteTask(String content) {} + + @Override + public URL call() throws Exception { + return new URL(debugPaste()); + } + + } + /** * Get an immutable collection containing all the files that have been added to this paster * diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/paste/ActorCallbackPaste.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/paste/ActorCallbackPaste.java index 74d4d31b0..2ead3102f 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/paste/ActorCallbackPaste.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/paste/ActorCallbackPaste.java @@ -19,6 +19,7 @@ package com.sk89q.worldedit.util.paste; +import com.boydti.fawe.util.IncendoPaster; import com.sk89q.worldedit.command.util.AsyncCommandBuilder; import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.util.task.Supervisor; @@ -28,7 +29,7 @@ import java.util.concurrent.Callable; public final class ActorCallbackPaste { - private static final Paster paster = new IncendoPaste("fastasyncworldedit"); + private static final Paster paster = new IncendoPaster("fastasyncworldedit"); private ActorCallbackPaste() { } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/paste/IncendoPaste.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/paste/IncendoPaste.java deleted file mode 100644 index eea5d05cc..000000000 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/paste/IncendoPaste.java +++ /dev/null @@ -1,275 +0,0 @@ -package com.sk89q.worldedit.util.paste; - -import com.boydti.fawe.Fawe; -import com.google.common.base.Charsets; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; - -import java.io.*; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLConnection; -import java.nio.file.Files; -import java.util.*; -import java.util.concurrent.Callable; -import java.util.stream.Collectors; - -/** - * Single class paster for the Incendo paste service - * - * @author Sauilitired - */ -@SuppressWarnings({"unused", "WeakerAccess"}) -public final class IncendoPaste implements Paster{ - - /** - * 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 IncendoPaste(String pasteApplication) { - if (pasteApplication == null || pasteApplication.isEmpty()) { - throw new IllegalArgumentException("paste application cannot be null, nor empty"); - } - if (!VALID_APPLICATIONS.contains(pasteApplication.toLowerCase(Locale.ROOT))) { - throw new IllegalArgumentException( - String.format("Unknown application name: %s", pasteApplication)); - } - this.pasteApplication = pasteApplication; - } - - @Override - public Callable paste(String content) { - return new PasteTask(content); - } - - private final class PasteTask implements Callable { - - private PasteTask(String content) {} - - @Override - public URL call() throws Exception { - return new URL(debugPaste()); - } - - } - - /** - * 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(PasteFile file) { - if (file == null) { - throw new IllegalArgumentException("File cannot be null"); - } - // Check to see that no duplicate files are submitted - for (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 (OutputStream stream = httpURLConnection.getOutputStream()) { - stream.write(content); - } - if (!httpURLConnection.getResponseMessage().contains("OK")) { - throw new IllegalStateException(String.format("Server returned status: %d %s", - httpURLConnection.getResponseCode(), httpURLConnection.getResponseMessage())); - } - final String input; - try (BufferedReader inputStream = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()))) { - input = inputStream.lines().map(line -> line + "\n").collect(Collectors.joining()); - } - return input; - } - - /** - * 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(String fileName, 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; - } - } - - public static String debugPaste() throws IOException { - final IncendoPaste incendoPaster = new IncendoPaste("fastasyncworldedit"); - - StringBuilder b = new StringBuilder(); - b.append( - "# Welcome to this paste\n# It is meant to provide us at IntellectualSites with better information about your " - + "problem\n"); - b.append("\n# Server Information\n"); - b.append("server.platform: ").append(Fawe.imp().getPlatform()).append('\n'); - b.append(Fawe.imp().getDebugInfo()).append('\n'); - b.append("\n\n# YAY! Now, let's see what we can find in your JVM\n"); - Runtime runtime = Runtime.getRuntime(); - b.append("memory.free: ").append(runtime.freeMemory()).append('\n'); - b.append("memory.max: ").append(runtime.maxMemory()).append('\n'); - b.append("java.specification.version: '").append(System.getProperty("java.specification.version")).append("'\n"); - b.append("java.vendor: '").append(System.getProperty("java.vendor")).append("'\n"); - b.append("java.version: '").append(System.getProperty("java.version")).append("'\n"); - b.append("os.arch: '").append(System.getProperty("os.arch")).append("'\n"); - b.append("os.name: '").append(System.getProperty("os.name")).append("'\n"); - b.append("os.version: '").append(System.getProperty("os.version")).append("'\n\n"); - b.append("# Okay :D Great. You are now ready to create your bug report!"); - b.append("\n# You can do so at https://github.com/boy0001/FastAsyncWorldedit/issues"); - b.append("\n# or via our Discord at https://discord.gg/ngZCzbU"); - incendoPaster.addFile(new IncendoPaste.PasteFile("information", b.toString())); - - try { - final File logFile = new File(Fawe.imp().getDirectory(), "../../logs/latest.log"); - final String file; - if (Files.size(logFile.toPath()) > 14_000_000) { - file = "too big :("; - } else { - file = readFile(logFile); - } - incendoPaster.addFile(new IncendoPaste.PasteFile("latest.log", file)); - } catch (IOException ignored) { - } - - incendoPaster.addFile(new PasteFile("config.yml", readFile(new File(Fawe.imp().getDirectory(), "config.yml")))); - incendoPaster.addFile(new PasteFile("config-legacy.yml", readFile(new File(Fawe.imp().getDirectory(), "config-legacy.yml")))); - incendoPaster.addFile(new PasteFile("message.yml", readFile(new File(Fawe.imp().getDirectory(), "message.yml")))); - incendoPaster.addFile(new PasteFile("commands.yml", readFile(new File(Fawe.imp().getDirectory(), "commands.yml")))); - - final String rawResponse; - try { - rawResponse = incendoPaster.upload(); - } catch (Throwable throwable) { - throw new IOException(String.format("Failed to upload files: %s", throwable.getMessage()), throwable); - } - final JsonObject jsonObject = new JsonParser().parse(rawResponse).getAsJsonObject(); - - if (jsonObject.has("created")) { - final String pasteId = jsonObject.get("paste_id").getAsString(); - return String.format("https://athion.net/ISPaster/paste/view/%s", pasteId); - } else { - throw new IOException(String.format("Failed to upload files: %s", - jsonObject.get("response").getAsString())); - } - } - - private static String readFile(File file) throws IOException { - final StringBuilder content = new StringBuilder(); - final List lines; - try (BufferedReader reader = new BufferedReader(new FileReader(file))) { - lines = reader.lines().collect(Collectors.toList()); - } - for (int i = Math.max(0, lines.size() - 1000); i < lines.size(); i++) { - content.append(lines.get(i)).append("\n"); - } - return content.toString(); - } - -}