Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FileUpload class #2120

Merged
merged 13 commits into from
May 13, 2022
13 changes: 6 additions & 7 deletions src/main/java/net/dv8tion/jda/api/utils/AttachedFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ static FileUpload fromData(@Nonnull Path path, @Nonnull String name, @Nonnull Op
@Nonnull
static FileUpload fromData(@Nonnull Path path, @Nonnull OpenOption... options)
{
return FileUpload.fromData(path, path.getFileName().toString(), options);
return FileUpload.fromData(path, options);
}

/**
Expand Down Expand Up @@ -241,7 +241,7 @@ static AttachmentUpdate fromAttachment(@Nonnull Message.Attachment attachment)
* @throws IllegalStateException
* If this attachment has already been used
*/
default void claim() {}
void claim();

/**
* Whether this attached file has already been used.
Expand All @@ -251,20 +251,19 @@ default void claim() {}
*
* @return True if this attachment has already been used
*/
default boolean isClaimed()
{
return false;
}
boolean isClaimed();

/**
* Used internally to build the multipart request.
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
*
* <p>The index can be used as a unique identifier for the multipart name, which is required to be unique by Discord.
*
* @param builder
* The {@link MultipartBody.Builder} used for the request body
* @param index
* The index of the attachment, ignored for {@link AttachmentUpdate}
*/
default void addPart(MultipartBody.Builder builder, int index) {}
void addPart(@Nonnull MultipartBody.Builder builder, int index);

/**
* Used internally to build attachment descriptions for requests.
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/net/dv8tion/jda/api/utils/AttachmentUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.utils.Checks;
import okhttp3.MultipartBody;
import org.jetbrains.annotations.NotNull;
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -105,6 +107,18 @@ public long getIdLong()
return id;
}

@Override
public void claim() {}

@Override
public boolean isClaimed()
{
return false;
}

@Override
public void addPart(@NotNull MultipartBody.Builder builder, int index) {}

@Nonnull
@Override
public DataObject toAttachmentData(int index)
Expand All @@ -121,6 +135,6 @@ public void close() {}
@Override
public String toString()
{
return "AttachedFile[Attachment]:" + name + '(' + id + ')';
return "AttachedFile[Attachment]" + (name == null ? "" : ":" + name) + '(' + id + ')';
}
}
12 changes: 8 additions & 4 deletions src/main/java/net/dv8tion/jda/api/utils/FileUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected FileUpload(InputStream resource, String name)
public static FileUpload fromData(@Nonnull InputStream data, @Nonnull String name)
{
Checks.notNull(data, "Data");
Checks.notNull(name, "Name");
Checks.notBlank(name, "Name");
return new FileUpload(data, name);
}

Expand Down Expand Up @@ -191,13 +191,14 @@ public static FileUpload fromData(@Nonnull Path path, @Nonnull String name, @Non
{
Checks.notNull(path, "Path");
Checks.noneNull(options, "Options");
Checks.check(Files.isReadable(path), "File for specified path cannot be read. Path: %s", path);
try
{
return fromData(Files.newInputStream(path, options), name);
}
catch (IOException e)
{
throw new UncheckedIOException(e);
throw new UncheckedIOException("Could not open file for specified path. Path: " + path, e);
}
}

Expand All @@ -224,7 +225,10 @@ public static FileUpload fromData(@Nonnull Path path, @Nonnull String name, @Non
@Nonnull
public static FileUpload fromData(@Nonnull Path path, @Nonnull OpenOption... options)
{
return fromData(path, path.getFileName().toString(), options);
Checks.notNull(path, "Path");
Path fileName = path.getFileName();
Checks.check(fileName != null, "Path does not have a file name. Path: %s", path);
return fromData(path, fileName.toString(), options);
}

/**
Expand Down Expand Up @@ -264,7 +268,7 @@ public synchronized boolean isClaimed()
}

@Override
public void addPart(MultipartBody.Builder builder, int index)
public void addPart(@Nonnull MultipartBody.Builder builder, int index)
{
builder.addFormDataPart("files[" + index + "]", name, IOUtil.createRequestBody(Requester.MEDIA_TYPE_OCTET, resource));
}
Expand Down