Fix file paste utility

This commit is contained in:
Jaime Martínez Rincón 2020-09-08 22:27:52 +02:00
parent 9525f48cc1
commit 3331ea4851
No known key found for this signature in database
GPG Key ID: D7F007B5BBA8E142
2 changed files with 20 additions and 18 deletions

View File

@ -37,7 +37,7 @@ public enum PasteHelper {
try (FileInputStream stream = new FileInputStream(file)) {
try (InputStreamReader reader = new InputStreamReader(stream, "UTF-8")) {
String content = CharStreams.toString(reader);
HastebinPaste paste = new HastebinPaste("https://file.properties/paste/", content);
HastebinPaste paste = new HastebinPaste(HASTEBIN_HOST, content);
return paste.paste();
}
}
@ -62,7 +62,7 @@ public enum PasteHelper {
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
String content = CharStreams.toString(reader);
content = content.replaceAll("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}", "?.?.?.?");
HastebinPaste paste = new HastebinPaste("https://file.properties/paste/", content);
HastebinPaste paste = new HastebinPaste(HASTEBIN_HOST, content);
return paste.paste();
}
}
@ -86,14 +86,16 @@ public enum PasteHelper {
try (FileInputStream stream = new FileInputStream(file)) {
try (InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
String content = CharStreams.toString(reader);
HastebinPaste paste = new HastebinPaste("https://file.properties/paste/", content);
HastebinPaste paste = new HastebinPaste(HASTEBIN_HOST, content);
return paste.paste();
}
}
}
};
private URL url;
private static final String HASTEBIN_HOST = "https://hastebin.com/";
private URL lastPasteUrl;
private final BiConsumer<CommandSender, URL> consumer;
private final boolean cache;
@ -104,9 +106,9 @@ public enum PasteHelper {
}
public void send(PlayerBalancer plugin, CommandSender sender) {
if (url == null || !cache) {
if (lastPasteUrl == null || !cache) {
try {
url = paste(plugin);
lastPasteUrl = paste(plugin);
} catch (PasteException e) {
sender.sendMessage(new ComponentBuilder("An exception occurred while trying to send the paste: " + e.getMessage())
.color(ChatColor.RED)
@ -127,8 +129,8 @@ public enum PasteHelper {
);
}
if (url != null) {
consumer.accept(sender, url);
if (lastPasteUrl != null) {
consumer.accept(sender, lastPasteUrl);
} else {
sender.sendMessage(new ComponentBuilder("Could not create the paste, try again...")
.color(ChatColor.RED)
@ -137,15 +139,15 @@ public enum PasteHelper {
}
}
public URL getURL() {
return url;
public URL getLastPasteURL() {
return lastPasteUrl;
}
public abstract URL paste(PlayerBalancer plugin) throws Exception;
public static void reset() {
for (PasteHelper helper : values()) {
helper.url = null;
helper.lastPasteUrl = null;
}
}
}

View File

@ -1,10 +1,13 @@
import com.jaimemartz.playerbalancer.utils.HastebinPaste;
import org.junit.Test;
import java.net.URL;
import static org.junit.Assert.*;
public class HastebinPasteTest {
@Test
public void test() {
HastebinPaste paste = new HastebinPaste("https://file.properties/paste/",
public void test() throws Exception {
HastebinPaste paste = new HastebinPaste("https://hastebin.com/",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed " +
"iaculis, sapien et vehicula tristique, diam libero bibendum " +
"nunc, et rutrum nisl nulla quis diam. Cras ipsum enim, molestie" +
@ -16,10 +19,7 @@ public class HastebinPasteTest {
" orci, posuere malesuada ante non, elementum vehicula libero."
);
try {
System.out.println(paste.paste());
} catch (Exception e) {
e.printStackTrace();
}
URL pasteUrl = paste.paste();
assertNotNull(pasteUrl);
}
}