aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/util/HastebinUploader.java
diff options
context:
space:
mode:
authorMoulberry <jjenour@student.unimelb.edu.au>2021-10-13 15:46:13 +0800
committerGitHub <noreply@github.com>2021-10-13 15:46:13 +0800
commitb11742988dec635b5c5da7c2363803cbfafb37b1 (patch)
treed161c7ab2f675dd1f82d2fe8194d7e62bbdaac0a /src/main/java/io/github/moulberry/notenoughupdates/util/HastebinUploader.java
parent8eee6262a6e2c372240331f3c43c2279bd18539e (diff)
parent030f72bb49ab4bca0c35899090550432c5aa0bea (diff)
downloadnotenoughupdates-b11742988dec635b5c5da7c2363803cbfafb37b1.tar.gz
notenoughupdates-b11742988dec635b5c5da7c2363803cbfafb37b1.tar.bz2
notenoughupdates-b11742988dec635b5c5da7c2363803cbfafb37b1.zip
Merge pull request #231 from DoKM/master
DoKM Fork Merge
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/util/HastebinUploader.java')
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/util/HastebinUploader.java59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/HastebinUploader.java b/src/main/java/io/github/moulberry/notenoughupdates/util/HastebinUploader.java
new file mode 100644
index 00000000..70753e10
--- /dev/null
+++ b/src/main/java/io/github/moulberry/notenoughupdates/util/HastebinUploader.java
@@ -0,0 +1,59 @@
+package io.github.moulberry.notenoughupdates.util;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
+import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
+import org.apache.commons.io.Charsets;
+import org.apache.commons.io.IOUtils;
+
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * @author ThatGravyBoat
+ */
+public class HastebinUploader {
+
+ private static final String UPLOAD_URL = "https://hst.sh/documents";
+ private static final String RETURN_URL = "https://hst.sh/";
+ private static final String RAW_RETURN_URL = "https://hst.sh/raw/";
+
+ private static final Gson GSON = new Gson();
+
+ /**
+ * @param data the data you want to upload
+ * @param mode the mode in which the thing should return NORMAL = returns the url, RAW = returns the raw url, NO_URL = returns the slug.
+ * @return The url if mode is NORMAL OR RAW, the slug if mode is NO_URL, and null if an error occurred.
+ */
+ public static String upload(String data, Mode mode){
+ try {
+ byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
+ HttpURLConnection connection = (HttpURLConnection)new URL(UPLOAD_URL).openConnection();
+ connection.setRequestMethod("POST");
+ connection.addRequestProperty("User-Agent", "Minecraft Mod ("+ NotEnoughUpdates.MODID +"/"+NotEnoughUpdates.VERSION+")");
+ connection.addRequestProperty("Content-Length", String.valueOf(bytes.length));
+ connection.setReadTimeout(15000);
+ connection.setConnectTimeout(15000);
+ connection.setDoOutput(true);
+ connection.getOutputStream().write(bytes);
+ final JsonObject json = GSON.fromJson(IOUtils.toString(connection.getInputStream(), Charsets.UTF_8), JsonObject.class);
+ if (!json.has("key")) return null;
+ final String key = json.get("key").getAsString();
+ switch (mode) {
+ case RAW: return RAW_RETURN_URL + key;
+ case NORMAL: return RETURN_URL + key;
+ case NO_URL: return key;
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public enum Mode {
+ NORMAL,
+ RAW,
+ NO_URL
+ }
+}