ruma_client_api/media/
get_content.rs1pub mod v3 {
6    use std::time::Duration;
11
12    use http::header::{CONTENT_DISPOSITION, CONTENT_TYPE};
13    use ruma_common::{
14        api::{request, response, Metadata},
15        http_headers::ContentDisposition,
16        metadata, IdParseError, MxcUri, OwnedServerName,
17    };
18
19    use crate::http_headers::CROSS_ORIGIN_RESOURCE_POLICY;
20
21    const METADATA: Metadata = metadata! {
22        method: GET,
23        rate_limited: false,
24        authentication: None,
25        history: {
26            1.0 => "/_matrix/media/r0/download/:server_name/:media_id",
27            1.1 => "/_matrix/media/v3/download/:server_name/:media_id",
28            1.11 => deprecated,
29        }
30    };
31
32    #[request(error = crate::Error)]
34    #[deprecated = "\
35        Since Matrix 1.11, clients should use `authenticated_media::get_content::v1::Request` \
36        instead if the homeserver supports it.\
37    "]
38    pub struct Request {
39        #[ruma_api(path)]
41        pub server_name: OwnedServerName,
42
43        #[ruma_api(path)]
45        pub media_id: String,
46
47        #[ruma_api(query)]
51        #[serde(
52            default = "ruma_common::serde::default_true",
53            skip_serializing_if = "ruma_common::serde::is_true"
54        )]
55        pub allow_remote: bool,
56
57        #[ruma_api(query)]
62        #[serde(
63            with = "ruma_common::serde::duration::ms",
64            default = "ruma_common::media::default_download_timeout",
65            skip_serializing_if = "ruma_common::media::is_default_download_timeout"
66        )]
67        pub timeout_ms: Duration,
68
69        #[ruma_api(query)]
74        #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
75        pub allow_redirect: bool,
76    }
77
78    #[response(error = crate::Error)]
80    pub struct Response {
81        #[ruma_api(raw_body)]
83        pub file: Vec<u8>,
84
85        #[ruma_api(header = CONTENT_TYPE)]
87        pub content_type: Option<String>,
88
89        #[ruma_api(header = CONTENT_DISPOSITION)]
92        pub content_disposition: Option<ContentDisposition>,
93
94        #[ruma_api(header = CROSS_ORIGIN_RESOURCE_POLICY)]
100        pub cross_origin_resource_policy: Option<String>,
101    }
102
103    #[allow(deprecated)]
104    impl Request {
105        pub fn new(media_id: String, server_name: OwnedServerName) -> Self {
107            Self {
108                media_id,
109                server_name,
110                allow_remote: true,
111                timeout_ms: ruma_common::media::default_download_timeout(),
112                allow_redirect: false,
113            }
114        }
115
116        pub fn from_url(url: &MxcUri) -> Result<Self, IdParseError> {
118            let (server_name, media_id) = url.parts()?;
119
120            Ok(Self::new(media_id.to_owned(), server_name.to_owned()))
121        }
122    }
123
124    impl Response {
125        pub fn new(
129            file: Vec<u8>,
130            content_type: String,
131            content_disposition: ContentDisposition,
132        ) -> Self {
133            Self {
134                file,
135                content_type: Some(content_type),
136                content_disposition: Some(content_disposition),
137                cross_origin_resource_policy: Some("cross-origin".to_owned()),
138            }
139        }
140    }
141}