Class AttachmentProxy

java.lang.Object
net.dv8tion.jda.api.utils.FileProxy
net.dv8tion.jda.api.utils.AttachmentProxy

public class AttachmentProxy extends FileProxy
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.
  • Constructor Details

  • Method Details

    • getUrl

      @Nonnull public String getUrl(int width, int height)
      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 image
      height - The height of the image
      Returns:
      URL of the attachment with the specified width and height
    • download

      @Nonnull @CheckReturnValue public CompletableFuture<InputStream> download(int width, int height)
      Retrieves the InputStream 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 positive
      height - The height of this image, must be positive
      Returns:
      CompletableFuture which holds an InputStream, the InputStream 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

      @Nonnull @CheckReturnValue public CompletableFuture<Path> 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).
      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 positive
      height - The height of this image, must be positive
      Returns:
      CompletableFuture which holds a Path 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 image
      width - The width of this image, must be positive
      height - The height of this image, must be positive
      Returns:
      CompletableFuture which holds a File, 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 image
      width - The width of this image, must be positive
      height - The height of this image, must be positive
      Returns:
      CompletableFuture which holds a Path, 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 an Icon from the data.
      Returns:
      CompletableFuture which holds an Icon.
    • downloadAsIcon

      @Nonnull @CheckReturnValue public CompletableFuture<Icon> downloadAsIcon(int width, int height)
      Downloads the data of this attachment, at the specified size, and constructs an Icon 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 positive
      height - The height of this image, must be positive
      Returns:
      CompletableFuture which holds an Icon.
      Throws:
      IllegalArgumentException - If any of the follow checks are true
      • The requested width is negative or 0
      • The requested height is negative or 0