ruma_client_api/authenticated_media/
get_content_as_filename.rs1pub mod v1 {
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 const METADATA: Metadata = metadata! {
20 method: GET,
21 rate_limited: true,
22 authentication: AccessToken,
23 history: {
24 unstable => "/_matrix/client/unstable/org.matrix.msc3916/media/download/:server_name/:media_id/:filename",
25 1.11 => "/_matrix/client/v1/media/download/:server_name/:media_id/:filename",
26 }
27 };
28
29 #[request(error = crate::Error)]
31 pub struct Request {
32 #[ruma_api(path)]
34 pub server_name: OwnedServerName,
35
36 #[ruma_api(path)]
38 pub media_id: String,
39
40 #[ruma_api(path)]
42 pub filename: String,
43
44 #[ruma_api(query)]
49 #[serde(
50 with = "ruma_common::serde::duration::ms",
51 default = "ruma_common::media::default_download_timeout",
52 skip_serializing_if = "ruma_common::media::is_default_download_timeout"
53 )]
54 pub timeout_ms: Duration,
55 }
56
57 #[response(error = crate::Error)]
59 pub struct Response {
60 #[ruma_api(raw_body)]
62 pub file: Vec<u8>,
63
64 #[ruma_api(header = CONTENT_TYPE)]
66 pub content_type: Option<String>,
67
68 #[ruma_api(header = CONTENT_DISPOSITION)]
71 pub content_disposition: Option<ContentDisposition>,
72 }
73
74 impl Request {
75 pub fn new(media_id: String, server_name: OwnedServerName, filename: String) -> Self {
77 Self {
78 media_id,
79 server_name,
80 filename,
81 timeout_ms: ruma_common::media::default_download_timeout(),
82 }
83 }
84
85 pub fn from_uri(uri: &MxcUri, filename: String) -> Result<Self, IdParseError> {
87 let (server_name, media_id) = uri.parts()?;
88
89 Ok(Self::new(media_id.to_owned(), server_name.to_owned(), filename))
90 }
91 }
92
93 impl Response {
94 pub fn new(
96 file: Vec<u8>,
97 content_type: String,
98 content_disposition: ContentDisposition,
99 ) -> Self {
100 Self {
101 file,
102 content_type: Some(content_type),
103 content_disposition: Some(content_disposition),
104 }
105 }
106 }
107}