ruma_client_api/media/
get_content.rs

1//! `GET /_matrix/media/*/download/{serverName}/{mediaId}`
2//!
3//! Retrieve content from the media store.
4
5pub mod v3 {
6    //! `/v3/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixmediav3downloadservernamemediaid
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::NoAuthentication, request, response},
16        http_headers::ContentDisposition,
17        metadata,
18    };
19
20    use crate::http_headers::CROSS_ORIGIN_RESOURCE_POLICY;
21
22    metadata! {
23        method: GET,
24        rate_limited: false,
25        authentication: NoAuthentication,
26        history: {
27            1.0 => "/_matrix/media/r0/download/{server_name}/{media_id}",
28            1.1 => "/_matrix/media/v3/download/{server_name}/{media_id}",
29            1.11 => deprecated,
30        }
31    }
32
33    /// Request type for the `get_media_content` endpoint.
34    #[request(error = crate::Error)]
35    #[deprecated = "\
36        Since Matrix 1.11, clients should use `authenticated_media::get_content::v1::Request` \
37        instead if the homeserver supports it.\
38    "]
39    pub struct Request {
40        /// The server name from the mxc:// URI (the authoritory component).
41        #[ruma_api(path)]
42        pub server_name: OwnedServerName,
43
44        /// The media ID from the mxc:// URI (the path component).
45        #[ruma_api(path)]
46        pub media_id: String,
47
48        /// Whether to fetch media deemed remote.
49        ///
50        /// Used to prevent routing loops. Defaults to `true`.
51        #[ruma_api(query)]
52        #[serde(
53            default = "ruma_common::serde::default_true",
54            skip_serializing_if = "ruma_common::serde::is_true"
55        )]
56        pub allow_remote: bool,
57
58        /// The maximum duration that the client is willing to wait to start receiving data, in the
59        /// case that the content has not yet been uploaded.
60        ///
61        /// The default value is 20 seconds.
62        #[ruma_api(query)]
63        #[serde(
64            with = "ruma_common::serde::duration::ms",
65            default = "ruma_common::media::default_download_timeout",
66            skip_serializing_if = "ruma_common::media::is_default_download_timeout"
67        )]
68        pub timeout_ms: Duration,
69
70        /// Whether the server may return a 307 or 308 redirect response that points at the
71        /// relevant media content.
72        ///
73        /// Unless explicitly set to `true`, the server must return the media content itself.
74        #[ruma_api(query)]
75        #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
76        pub allow_redirect: bool,
77    }
78
79    /// Response type for the `get_media_content` endpoint.
80    #[response(error = crate::Error)]
81    pub struct Response {
82        /// The content that was previously uploaded.
83        #[ruma_api(raw_body)]
84        pub file: Vec<u8>,
85
86        /// The content type of the file that was previously uploaded.
87        #[ruma_api(header = CONTENT_TYPE)]
88        pub content_type: Option<String>,
89
90        /// The value of the `Content-Disposition` HTTP header, possibly containing the name of the
91        /// file that was previously uploaded.
92        #[ruma_api(header = CONTENT_DISPOSITION)]
93        pub content_disposition: Option<ContentDisposition>,
94
95        /// The value of the `Cross-Origin-Resource-Policy` HTTP header.
96        ///
97        /// See [MDN] for the syntax.
98        ///
99        /// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Resource-Policy#syntax
100        #[ruma_api(header = CROSS_ORIGIN_RESOURCE_POLICY)]
101        pub cross_origin_resource_policy: Option<String>,
102    }
103
104    #[allow(deprecated)]
105    impl Request {
106        /// Creates a new `Request` with the given media ID and server name.
107        pub fn new(media_id: String, server_name: OwnedServerName) -> Self {
108            Self {
109                media_id,
110                server_name,
111                allow_remote: true,
112                timeout_ms: ruma_common::media::default_download_timeout(),
113                allow_redirect: false,
114            }
115        }
116
117        /// Creates a new `Request` with the given url.
118        pub fn from_url(url: &MxcUri) -> Result<Self, IdParseError> {
119            let (server_name, media_id) = url.parts()?;
120
121            Ok(Self::new(media_id.to_owned(), server_name.to_owned()))
122        }
123    }
124
125    impl Response {
126        /// Creates a new `Response` with the given file contents.
127        ///
128        /// The Cross-Origin Resource Policy defaults to `cross-origin`.
129        pub fn new(
130            file: Vec<u8>,
131            content_type: String,
132            content_disposition: ContentDisposition,
133        ) -> Self {
134            Self {
135                file,
136                content_type: Some(content_type),
137                content_disposition: Some(content_disposition),
138                cross_origin_resource_policy: Some("cross-origin".to_owned()),
139            }
140        }
141    }
142}