diff --git a/build.gradle.kts b/build.gradle.kts index e34d0a2..8b469f3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -35,7 +35,8 @@ tasks { description = "" dependsOn("publishAllPublicationsToMavenCentralRepository") doLast { - val path = "https://ossrh-staging-api.central.sonatype.com/manual/upload/defaultRepository/$namespace?publishing_type=automatic" + val path = + "https://ossrh-staging-api.central.sonatype.com/manual/upload/defaultRepository/$namespace?publishing_type=automatic" val auth = Base64.getEncoder().encodeToString( "${System.getenv("MAVEN_CENTRAL_USERNAME")}:${System.getenv("MAVEN_CENTRAL_PASSWORD")}" .toByteArray() @@ -94,6 +95,9 @@ repositories { dependencies { @Suppress("VulnerableLibrariesLocal", "RedundantSuppression") compileOnly("io.papermc.paper:paper-api:${mcVersion}.build.+") + + compileOnly("org.apache.httpcomponents:httpclient:4.5.14") + compileOnly("org.apache.httpcomponents:httpmime:4.5.14") } java { diff --git a/src/main/java/io/github/kiber2009/plugin/contentapi/ContentApiPlugin.java b/src/main/java/io/github/kiber2009/plugin/contentapi/ContentApiPlugin.java index 9625932..39e65ed 100644 --- a/src/main/java/io/github/kiber2009/plugin/contentapi/ContentApiPlugin.java +++ b/src/main/java/io/github/kiber2009/plugin/contentapi/ContentApiPlugin.java @@ -1,12 +1,18 @@ package io.github.kiber2009.plugin.contentapi; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import io.github.kiber2009.plugin.contentapi.commands.GiveCommand; +import io.github.kiber2009.plugin.contentapi.uploading.LobfileUploadProvider; import io.papermc.paper.command.brigadier.Commands; import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; import org.bukkit.plugin.java.JavaPlugin; import org.jspecify.annotations.NonNull; public final class ContentApiPlugin extends JavaPlugin { + public static final Gson GSON = new GsonBuilder() + .registerTypeAdapter(LobfileUploadProvider.Response.class, LobfileUploadProvider.Response.Deserializer.INSTANCE) + .create(); private static ContentApiPlugin instance; diff --git a/src/main/java/io/github/kiber2009/plugin/contentapi/uploading/LobfileUploadProvider.java b/src/main/java/io/github/kiber2009/plugin/contentapi/uploading/LobfileUploadProvider.java new file mode 100644 index 0000000..2e2d33c --- /dev/null +++ b/src/main/java/io/github/kiber2009/plugin/contentapi/uploading/LobfileUploadProvider.java @@ -0,0 +1,70 @@ +package io.github.kiber2009.plugin.contentapi.uploading; + +import com.google.gson.*; +import io.github.kiber2009.plugin.contentapi.ContentApiPlugin; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.jspecify.annotations.NonNull; + +import java.io.IOException; +import java.lang.reflect.Type; +import java.net.URI; + +public class LobfileUploadProvider implements UploadProvider { + private static final URI UPLOAD_URI = URI.create("https://lobfile.com/api/v3/upload"); + + private final String apiKey; + + public LobfileUploadProvider(final String apiKey) { + this.apiKey = apiKey; + } + + @Override + public @NonNull String uploadBytes(final @NonNull String fileName, final byte @NonNull [] bytes) { + try (final CloseableHttpClient client = HttpClients.createDefault()) { + final HttpPost request = new HttpPost(UPLOAD_URI); + request.setHeader("X-API-Key", apiKey); + request.setEntity(MultipartEntityBuilder.create() + .addBinaryBody("file", bytes, ContentType.APPLICATION_OCTET_STREAM, fileName) + .addTextBody("sha256", "john_doe") + .build()); + try (final CloseableHttpResponse httpResponse = client.execute(request)) { + final String responseString = EntityUtils.toString(httpResponse.getEntity()); + final Response response = ContentApiPlugin.GSON.fromJson(responseString, Response.class); + if (response.success) + return response.url; + else + throw new RuntimeException("Error: " + response.error); + } + } catch (final IOException e) { + throw new RuntimeException(e); + } + } + + public record Response(boolean success, String url, String error) { + public static final class Deserializer implements JsonDeserializer { + public static final Deserializer INSTANCE = new Deserializer(); + + private Deserializer() { + } + + @Override + public Response deserialize(final JsonElement json, final Type typeOfT, + final JsonDeserializationContext context) throws JsonParseException { + final JsonObject object = json.getAsJsonObject(); + + final JsonElement url = object.get("url"); + final JsonElement error = object.get("error"); + + return new Response(object.get("success").getAsBoolean(), + url != null ? url.getAsString() : null, + error != null ? error.getAsString() : null); + } + } + } +} diff --git a/src/main/java/io/github/kiber2009/plugin/contentapi/uploading/UploadProvider.java b/src/main/java/io/github/kiber2009/plugin/contentapi/uploading/UploadProvider.java new file mode 100644 index 0000000..7d009a0 --- /dev/null +++ b/src/main/java/io/github/kiber2009/plugin/contentapi/uploading/UploadProvider.java @@ -0,0 +1,7 @@ +package io.github.kiber2009.plugin.contentapi.uploading; + +import org.jspecify.annotations.NonNull; + +public interface UploadProvider { + @NonNull String uploadBytes(final @NonNull String fileName, final byte @NonNull [] bytes); +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 6e53b32..19ea0ab 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,4 +3,6 @@ version: $version main: io.github.kiber2009.plugin.contentapi.ContentApiPlugin api-version: $mcVersion author: $author -description: $description \ No newline at end of file +description: $description +libraries: + - org.apache.httpcomponents:httpmime:4.5.14 \ No newline at end of file