Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/feature/expiration'
Browse files Browse the repository at this point in the history
# Conflicts:
#	gradle.properties
  • Loading branch information
noeppi-noeppi committed Jun 29, 2024
2 parents 081d2d6 + fa430e4 commit e0a8032
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
23 changes: 15 additions & 8 deletions src/main/java/org/moddingx/pastewrapper/PasteApi.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.moddingx.pastewrapper;

import com.google.gson.*;
import org.moddingx.pastewrapper.route.CreateRoute;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -14,17 +15,17 @@
import java.nio.charset.StandardCharsets;

public class PasteApi {

private static final Logger logger = LoggerFactory.getLogger(PasteApi.class);

private static final Gson GSON;

static {
GsonBuilder builder = new GsonBuilder();
builder.disableHtmlEscaping();
GSON = builder.create();
}

private final String token;
private final HttpClient client;

Expand All @@ -34,7 +35,12 @@ public PasteApi(String token) {
}

public Paste createPaste(@Nullable String title, String content) throws IOException {
return this.createPaste(title, content, CreateRoute.EXPIRATION_ONE_YEAR);
}

public Paste createPaste(@Nullable String title, String content, int expirationSeconds) throws IOException {
try {
expirationSeconds = Math.min(expirationSeconds, CreateRoute.EXPIRATION_ONE_YEAR);
JsonObject json = new JsonObject();
if (title != null) json.addProperty("description", title);
JsonArray sections = new JsonArray();
Expand All @@ -43,6 +49,7 @@ public Paste createPaste(@Nullable String title, String content) throws IOExcept
section.addProperty("contents", content);
sections.add(section);
json.add("sections", sections);
json.addProperty("expiration", expirationSeconds);
String jsonStr = GSON.toJson(json) + "\n";

HttpRequest request = HttpRequest.newBuilder()
Expand All @@ -67,7 +74,7 @@ record Result(int code, @Nullable String data) {}
JsonObject response = GSON.fromJson(result.data(), JsonObject.class);
String id = response.get("id").getAsString();
URI uri = URI.create(response.get("link").getAsString());
return new Paste(id, uri);
return new Paste(id, uri, expirationSeconds);
} catch (JsonSyntaxException | IllegalArgumentException e) {
throw new IOException("Invalid response", e);
}
Expand All @@ -76,7 +83,7 @@ record Result(int code, @Nullable String data) {}
throw new IOException("Interrupted", e);
}
}

public void delete(String pasteId) throws IOException {
try {
HttpRequest request = HttpRequest.newBuilder()
Expand All @@ -94,7 +101,7 @@ record Result(int code, @Nullable String data) {}
return HttpResponse.BodySubscribers.replacing(new Result(info.statusCode(), null));
}
}).body();

if (result.data() == null) throw new IOException("HTTP status code " + result.code());
try {
JsonObject response = GSON.fromJson(result.data(), JsonObject.class);
Expand All @@ -107,6 +114,6 @@ record Result(int code, @Nullable String data) {}
throw new IOException("Interrupted", e);
}
}
public record Paste(String id, URI uri) {}

public record Paste(String id, URI uri, int expirationSeconds) {}
}
13 changes: 11 additions & 2 deletions src/main/java/org/moddingx/pastewrapper/route/CreateRoute.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,29 @@
import java.io.IOException;

public class CreateRoute extends JsonRoute {


public static final int EXPIRATION_ONE_YEAR = 60 * 60 * 24 * 365;

public CreateRoute(Service spark, PasteApi api, EditKeyManager mgr) {
super(spark, api, mgr);
}

@Override
protected JsonElement apply(Request request, Response response) throws IOException {
String title = request.queryParams("title");
int expirationSeconds;
try {
expirationSeconds = request.queryParams("expiration") == null ? EXPIRATION_ONE_YEAR : Integer.parseInt(request.queryParams("expiration"));
} catch (NumberFormatException e) {
throw this.spark.halt(400, "Invalid expiration seconds: " + request.queryParams("expiration"));
}
String content = request.body();
if (content == null || content.isEmpty()) throw this.spark.halt(400, "No Content");
PasteApi.Paste paste = this.api.createPaste(title, content);
PasteApi.Paste paste = this.api.createPaste(title, content, expirationSeconds);
JsonObject json = new JsonObject();
json.addProperty("url", paste.uri().toString());
json.addProperty("edit", this.mgr.getEditToken(paste.id()));
json.addProperty("expiration", paste.expirationSeconds());
return json;
}
}

0 comments on commit e0a8032

Please sign in to comment.