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