Fixed JSON as plaintext

This commit is contained in:
2023-12-20 09:56:47 -06:00
parent a604b1e3ec
commit decc0598b1
3 changed files with 26 additions and 16 deletions

View File

@@ -4,6 +4,8 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Timer;
import java.util.TimerTask;
@@ -21,6 +23,21 @@ public class Server {
get("/ngx/raw/*", (req, res) -> {
String id = req.splat()[0];
TextStorage ts = TextStorage.getPaste(id);
res.type("text/plain");
if (ts == null) {
res.status(404);
return "Content Not Found!";
}
return URLDecoder.decode(ts.getText(), StandardCharsets.UTF_8);
});
get("/ngx/json/*", (req, res) -> {
String id = req.splat()[0];
TextStorage ts = TextStorage.getPaste(id);
res.type("application/json");
if (ts == null) {
res.status(404);
return "{\"error\":\"content not found\"}";
}
return GSON.toJson(ts);
});

View File

@@ -23,6 +23,10 @@ public class TextStorage {
return this.id;
}
public String getText() {
return this.text;
}
public static TextStorage getPaste(String id) {
for (TextStorage ts : DB) {
if (ts.id.equals(id)) {
@@ -33,7 +37,7 @@ public class TextStorage {
}
public static TextStorage createPaste(String text) {
TextStorage ts = new TextStorage(generateId(), text, System.currentTimeMillis() + 3600000);
TextStorage ts = new TextStorage(generateId(), text, System.currentTimeMillis() + 1800000);
DB.add(ts);
return ts;
}