Interface AttachedFile
- All Superinterfaces:
AutoCloseable
,Closeable
- All Known Implementing Classes:
AttachmentUpdate
,FileUpload
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addPart
(okhttp3.MultipartBody.Builder builder, int index) Used internally to build the multipart request.void
claim()
Marks this attachment as used and throws if it has already been used.static okhttp3.MultipartBody.Builder
createMultipartBody
(List<? extends AttachedFile> files, DataObject payloadJson) Build a complete request using the provided files and payload data.static AttachmentUpdate
fromAttachment
(long id) Creates anAttachmentUpdate
with the given attachment id.static AttachmentUpdate
fromAttachment
(String id) Creates anAttachmentUpdate
with the given attachment id.static AttachmentUpdate
fromAttachment
(Message.Attachment attachment) Creates anAttachmentUpdate
with the given attachment.static FileUpload
Create a newFileUpload
for a byte array.static FileUpload
Create a newFileUpload
for a local file.static FileUpload
Create a newFileUpload
for a local file.static FileUpload
fromData
(InputStream data, String name) Create a newFileUpload
for an input stream.static FileUpload
fromData
(Path path, String name, OpenOption... options) Create a newFileUpload
for a local file.static FileUpload
fromData
(Path path, OpenOption... options) Create a newFileUpload
for a local file.boolean
Whether this attached file has already been used.toAttachmentData
(int index) Used internally to build attachment descriptions for requests.
-
Method Details
-
fromData
Create a newFileUpload
for an input stream.
This is used to upload data to discord for various purposes.The
InputStream
will be closed on consumption by the request. You can useFileUpload.close()
to close the stream manually.- Parameters:
data
- TheInputStream
to uploadname
- The representative name to use for the file- Returns:
FileUpload
- Throws:
IllegalArgumentException
- If null is provided or the name is empty- See Also:
-
fromData
Create a newFileUpload
for a byte array.
This is used to upload data to discord for various purposes.- Parameters:
data
- Thebyte[]
to uploadname
- The representative name to use for the file- Returns:
FileUpload
- Throws:
IllegalArgumentException
- If null is provided or the name is empty
-
fromData
Create a newFileUpload
for a local file.
This is used to upload data to discord for various purposes.This opens a
FileInputStream
, which will be closed on consumption by the request. You can useFileUpload.close()
to close the stream manually.- Parameters:
file
- TheFile
to uploadname
- The representative name to use for the file- Returns:
FileUpload
- Throws:
IllegalArgumentException
- If null is provided or the name is emptyUncheckedIOException
- If an IOException is thrown while opening the file- See Also:
-
fromData
Create a newFileUpload
for a local file.
This is used to upload data to discord for various purposes.This opens a
FileInputStream
, which will be closed on consumption by the request. You can useFileUpload.close()
to close the stream manually.- Parameters:
file
- TheFile
to upload- Returns:
FileUpload
- Throws:
IllegalArgumentException
- If null is providedUncheckedIOException
- If an IOException is thrown while opening the file- See Also:
-
fromData
@Nonnull static FileUpload fromData(@Nonnull Path path, @Nonnull String name, @Nonnull OpenOption... options) Create a newFileUpload
for a local file.
This is used to upload data to discord for various purposes.This opens the path using
Files.newInputStream(Path, OpenOption...)
, which will be closed on consumption by the request. You can useFileUpload.close()
to close the stream manually.- Parameters:
path
- ThePath
of the file to uploadname
- The representative name to use for the fileoptions
- TheOpenOptions
specifying how the file is opened- Returns:
FileUpload
- Throws:
IllegalArgumentException
- If null is provided or the name is emptyUncheckedIOException
- If an IOException is thrown while opening the file
-
fromData
Create a newFileUpload
for a local file.
This is used to upload data to discord for various purposes. UsesPath.getFileName()
to specify the name of the file, to customize the filename usefromData(Path, String, OpenOption...)
.This opens the path using
Files.newInputStream(Path, OpenOption...)
, which will be closed on consumption by the request. You can useFileUpload.close()
to close the stream manually.- Parameters:
path
- ThePath
of the file to uploadoptions
- TheOpenOptions
specifying how the file is opened- Returns:
FileUpload
- Throws:
IllegalArgumentException
- If null is providedUncheckedIOException
- If an IOException is thrown while opening the file
-
fromAttachment
Creates anAttachmentUpdate
with the given attachment id.
This is primarily used for message edit requests, to specify which attachments to retain in the message after the update.- Parameters:
id
- The id of the attachment to retain- Returns:
AttachmentUpdate
-
fromAttachment
Creates anAttachmentUpdate
with the given attachment id.
This is primarily used for message edit requests, to specify which attachments to retain in the message after the update.- Parameters:
id
- The id of the attachment to retain- Returns:
AttachmentUpdate
- Throws:
IllegalArgumentException
- If the id is not a valid snowflake
-
fromAttachment
Creates anAttachmentUpdate
with the given attachment.
This is primarily used for message edit requests, to specify which attachments to retain in the message after the update.- Parameters:
attachment
- The attachment to retain- Returns:
AttachmentUpdate
-
claim
void claim()Marks this attachment as used and throws if it has already been used.
This does nothing onAttachmentUpdate
.- Throws:
IllegalStateException
- If this attachment has already been used
-
isClaimed
boolean isClaimed()Whether this attached file has already been used.
When this is true,claim()
will throw anIllegalStateException
.Resources cannot be read multiple times, so repeated use of this instance is not allowed.
- Returns:
- True if this attachment has already been used
-
addPart
Used internally to build the multipart request.The index can be used as a unique identifier for the multipart name, which is required to be unique by Discord.
- Parameters:
builder
- TheMultipartBody.Builder
used for the request bodyindex
- The index of the attachment, ignored forAttachmentUpdate
-
toAttachmentData
Used internally to build attachment descriptions for requests.
This contains the id/index of the attachment, and the name of the file.- Parameters:
index
- The reference index (should be same asaddPart(MultipartBody.Builder, int)
)- Returns:
DataObject
for the attachment
-
createMultipartBody
@Nonnull static okhttp3.MultipartBody.Builder createMultipartBody(@Nonnull List<? extends AttachedFile> files, @Nullable DataObject payloadJson) Build a complete request using the provided files and payload data.
If the providedpayloadJson
is null, the multipart request will not setattachments
.- Parameters:
files
- The files to upload/editpayloadJson
- The payload data to send, excludingattachments
field- Returns:
MultipartBody.Builder
- Throws:
IllegalArgumentException
- If the file list is null
-