Class ImageProxy
This supports downloading the images from the normal URL, as well as downloading the image with a specific size.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondownload
(int size) Retrieves theInputStream
of this image at the specified size.downloadAsFileUpload
(String name, int size) Returns aFileUpload
which supplies a data stream of this attachment, with the given file name and at the specified size.Downloads the data of this attachment, and constructs anIcon
from the data.downloadAsIcon
(int size) Downloads the data of this image, at the specified size, and constructs anIcon
from the data.downloadToFile
(File file, int size) Downloads the data of this image, at the specified size, and stores it in the specified file.downloadToPath
(int size) Downloads the data of this image, at the specified size, 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 size) Downloads the data of this image, at the specified size, and stores it in the specified file.getUrl
(int size) Returns the image URL for the specified size.withClient
(okhttp3.OkHttpClient customHttpClient) Sets the custom OkHttpClient used by this instance, regardless of ifFileProxy.setDefaultHttpClient(OkHttpClient)
has been used or not.Methods inherited from class net.dv8tion.jda.api.utils.FileProxy
download, downloadAsFileUpload, downloadToFile, downloadToPath, downloadToPath, getUrl, setDefaultHttpClient
-
Constructor Details
-
ImageProxy
Constructs a newImageProxy
for the provided URL.- Parameters:
url
- The URL to download the image from- Throws:
IllegalArgumentException
- If the provided URL is null
-
-
Method Details
-
withClient
Description copied from class:FileProxy
Sets the custom OkHttpClient used by this instance, regardless of ifFileProxy.setDefaultHttpClient(OkHttpClient)
has been used or not.- Overrides:
withClient
in classFileProxy
- Parameters:
customHttpClient
- The customOkHttpClient
to use while making HTTP requests- Returns:
- This proxy for chaining convenience.
-
getUrl
Returns the image URL for the specified size.
The size is a best-effort resize from Discord, with recommended size values as powers of 2 such as 1024 or 512.- Parameters:
size
- The size of the image- Returns:
- URL of the image with the specified size
- Throws:
IllegalArgumentException
- If the requested size is negative or 0
-
download
Retrieves theInputStream
of this image at the specified size.
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 and 600 might also be valid sizes.If the image is not of a valid size, the CompletableFuture will hold an exception since the HTTP request would have returned a 404.
- Parameters:
size
- The size of this image- Returns:
CompletableFuture
which holds anInputStream
, theInputStream
must be closed manually.
-
downloadToPath
Downloads the data of this image, at the specified size, 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 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 and 600 might also be valid sizes.If the image is not of a valid size, the CompletableFuture will hold an exception since the HTTP request would have returned a 404.
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:
size
- The size 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 size is negative or 0
- The URL's scheme is neither http or https
-
downloadToFile
@Nonnull @CheckReturnValue public CompletableFuture<File> downloadToFile(@Nonnull File file, int size) Downloads the data of this image, at the specified size, and stores it in the specified file.
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 and 600 might also be valid sizes.If the image is not of a valid size, the CompletableFuture will hold an exception since the HTTP request would have returned a 404.
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 imagesize
- The size 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 size is negative or 0
-
downloadToPath
@Nonnull @CheckReturnValue public CompletableFuture<Path> downloadToPath(@Nonnull Path path, int size) Downloads the data of this image, at the specified size, and stores it in the specified file.
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 and 600 might also be valid sizes.If the image is not of a valid size, the CompletableFuture will hold an exception since the HTTP request would have returned a 404.
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 imagesize
- The size 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 size 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 image, at the specified size, and constructs anIcon
from the data.
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 and 600 might also be valid sizes.If the image is not of a valid size, the CompletableFuture will hold an exception since the HTTP request would have returned a 404.
- Parameters:
size
- The size of this image, must be positive- Returns:
CompletableFuture
which holds anIcon
.- Throws:
IllegalArgumentException
- If the requested size is negative or 0
-
downloadAsFileUpload
Returns aFileUpload
which supplies a data stream of this attachment, with the given file name and at the specified size.
The returnedFileUpload
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.
- Parameters:
name
- The name of the to-be-uploaded filesize
- The size of this image- Returns:
FileUpload
from this attachment.- Throws:
IllegalArgumentException
- If the file name is null or blank
-