Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Response> {
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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
4 changes: 3 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ version: $version
main: io.github.kiber2009.plugin.contentapi.ContentApiPlugin
api-version: $mcVersion
author: $author
description: $description
description: $description
libraries:
- org.apache.httpcomponents:httpmime:4.5.14
Loading