ruma_client_api/media/
create_content_async.rs

1//! `PUT /_matrix/media/*/upload/{serverName}/{mediaId}`
2//!
3//! Upload media to an MXC URI that was created with create_mxc_uri.
4
5pub mod v3 {
6    //! `/v3/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#put_matrixmediav3uploadservernamemediaid
9
10    use http::header::CONTENT_TYPE;
11    use ruma_common::{
12        api::{request, response, Metadata},
13        metadata, IdParseError, MxcUri, OwnedServerName,
14    };
15
16    const METADATA: Metadata = metadata! {
17        method: PUT,
18        rate_limited: true,
19        authentication: AccessToken,
20        history: {
21            unstable => "/_matrix/media/unstable/fi.mau.msc2246/upload/:server_name/:media_id",
22            1.7 => "/_matrix/media/v3/upload/:server_name/:media_id",
23        }
24    };
25
26    /// Request type for the `create_content_async` endpoint.
27    #[request(error = crate::Error)]
28    pub struct Request {
29        /// The server name from the mxc:// URI (the authoritory component).
30        #[ruma_api(path)]
31        pub server_name: OwnedServerName,
32
33        /// The media ID from the mxc:// URI (the path component).
34        #[ruma_api(path)]
35        pub media_id: String,
36
37        /// The file contents to upload.
38        #[ruma_api(raw_body)]
39        pub file: Vec<u8>,
40
41        /// The content type of the file being uploaded.
42        #[ruma_api(header = CONTENT_TYPE)]
43        pub content_type: Option<String>,
44
45        /// The name of the file being uploaded.
46        #[ruma_api(query)]
47        #[serde(skip_serializing_if = "Option::is_none")]
48        pub filename: Option<String>,
49        // TODO: How does this and msc2448 (blurhash) interact?
50    }
51
52    /// Response type for the `create_content_async` endpoint.
53    #[response(error = crate::Error)]
54    pub struct Response {}
55
56    impl Request {
57        /// Creates a new `Request` with the given file contents.
58        pub fn new(media_id: String, server_name: OwnedServerName, file: Vec<u8>) -> Self {
59            Self { media_id, server_name, file, content_type: None, filename: None }
60        }
61
62        /// Creates a new `Request` with the given url and file contents.
63        pub fn from_url(url: &MxcUri, file: Vec<u8>) -> Result<Self, IdParseError> {
64            let (server_name, media_id) = url.parts()?;
65            Ok(Self::new(media_id.to_owned(), server_name.to_owned(), file))
66        }
67    }
68}