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);
}
}
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);
}
}
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));
}
}
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()));
}
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()));
}
}
Aggregations