#[non_exhaustive]pub struct Media {
pub content_type: String,
pub source: MediaSource,
pub name: Option<String>,
}Expand description
An image, audio or other file in a message.
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.content_type: StringMIME type, for example image/png.
source: MediaSourceWhere the bytes are.
name: Option<String>File name, if known.
Implementations§
Source§impl Media
impl Media
Sourcepub fn new(content_type: impl Into<String>, source: MediaSource) -> Self
pub fn new(content_type: impl Into<String>, source: MediaSource) -> Self
Creates media from its MIME type and source.
Sourcepub fn from_url(content_type: impl Into<String>, url: impl Into<String>) -> Self
pub fn from_url(content_type: impl Into<String>, url: impl Into<String>) -> Self
Creates media the provider downloads from url. Not every provider accepts URLs; use
Media::from_bytes when yours does not.
Sourcepub fn image_url(url: impl Into<String>) -> Self
pub fn image_url(url: impl Into<String>) -> Self
Creates an image the provider downloads from url. The MIME type is guessed from the
file extension with mime::from_path; URLs without an image extension are sent as
image/jpeg. Use Media::from_url to set it yourself.
Sourcepub fn from_bytes(
content_type: impl Into<String>,
bytes: impl AsRef<[u8]>,
) -> Self
pub fn from_bytes( content_type: impl Into<String>, bytes: impl AsRef<[u8]>, ) -> Self
Creates media from its bytes, for example an image read from a file. The bytes are sent base64 encoded inside the request.
Sourcepub fn from_base64(
content_type: impl Into<String>,
data: impl Into<String>,
) -> Self
pub fn from_base64( content_type: impl Into<String>, data: impl Into<String>, ) -> Self
Creates media from bytes that are already base64 encoded.
Sourcepub fn with_name(self, name: impl Into<String>) -> Self
pub fn with_name(self, name: impl Into<String>) -> Self
Sets the file name. Some providers show it to the model.
Sourcepub fn extension(&self) -> &str
pub fn extension(&self) -> &str
File extension of the MIME type, see mime::extension.
Sourcepub fn file_name(&self, stem: &str) -> String
pub fn file_name(&self, stem: &str) -> String
A file name for the media: its name, else stem, with the extension of
its type unless it already ends in a known one (see mime::with_extension).
let audio = Media::from_bytes("audio/mpeg", b"ID3");
assert_eq!(audio.file_name("speech"), "speech.mp3");
assert_eq!(audio.with_name("intro.wav").file_name("speech"), "intro.wav");Sourcepub fn to_url(&self) -> String
pub fn to_url(&self) -> String
The media as a URL: its own URL, or a data: URL with the base64 bytes.