ruma_client_api/authenticated_media/
get_content_as_filename.rs

1//! `GET /_matrix/client/*/media/download/{serverName}/{mediaId}/{fileName}`
2//!
3//! Retrieve content from the media store, specifying a filename to return.
4
5pub mod v1 {
6    //! `/v1/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixclientv1mediadownloadservernamemediaidfilename
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::AccessToken, request, response},
16        http_headers::ContentDisposition,
17        metadata,
18    };
19
20    metadata! {
21        method: GET,
22        rate_limited: true,
23        authentication: AccessToken,
24        history: {
25            unstable("org.matrix.msc3916") => "/_matrix/client/unstable/org.matrix.msc3916/media/download/{server_name}/{media_id}/{filename}",
26            1.11 | stable("org.matrix.msc3916.stable") => "/_matrix/client/v1/media/download/{server_name}/{media_id}/{filename}",
27        }
28    }
29
30    /// Request type for the `get_media_content_as_filename` endpoint.
31    #[request(error = crate::Error)]
32    pub struct Request {
33        /// The server name from the mxc:// URI (the authoritory component).
34        #[ruma_api(path)]
35        pub server_name: OwnedServerName,
36
37        /// The media ID from the mxc:// URI (the path component).
38        #[ruma_api(path)]
39        pub media_id: String,
40
41        /// The filename to return in the `Content-Disposition` header.
42        #[ruma_api(path)]
43        pub filename: String,
44
45        /// The maximum duration that the client is willing to wait to start receiving data, in the
46        /// case that the content has not yet been uploaded.
47        ///
48        /// The default value is 20 seconds.
49        #[ruma_api(query)]
50        #[serde(
51            with = "ruma_common::serde::duration::ms",
52            default = "ruma_common::media::default_download_timeout",
53            skip_serializing_if = "ruma_common::media::is_default_download_timeout"
54        )]
55        pub timeout_ms: Duration,
56    }
57
58    /// Response type for the `get_media_content_as_filename` endpoint.
59    #[response(error = crate::Error)]
60    pub struct Response {
61        /// The content that was previously uploaded.
62        #[ruma_api(raw_body)]
63        pub file: Vec<u8>,
64
65        /// The content type of the file that was previously uploaded.
66        #[ruma_api(header = CONTENT_TYPE)]
67        pub content_type: Option<String>,
68
69        /// The value of the `Content-Disposition` HTTP header, possibly containing the name of the
70        /// file that was previously uploaded.
71        #[ruma_api(header = CONTENT_DISPOSITION)]
72        pub content_disposition: Option<ContentDisposition>,
73    }
74
75    impl Request {
76        /// Creates a new `Request` with the given media ID, server name and filename.
77        pub fn new(media_id: String, server_name: OwnedServerName, filename: String) -> Self {
78            Self {
79                media_id,
80                server_name,
81                filename,
82                timeout_ms: ruma_common::media::default_download_timeout(),
83            }
84        }
85
86        /// Creates a new `Request` with the given URI and filename.
87        pub fn from_uri(uri: &MxcUri, filename: String) -> Result<Self, IdParseError> {
88            let (server_name, media_id) = uri.parts()?;
89
90            Ok(Self::new(media_id.to_owned(), server_name.to_owned(), filename))
91        }
92    }
93
94    impl Response {
95        /// Creates a new `Response` with the given file.
96        pub fn new(
97            file: Vec<u8>,
98            content_type: String,
99            content_disposition: ContentDisposition,
100        ) -> Self {
101            Self {
102                file,
103                content_type: Some(content_type),
104                content_disposition: Some(content_disposition),
105            }
106        }
107    }
108}