Skip to main content

FileUpload

Struct FileUpload 

Source
#[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
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§name: CompactString

The file name, including its extension (e.g. ferris.png).

§data: Bytes

The 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: bool

Whether the file is blurred until clicked (sets AttachmentFlags::IS_SPOILER).

Implementations§

Source§

impl FileUpload

Source

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.

Source

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"));

Trait Implementations§

Source§

impl Clone for FileUpload

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FileUpload

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Shows the size of the data instead of the data.

Source§

impl PartialEq for FileUpload

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for FileUpload

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.