From 40b8fe4a5046d0d833e3b1be44604d73e8cd0fb9 Mon Sep 17 00:00:00 2001 From: freya02 <41875020+freya022@users.noreply.github.com> Date: Tue, 24 Dec 2024 15:30:10 +0100 Subject: [PATCH] Add `downloadAsFileUpload` to file proxies --- .../jda/api/utils/AttachmentProxy.java | 43 +++++++++++++++++++ .../net/dv8tion/jda/api/utils/FileProxy.java | 29 +++++++++++++ .../net/dv8tion/jda/api/utils/ImageProxy.java | 36 ++++++++++++++++ 3 files changed, 108 insertions(+) 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..a73149e640 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,47 @@ 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, () -> + { + try + { + // Blocking is fine on the elastic rate limit thread pool [[JDABuilder#setRateLimitElastic]] + return download(url).get(); + } + catch (Throwable e) + { + throw new RuntimeException("Unable to download " + url, e); + } + }); + } } 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..4b54806c08 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,35 @@ 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, () -> + { + try + { + // Blocking is fine on the elastic rate limit thread pool [[JDABuilder#setRateLimitElastic]] + return download().get(); + } + catch (Throwable e) + { + throw new RuntimeException("Unable to download " + getUrl(), e); + } + }); + } + 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..f27febd3fd 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,40 @@ 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, () -> + { + try + { + // Blocking is fine on the elastic rate limit thread pool [[JDABuilder#setRateLimitElastic]] + return download(url).get(); + } + catch (Throwable e) + { + throw new RuntimeException("Unable to download " + url, e); + } + }); + } }