Package net.dv8tion.jda.api.utils
Class AttachmentProxy
java.lang.Object
net.dv8tion.jda.api.utils.FileProxy
net.dv8tion.jda.api.utils.AttachmentProxy
A utility class to retrieve attachments.
This supports downloading the images from the normal URL, as well as downloading the image with a specific width and height.
This supports downloading the images from the normal URL, as well as downloading the image with a specific width and height.
-
Constructor Summary
ConstructorDescriptionAttachmentProxy
(String url) Constructs a newAttachmentProxy
for the provided URL. -
Method Summary
Modifier and TypeMethodDescriptiondownload
(int width, int height) Retrieves theInputStream
of this attachment at the specified width and height.Downloads the data of this attachment, and constructs anIcon
from the data.downloadAsIcon
(int width, int height) Downloads the data of this attachment, at the specified size, and constructs anIcon
from the data.downloadToFile
(File file, int width, int height) Downloads the data of this attachment, at the specified width and height, and stores it in the specified file.downloadToPath
(int width, int height) Downloads the data of this attachment, at the specified width and height, and stores it in a file with the same name as the queried file name (this would be the last segment of the URL).downloadToPath
(Path path, int width, int height) Downloads the data of this attachment, at the specified size, and stores it in the specified file.getUrl
(int width, int height) Returns the attachment URL for the specified width and height.Methods inherited from class net.dv8tion.jda.api.utils.FileProxy
download, downloadToFile, downloadToPath, downloadToPath, getUrl, setDefaultHttpClient, withClient
-
Constructor Details
-
AttachmentProxy
Constructs a newAttachmentProxy
for the provided URL.- Parameters:
url
- The URL to download the attachment from- Throws:
IllegalArgumentException
- If the provided URL is null
-
-
Method Details
-
getUrl
Returns the attachment URL for the specified width and height.
The width and height is a best-effort resize from Discord.- Parameters:
width
- The width of the imageheight
- The height of the image- Returns:
- URL of the attachment with the specified width and height
-
download
Retrieves theInputStream
of this attachment at the specified width and height.
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.- Parameters:
width
- The width of this image, must be positiveheight
- The height of this image, must be positive- Returns:
CompletableFuture
which holds anInputStream
, theInputStream
must be closed manually.- Throws:
IllegalArgumentException
- If any of the follow checks are true- The requested width is negative or 0
- The requested height is negative or 0
-
downloadToPath
Downloads the data of this attachment, at the specified width and height, and stores it in a file with the same name as the queried file name (this would be the last segment of the URL).
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.Implementation note: The file is first downloaded into a temporary file, the file is then moved to its real destination when the download is complete.
- Parameters:
width
- The width of this image, must be positiveheight
- The height of this image, must be positive- Returns:
CompletableFuture
which holds aPath
which corresponds to the location the file has been downloaded.- Throws:
IllegalArgumentException
- If any of the follow checks are true- The requested width is negative or 0
- The requested height is negative or 0
- The URL's scheme is neither http or https
-
downloadToFile
@Nonnull @CheckReturnValue public CompletableFuture<File> downloadToFile(@Nonnull File file, int width, int height) Downloads the data of this attachment, at the specified width and height, and stores it in the specified file.
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.Implementation note: The file is first downloaded into a temporary file, the file is then moved to its real destination when the download is complete.
- Parameters:
file
- The file in which to download the imagewidth
- The width of this image, must be positiveheight
- The height of this image, must be positive- Returns:
CompletableFuture
which holds aFile
, it is the same as the file passed in the parameters.- Throws:
IllegalArgumentException
- If any of the follow checks are true- The target file is null
- The parent folder of the target file does not exist
- The target file exists and is not a
regular file
- The target file exists and is not
writable
- The requested width is negative or 0
- The requested height is negative or 0
-
downloadToPath
@Nonnull @CheckReturnValue public CompletableFuture<Path> downloadToPath(@Nonnull Path path, int width, int height) Downloads the data of this attachment, at the specified size, and stores it in the specified file.
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.Implementation note: The file is first downloaded into a temporary file, the file is then moved to its real destination when the download is complete.
The given path can also target filesystems such as a ZIP filesystem.- Parameters:
path
- The file in which to download the imagewidth
- The width of this image, must be positiveheight
- The height of this image, must be positive- Returns:
CompletableFuture
which holds aPath
, it is the same as the path passed in the parameters.- Throws:
IllegalArgumentException
- If any of the follow checks are true- The target path is null
- The parent folder of the target path does not exist
- The target path exists and is not a
regular file
- The target path exists and is not
writable
- The requested width is negative or 0
- The requested height is negative or 0
-
downloadAsIcon
Downloads the data of this attachment, and constructs anIcon
from the data.- Returns:
CompletableFuture
which holds anIcon
.
-
downloadAsIcon
Downloads the data of this attachment, at the specified size, and constructs anIcon
from the data.
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.- Parameters:
width
- The width of this image, must be positiveheight
- The height of this image, must be positive- Returns:
CompletableFuture
which holds anIcon
.- Throws:
IllegalArgumentException
- If any of the follow checks are true- The requested width is negative or 0
- The requested height is negative or 0
-