Package net.dv8tion.jda.api.utils
Class FileProxy
java.lang.Object
net.dv8tion.jda.api.utils.FileProxy
- Direct Known Subclasses:
AttachmentProxy
,ImageProxy
A utility class to download files.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiondownload()
Retrieves theInputStream
of this filedownloadToFile
(File file) Downloads the data of this file into the specified file.Downloads the data of this file, 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) Downloads the data of this file into the specified file.getUrl()
Returns the URL that has been passed to this proxy.static void
setDefaultHttpClient
(okhttp3.OkHttpClient httpClient) Sets the default OkHttpClient used byFileProxy
andImageProxy
.withClient
(okhttp3.OkHttpClient customHttpClient) Sets the custom OkHttpClient used by this instance, regardless of ifsetDefaultHttpClient(OkHttpClient)
has been used or not.
-
Constructor Details
-
FileProxy
Constructs a newFileProxy
for the provided URL.- Parameters:
url
- The URL to download from- Throws:
IllegalArgumentException
- If the provided URL is null
-
-
Method Details
-
setDefaultHttpClient
Sets the default OkHttpClient used byFileProxy
andImageProxy
.
This can still be overridden on a per-instance basis withwithClient(OkHttpClient)
.- Parameters:
httpClient
- The defaultOkHttpClient
to use while making HTTP requests- Throws:
IllegalArgumentException
- If the providedOkHttpClient
is null
-
getUrl
Returns the URL that has been passed to this proxy.
This URL is always from Discord.- Returns:
- The URL of the file.
-
withClient
Sets the custom OkHttpClient used by this instance, regardless of ifsetDefaultHttpClient(OkHttpClient)
has been used or not.- Parameters:
customHttpClient
- The customOkHttpClient
to use while making HTTP requests- Returns:
- This proxy for chaining convenience.
- Throws:
IllegalArgumentException
- If the providedOkHttpClient
is null
-
download
Retrieves theInputStream
of this file- Returns:
CompletableFuture
which holds anInputStream
, theInputStream
must be closed manually
-
downloadToPath
Downloads the data of this file, and stores it in a file with the same name as the queried file name (this would be the last segment of the URL).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.
- Returns:
CompletableFuture
which holds aPath
which corresponds to the location the file has been downloaded.
-
downloadToFile
Downloads the data of this file into the specified file.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 data- 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
-
downloadToPath
Downloads the data of this file into the specified file.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 image- 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
-