Skip to main content

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