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
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondownload()Retrieves theInputStreamof this filedownloadAsFileUpload(String name) Returns aFileUploadwhich supplies a data stream of this attachment, with the given file name.downloadToFile(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 voidsetDefaultHttpClient(okhttp3.OkHttpClient httpClient) Sets the default OkHttpClient used byFileProxyandImageProxy.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 newFileProxyfor 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 byFileProxyandImageProxy.
This can still be overridden on a per-instance basis withwithClient(OkHttpClient).- Parameters:
httpClient- The defaultOkHttpClientto use while making HTTP requests- Throws:
IllegalArgumentException- If the providedOkHttpClientis 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 customOkHttpClientto use while making HTTP requests- Returns:
- This proxy for chaining convenience.
- Throws:
IllegalArgumentException- If the providedOkHttpClientis null
-
download
Retrieves theInputStreamof this file- Returns:
CompletableFuturewhich holds anInputStream, theInputStreammust 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:
CompletableFuturewhich holds aPathwhich 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:
CompletableFuturewhich 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:
CompletableFuturewhich 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
-
downloadAsFileUpload
Returns aFileUploadwhich supplies a data stream of this attachment, with the given file name.
The returnedFileUploadcan be reused safely, and does not need to be closed.- Parameters:
name- The name of the to-be-uploaded file- Returns:
FileUploadfrom this attachment.- Throws:
IllegalArgumentException- If the file name is null or blank
-