ruma_client_api/authenticated_media/
get_content.rs

1//! `GET /_matrix/client/*/media/download/{serverName}/{mediaId}`
2//!
3//! Retrieve content from the media store.
4
5pub mod v1 {
6    //! `/v1/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixclientv1mediadownloadservernamemediaid
9
10    use std::time::Duration;
11
12    use http::header::{CONTENT_DISPOSITION, CONTENT_TYPE};
13    use ruma_common::{
14        IdParseError, MxcUri, OwnedServerName,
15        api::{auth_scheme::AccessToken, request, response},
16        http_headers::ContentDisposition,
17        metadata,
18    };
19
20    metadata! {
21        method: GET,
22        rate_limited: true,
23        authentication: AccessToken,
24        history: {
25            unstable("org.matrix.msc3916") => "/_matrix/client/unstable/org.matrix.msc3916/media/download/{server_name}/{media_id}",
26            1.11 | stable("org.matrix.msc3916.stable") => "/_matrix/client/v1/media/download/{server_name}/{media_id}",
27        }
28    }
29
30    /// Request type for the `get_media_content` endpoint.
31    #[request(error = crate::Error)]
32    pub struct Request {
33        /// The server name from the mxc:// URI (the authoritory component).
34        #[ruma_api(path)]
35        pub server_name: OwnedServerName,
36
37        /// The media ID from the mxc:// URI (the path component).
38        #[ruma_api(path)]
39        pub media_id: String,
40
41        /// The maximum duration that the client is willing to wait to start receiving data, in the
42        /// case that the content has not yet been uploaded.
43        ///
44        /// The default value is 20 seconds.
45        #[ruma_api(query)]
46        #[serde(
47            with = "ruma_common::serde::duration::ms",
48            default = "ruma_common::media::default_download_timeout",
49            skip_serializing_if = "ruma_common::media::is_default_download_timeout"
50        )]
51        pub timeout_ms: Duration,
52    }
53
54    /// Response type for the `get_media_content` endpoint.
55    #[response(error = crate::Error)]
56    pub struct Response {
57        /// The content that was previously uploaded.
58        #[ruma_api(raw_body)]
59        pub file: Vec<u8>,
60
61        /// The content type of the file that was previously uploaded.
62        #[ruma_api(header = CONTENT_TYPE)]
63        pub content_type: Option<String>,
64
65        /// The value of the `Content-Disposition` HTTP header, possibly containing the name of the
66        /// file that was previously uploaded.
67        #[ruma_api(header = CONTENT_DISPOSITION)]
68        pub content_disposition: Option<ContentDisposition>,
69    }
70
71    impl Request {
72        /// Creates a new `Request` with the given media ID and server name.
73        pub fn new(media_id: String, server_name: OwnedServerName) -> Self {
74            Self {
75                media_id,
76                server_name,
77                timeout_ms: ruma_common::media::default_download_timeout(),
78            }
79        }
80
81        /// Creates a new `Request` with the given URI.
82        pub fn from_uri(uri: &MxcUri) -> Result<Self, IdParseError> {
83            let (server_name, media_id) = uri.parts()?;
84
85            Ok(Self::new(media_id.to_owned(), server_name.to_owned()))
86        }
87    }
88
89    impl Response {
90        /// Creates a new `Response` with the given file contents.
91        pub fn new(
92            file: Vec<u8>,
93            content_type: String,
94            content_disposition: ContentDisposition,
95        ) -> Self {
96            Self {
97                file,
98                content_type: Some(content_type),
99                content_disposition: Some(content_disposition),
100            }
101        }
102    }
103}