|
| 1 | +package io.github.kiber2009.plugin.contentapi.uploading; |
| 2 | + |
| 3 | +import com.google.gson.*; |
| 4 | +import io.github.kiber2009.plugin.contentapi.ContentApiPlugin; |
| 5 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 6 | +import org.apache.http.client.methods.HttpPost; |
| 7 | +import org.apache.http.entity.ContentType; |
| 8 | +import org.apache.http.entity.mime.MultipartEntityBuilder; |
| 9 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 10 | +import org.apache.http.impl.client.HttpClients; |
| 11 | +import org.apache.http.util.EntityUtils; |
| 12 | +import org.jspecify.annotations.NonNull; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.lang.reflect.Type; |
| 16 | +import java.net.URI; |
| 17 | + |
| 18 | +public class LobfileUploadProvider implements UploadProvider { |
| 19 | + private static final URI UPLOAD_URI = URI.create("https://lobfile.com/api/v3/upload"); |
| 20 | + |
| 21 | + private final String apiKey; |
| 22 | + |
| 23 | + public LobfileUploadProvider(final String apiKey) { |
| 24 | + this.apiKey = apiKey; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public @NonNull String uploadBytes(final @NonNull String fileName, final byte @NonNull [] bytes) { |
| 29 | + try (final CloseableHttpClient client = HttpClients.createDefault()) { |
| 30 | + final HttpPost request = new HttpPost(UPLOAD_URI); |
| 31 | + request.setHeader("X-API-Key", apiKey); |
| 32 | + request.setEntity(MultipartEntityBuilder.create() |
| 33 | + .addBinaryBody("file", bytes, ContentType.APPLICATION_OCTET_STREAM, fileName) |
| 34 | + .addTextBody("sha256", "john_doe") |
| 35 | + .build()); |
| 36 | + try (final CloseableHttpResponse httpResponse = client.execute(request)) { |
| 37 | + final String responseString = EntityUtils.toString(httpResponse.getEntity()); |
| 38 | + final Response response = ContentApiPlugin.GSON.fromJson(responseString, Response.class); |
| 39 | + if (response.success) |
| 40 | + return response.url; |
| 41 | + else |
| 42 | + throw new RuntimeException("Error: " + response.error); |
| 43 | + } |
| 44 | + } catch (final IOException e) { |
| 45 | + throw new RuntimeException(e); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public record Response(boolean success, String url, String error) { |
| 50 | + public static final class Deserializer implements JsonDeserializer<Response> { |
| 51 | + public static final Deserializer INSTANCE = new Deserializer(); |
| 52 | + |
| 53 | + private Deserializer() { |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Response deserialize(final JsonElement json, final Type typeOfT, |
| 58 | + final JsonDeserializationContext context) throws JsonParseException { |
| 59 | + final JsonObject object = json.getAsJsonObject(); |
| 60 | + |
| 61 | + final JsonElement url = object.get("url"); |
| 62 | + final JsonElement error = object.get("error"); |
| 63 | + |
| 64 | + return new Response(object.get("success").getAsBoolean(), |
| 65 | + url != null ? url.getAsString() : null, |
| 66 | + error != null ? error.getAsString() : null); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments