All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dv8tion.jda.api.utils.AttachmentProxy Maven / Gradle / Ivy

Go to download

Java wrapper for the popular chat & VOIP service: Discord https://discord.com

The newest version!
/*
 * Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.dv8tion.jda.api.utils;

import net.dv8tion.jda.api.entities.Icon;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.FutureUtil;
import net.dv8tion.jda.internal.utils.IOUtil;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;

/**
 * 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. */ public class AttachmentProxy extends FileProxy { /** * Constructs a new {@link AttachmentProxy} for the provided URL. * * @param url * The URL to download the attachment from * * @throws IllegalArgumentException * If the provided URL is null */ public AttachmentProxy(@Nonnull String url) { super(url); } /** * Returns the attachment URL for the specified width and height. *
The width and height is a best-effort resize from Discord. * * @param width * The width of the image * @param height * The height of the image * * @return URL of the attachment with the specified width and height */ @Nonnull public String getUrl(int width, int height) { Checks.positive(width, "Image width"); Checks.positive(height, "Image height"); return IOUtil.addQuery(getUrl(), "width", width, "height", height); } /** * Retrieves the {@link 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. * * @param width * The width of this image, must be positive * @param height * The height of this image, must be positive * * @throws IllegalArgumentException * If any of the follow checks are true *
    *
  • The requested width is negative or 0
  • *
  • The requested height is negative or 0
  • *
* * @return {@link CompletableFuture} which holds an {@link InputStream}, the {@link InputStream} must be closed manually. */ @Nonnull @CheckReturnValue public CompletableFuture download(int width, int height) { return download(getUrl(width, 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. * * @param width * The width of this image, must be positive * @param height * The height of this image, must be positive * * @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
  • *
* * @return {@link CompletableFuture} which holds a {@link Path} which corresponds to the location the file has been downloaded. * */ @Nonnull @CheckReturnValue public CompletableFuture downloadToPath(int width, int height) { return downloadToPath(getUrl(width, 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. * * @param file * The file in which to download the image * @param width * The width of this image, must be positive * @param height * The height of this image, must be positive * * @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 {@link Files#isRegularFile(Path, LinkOption...) regular file}
  • *
  • The target file exists and is not {@link Files#isWritable(Path) writable}
  • *
  • The requested width is negative or 0
  • *
  • The requested height is negative or 0
  • *
* * @return {@link CompletableFuture} which holds a {@link File}, it is the same as the file passed in the parameters. */ @Nonnull @CheckReturnValue public CompletableFuture downloadToFile(@Nonnull File file, int width, int height) { Checks.notNull(file, "File"); final CompletableFuture downloadToPathFuture = downloadToPath(getUrl(width, height), file.toPath()); return FutureUtil.thenApplyCancellable(downloadToPathFuture, Path::toFile); } /** * 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. * * @param path * The file in which to download the image * @param width * The width of this image, must be positive * @param height * The height of this image, must be positive * * @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 {@link Files#isRegularFile(Path, LinkOption...) regular file}
  • *
  • The target path exists and is not {@link Files#isWritable(Path) writable}
  • *
  • The requested width is negative or 0
  • *
  • The requested height is negative or 0
  • *
* * @return {@link CompletableFuture} which holds a {@link Path}, it is the same as the path passed in the parameters. */ @Nonnull @CheckReturnValue public CompletableFuture downloadToPath(@Nonnull Path path, int width, int height) { Checks.notNull(path, "Path"); return downloadToPath(getUrl(width, height), path); } @Nonnull @CheckReturnValue private CompletableFuture downloadAsIcon(String url) { final CompletableFuture downloadFuture = download(url); return FutureUtil.thenApplyCancellable(downloadFuture, stream -> { try (final InputStream ignored = stream) { return Icon.from(stream); } catch (IOException e) { throw new UncheckedIOException(e); } }); } /** * Downloads the data of this attachment, and constructs an {@link Icon} from the data. * * @return {@link CompletableFuture} which holds an {@link Icon}. */ @Nonnull @CheckReturnValue public CompletableFuture downloadAsIcon() { return downloadAsIcon(getUrl()); } /** * Downloads the data of this attachment, at the specified size, and constructs an {@link 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. * * @param width * The width of this image, must be positive * @param height * The height of this image, must be positive * * @throws IllegalArgumentException * If any of the follow checks are true *
    *
  • The requested width is negative or 0
  • *
  • The requested height is negative or 0
  • *
* * @return {@link CompletableFuture} which holds an {@link Icon}. */ @Nonnull @CheckReturnValue public CompletableFuture downloadAsIcon(int width, int height) { return downloadAsIcon(getUrl(width, height)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy