Skip to content

Commit

Permalink
Add downloadAsFileUpload to file proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
freya022 committed Dec 24, 2024
1 parent 5ec2d8e commit 40b8fe4
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/main/java/net/dv8tion/jda/api/utils/AttachmentProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,47 @@ public CompletableFuture<Icon> 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.
* <br>The returned {@link FileUpload} can be reused safely, and does not need to be closed.
*
* <p>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.
* <br>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
* <ul>
* <li>The file name is null or blank</li>
* <li>The requested width is negative or 0</li>
* <li>The requested height is negative or 0</li>
* </ul>
*
* @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);
}
});
}
}
29 changes: 29 additions & 0 deletions src/main/java/net/dv8tion/jda/api/utils/FileProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,35 @@ public CompletableFuture<Path> 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.
* <br>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;
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/net/dv8tion/jda/api/utils/ImageProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,40 @@ public CompletableFuture<Path> 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.
* <br>The returned {@link FileUpload} can be reused safely, and does not need to be closed.
*
* <p><b>The image may not be resized at any size, usually Discord only allows for a few powers of 2</b>, so numbers like 128, 256, 512..., 100 might also be a valid size.
*
* <p>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);
}
});
}
}

0 comments on commit 40b8fe4

Please sign in to comment.