#[non_exhaustive]pub struct FileUpload {
pub name: CompactString,
pub data: Bytes,
pub content_type: Option<CompactString>,
pub description: Option<CompactString>,
pub title: Option<CompactString>,
pub duration_secs: Option<f64>,
pub waveform: Option<CompactString>,
pub is_spoiler: bool,
}Expand description
A file uploaded with a request, e.g. a message attachment.
Request bodies that accept files have a files field (D-087). djs-rest then sends the
request as multipart/form-data: the file at index n becomes the part files[n], the
JSON body the part payload_json, and the body’s attachments gets an entry for each file
with its name and metadata (D-094). Embeds can show an uploaded image with the URL
attachment://{name}.
The data is Bytes (D-093): cloning a FileUpload (to send the same file to many
channels, or for a retry) does not copy the file, and neither do Bytes::from_static
(e.g. with include_bytes!) and Bytes::from(Vec<u8>).
When content_type is not set, it is guessed from the extension of
name (D-095).
See Uploading Files.
discord.js counterpart: AttachmentBuilder / RawFile of @discordjs/rest.
§Examples
use djs_model::rest::FileUpload;
let mut image = FileUpload::new("ferris.png", &b"\x89PNG\r\n\x1a\n..."[..]);
image.description = Some("Ferris the crab".into());
image.is_spoiler = true;
// Cloning shares the data.
let copy = image.clone();
assert_eq!(copy.data.as_ptr(), image.data.as_ptr());Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.name: CompactStringThe file name, including its extension (e.g. ferris.png).
data: BytesThe file’s contents.
content_type: Option<CompactString>The media type (e.g. image/png); guessed
from the file name when None.
description: Option<CompactString>The alt text (max 1024 characters).
title: Option<CompactString>The file’s title.
duration_secs: Option<f64>The duration of an audio or video file in seconds (required for voice messages).
waveform: Option<CompactString>A base64-encoded sampled waveform (required for voice messages).
is_spoiler: boolWhether the file is blurred until clicked (sets
AttachmentFlags::IS_SPOILER).
Implementations§
Source§impl FileUpload
impl FileUpload
Sourcepub fn new(name: impl Into<CompactString>, data: impl Into<Bytes>) -> Self
pub fn new(name: impl Into<CompactString>, data: impl Into<Bytes>) -> Self
Creates an upload of data named name, without metadata.
data can be anything that converts into Bytes without copying: a Vec<u8>, a
String, a &'static [u8] or a &'static str.
discord.js counterpart: AttachmentBuilder.
Sourcepub fn target_users(user_ids: impl IntoIterator<Item = UserId>) -> Self
pub fn target_users(user_ids: impl IntoIterator<Item = UserId>) -> Self
A CSV file named users.csv listing user_ids, one per line (D-127): the
target_users_file of Update Target Users and Create Channel Invite.
discord.js counterpart: the targetUsersFile of RESTPutAPIInviteTargetUsersFormDataBody (discord-api-types).
§Examples
use djs_model::{id::UserId, rest::FileUpload};
let file = FileUpload::target_users([UserId::new(7), UserId::new(8)]);
assert_eq!(file.name, "users.csv");
assert_eq!(&file.data[..], b"7\n8");
assert_eq!(file.content_type.as_deref(), Some("text/csv"));