diff --git a/src/main/java/net/dv8tion/jda/api/utils/AttachmentProxy.java b/src/main/java/net/dv8tion/jda/api/utils/AttachmentProxy.java index 04420a2283..aef9e7262f 100644 --- a/src/main/java/net/dv8tion/jda/api/utils/AttachmentProxy.java +++ b/src/main/java/net/dv8tion/jda/api/utils/AttachmentProxy.java @@ -259,4 +259,40 @@ public CompletableFuture downloadAsIcon(int width, int height) { return downloadAsIcon(getUrl(width, height)); } + + /** + * Returns a {@link FileUpload} which supplies a data stream of this attachment, + * with the given file name and at the specified size. + *
The returned {@link FileUpload} can be reused safely, and does not need to be closed. + * + *

The attachment, if an image, may be resized at any size, however if the size does not fit the ratio of the image, then it will be cropped as to fit the target size. + *
If the attachment is not an image then the size parameters are ignored and the file is downloaded. + * + * @param name + * The name of the to-be-uploaded file + * @param width + * The width of this image, must be positive + * @param height + * The height of this image, must be positive + * + * @throws IllegalArgumentException + * If any of the follow checks are true + *

+ * + * @return {@link FileUpload} from this attachment. + */ + @Nonnull + public FileUpload downloadAsFileUpload(@Nonnull String name, int width, int height) + { + final String url = getUrl(width, height); // So the checks are also done outside the FileUpload + return FileUpload.fromStreamSupplier(name, () -> + { + // Blocking is fine on the elastic rate limit thread pool [[JDABuilder#setRateLimitElastic]] + return download(url).join(); + }); + } } diff --git a/src/main/java/net/dv8tion/jda/api/utils/FileProxy.java b/src/main/java/net/dv8tion/jda/api/utils/FileProxy.java index 78180a13b5..3c2a9b1b25 100644 --- a/src/main/java/net/dv8tion/jda/api/utils/FileProxy.java +++ b/src/main/java/net/dv8tion/jda/api/utils/FileProxy.java @@ -330,6 +330,28 @@ public CompletableFuture downloadToPath(@Nonnull Path path) return downloadToPath(url, path); } + /** + * Returns a {@link FileUpload} which supplies a data stream of this attachment, + * with the given file name. + *
The returned {@link FileUpload} can be reused safely, and does not need to be closed. + * + * @param name + * The name of the to-be-uploaded file + * + * @throws IllegalArgumentException If the file name is null or blank + * + * @return {@link FileUpload} from this attachment. + */ + @Nonnull + public FileUpload downloadAsFileUpload(@Nonnull String name) + { + return FileUpload.fromStreamSupplier(name, () -> + { + // Blocking is fine on the elastic rate limit thread pool [[JDABuilder#setRateLimitElastic]] + return download().join(); + }); + } + protected static class DownloadTask { private final Call call; diff --git a/src/main/java/net/dv8tion/jda/api/utils/ImageProxy.java b/src/main/java/net/dv8tion/jda/api/utils/ImageProxy.java index 4261a2e556..4960f0a22e 100644 --- a/src/main/java/net/dv8tion/jda/api/utils/ImageProxy.java +++ b/src/main/java/net/dv8tion/jda/api/utils/ImageProxy.java @@ -178,4 +178,33 @@ public CompletableFuture downloadToPath(@Nonnull Path path, int size) return downloadToPath(getUrl(size), path); } + + /** + * Returns a {@link FileUpload} which supplies a data stream of this attachment, + * with the given file name and at the specified size. + *
The returned {@link FileUpload} can be reused safely, and does not need to be closed. + * + *

The image may not be resized at any size, usually Discord only allows for a few powers of 2, so numbers like 128, 256, 512..., 100 might also be a valid size. + * + *

If the image is not of a valid size, the CompletableFuture will hold an exception since the HTTP request would have returned a 404. + * + * @param name + * The name of the to-be-uploaded file + * @param size + * The size of this image + * + * @throws IllegalArgumentException If the file name is null or blank + * + * @return {@link FileUpload} from this attachment. + */ + @Nonnull + public FileUpload downloadAsFileUpload(@Nonnull String name, int size) + { + final String url = getUrl(size); // So the checks are also done outside the FileUpload + return FileUpload.fromStreamSupplier(name, () -> + { + // Blocking is fine on the elastic rate limit thread pool [[JDABuilder#setRateLimitElastic]] + return download(url).join(); + }); + } }