Search in sources :

Example 16 with FileContainer

use of org.javacord.core.util.FileContainer in project Javacord by BtoBastian.

the class EmbedBuilderDelegateImpl method setThumbnail.

@Override
public void setThumbnail(byte[] thumbnail, String fileType) {
    thumbnailUrl = null;
    if (thumbnail == null) {
        thumbnailContainer = null;
    } else {
        thumbnailContainer = new FileContainer(thumbnail, fileType);
        thumbnailContainer.setFileTypeOrName(UUID.randomUUID().toString() + "." + fileType);
    }
}
Also used : FileContainer(org.javacord.core.util.FileContainer)

Example 17 with FileContainer

use of org.javacord.core.util.FileContainer in project Javacord by BtoBastian.

the class EmbedBuilderDelegateImpl method setImage.

@Override
public void setImage(byte[] image, String fileType) {
    imageUrl = null;
    if (image == null) {
        imageContainer = null;
    } else {
        imageContainer = new FileContainer(image, fileType);
        imageContainer.setFileTypeOrName(UUID.randomUUID().toString() + "." + fileType);
    }
}
Also used : FileContainer(org.javacord.core.util.FileContainer)

Example 18 with FileContainer

use of org.javacord.core.util.FileContainer in project Javacord by BtoBastian.

the class EmbedBuilderDelegateImpl method setImage.

@Override
public void setImage(File image) {
    imageUrl = null;
    if (image == null) {
        imageContainer = null;
    } else {
        imageContainer = new FileContainer(image);
        imageContainer.setFileTypeOrName(UUID.randomUUID().toString() + "." + FileUtils.getExtension(image));
    }
}
Also used : FileContainer(org.javacord.core.util.FileContainer)

Example 19 with FileContainer

use of org.javacord.core.util.FileContainer in project Javacord by BtoBastian.

the class StickerBuilderDelegateImpl method create.

@Override
public CompletableFuture<Sticker> create(String reason) {
    if (name == null) {
        throw new IllegalStateException("The name is no optional parameter.");
    }
    if (tags == null) {
        throw new IllegalStateException("The tags are no optional parameter.");
    }
    if (file == null) {
        throw new IllegalStateException("The file content is no optional parameter.");
    }
    if (file.length() > 512000) {
        throw new IllegalStateException("The file is too large (must be smaller than 500 KB).");
    }
    FileContainer container = new FileContainer(file);
    if (!container.getFileTypeOrName().endsWith("png") && !container.getFileTypeOrName().endsWith("apng") && !container.getFileTypeOrName().endsWith("json")) {
        throw new IllegalStateException("The file must be an image.");
    }
    String mediaType = URLConnection.guessContentTypeFromName(container.getFileTypeOrName());
    if (mediaType == null) {
        mediaType = "application/octet-stream";
    }
    MultipartBody multipartBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("name", name).addFormDataPart("description", description).addFormDataPart("tags", tags).addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse(mediaType), container.asByteArray(api).join())).build();
    return new RestRequest<Sticker>(api, RestMethod.POST, RestEndpoint.SERVER_STICKER).setUrlParameters(server.getIdAsString()).setMultipartBody(multipartBody).setAuditLogReason(reason).execute(result -> new StickerImpl(api, result.getJsonBody()));
}
Also used : RestRequest(org.javacord.core.util.rest.RestRequest) MultipartBody(okhttp3.MultipartBody) FileContainer(org.javacord.core.util.FileContainer)

Example 20 with FileContainer

use of org.javacord.core.util.FileContainer in project Javacord by BtoBastian.

the class InteractionMessageBuilderDelegateImpl method checkForAttachmentsAndExecuteRequest.

private CompletableFuture<Message> checkForAttachmentsAndExecuteRequest(RestRequest<Message> request, ObjectNode body) {
    if (!attachments.isEmpty() || embeds.stream().anyMatch(EmbedBuilder::requiresAttachments)) {
        CompletableFuture<Message> future = new CompletableFuture<>();
        // We access files etc. so this should be async
        request.getApi().getThreadPool().getExecutorService().submit(() -> {
            try {
                List<FileContainer> tempAttachments = new ArrayList<>(attachments);
                // Add the attachments required for the embed
                for (EmbedBuilder embed : embeds) {
                    tempAttachments.addAll(((EmbedBuilderDelegateImpl) embed.getDelegate()).getRequiredAttachments());
                }
                addMultipartBodyToRequest(request, body, tempAttachments, request.getApi());
                request.execute(result -> request.getApi().getOrCreateMessage(request.getApi().getTextChannelById(result.getJsonBody().get("channel_id").asLong()).orElseThrow(() -> new NoSuchElementException("TextChannel is not cached")), result.getJsonBody())).whenComplete((message, throwable) -> {
                    if (throwable != null) {
                        future.completeExceptionally(throwable);
                    } else {
                        future.complete(message);
                    }
                });
            } catch (Throwable t) {
                future.completeExceptionally(t);
            }
        });
        return future;
    } else {
        request.setBody(body);
        return request.execute(result -> request.getApi().getOrCreateMessage(request.getApi().getTextChannelById(result.getJsonBody().get("channel_id").asLong()).orElseThrow(() -> new NoSuchElementException("TextChannel is not cached")), result.getJsonBody()));
    }
}
Also used : InteractionCallbackDataFlag(org.javacord.api.interaction.callback.InteractionCallbackDataFlag) EmbedBuilder(org.javacord.api.entity.message.embed.EmbedBuilder) InteractionBase(org.javacord.api.interaction.InteractionBase) CompletableFuture(java.util.concurrent.CompletableFuture) RestMethod(org.javacord.core.util.rest.RestMethod) Message(org.javacord.api.entity.message.Message) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FileContainer(org.javacord.core.util.FileContainer) ArrayList(java.util.ArrayList) RestEndpoint(org.javacord.core.util.rest.RestEndpoint) List(java.util.List) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) MessageComponentInteraction(org.javacord.api.interaction.MessageComponentInteraction) RestRequest(org.javacord.core.util.rest.RestRequest) EmbedBuilderDelegateImpl(org.javacord.core.entity.message.embed.EmbedBuilderDelegateImpl) InteractionImpl(org.javacord.core.interaction.InteractionImpl) NoSuchElementException(java.util.NoSuchElementException) EnumSet(java.util.EnumSet) InteractionMessageBuilderDelegate(org.javacord.api.entity.message.internal.InteractionMessageBuilderDelegate) CompletableFuture(java.util.concurrent.CompletableFuture) EmbedBuilder(org.javacord.api.entity.message.embed.EmbedBuilder) Message(org.javacord.api.entity.message.Message) ArrayList(java.util.ArrayList) FileContainer(org.javacord.core.util.FileContainer) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

FileContainer (org.javacord.core.util.FileContainer)29 RestRequest (org.javacord.core.util.rest.RestRequest)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 ArrayList (java.util.ArrayList)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Message (org.javacord.api.entity.message.Message)3 EmbedBuilder (org.javacord.api.entity.message.embed.EmbedBuilder)3 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)2 List (java.util.List)2 MultipartBody (okhttp3.MultipartBody)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1