Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add downloadAsFileUpload to file proxies #2782

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add an overload that just uses the known attachment filename

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have the file name as AttachmentProxy is used by MessageEmbed.Thumbnail and MessageEmbed.ImageInfo and they don't have that info

{
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();
freya022 marked this conversation as resolved.
Show resolved Hide resolved
}
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);
}
});
}
}
Loading