ruma_client_api/media/
get_content_as_filename.rs

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