ruma_federation_api/authenticated_media/
get_content.rs
1pub mod v1 {
6 use std::time::Duration;
11
12 use ruma_common::{
13 api::{request, Metadata},
14 metadata,
15 };
16
17 use crate::authenticated_media::{ContentMetadata, FileOrLocation};
18
19 const METADATA: Metadata = metadata! {
20 method: GET,
21 rate_limited: true,
22 authentication: ServerSignatures,
23 history: {
24 unstable => "/_matrix/federation/unstable/org.matrix.msc3916.v2/media/download/:media_id",
25 1.11 => "/_matrix/federation/v1/media/download/:media_id",
26 }
27 };
28
29 #[request]
31 pub struct Request {
32 #[ruma_api(path)]
34 pub media_id: String,
35
36 #[ruma_api(query)]
41 #[serde(
42 with = "ruma_common::serde::duration::ms",
43 default = "ruma_common::media::default_download_timeout",
44 skip_serializing_if = "ruma_common::media::is_default_download_timeout"
45 )]
46 pub timeout_ms: Duration,
47 }
48
49 impl Request {
50 pub fn new(media_id: String) -> Self {
52 Self { media_id, timeout_ms: ruma_common::media::default_download_timeout() }
53 }
54 }
55
56 #[derive(Debug, Clone)]
58 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
59 pub struct Response {
60 pub metadata: ContentMetadata,
62
63 pub content: FileOrLocation,
65 }
66
67 impl Response {
68 pub fn new(metadata: ContentMetadata, content: FileOrLocation) -> Self {
70 Self { metadata, content }
71 }
72 }
73
74 #[cfg(feature = "client")]
75 impl ruma_common::api::IncomingResponse for Response {
76 type EndpointError = ruma_common::api::error::MatrixError;
77
78 fn try_from_http_response<T: AsRef<[u8]>>(
79 http_response: http::Response<T>,
80 ) -> Result<Self, ruma_common::api::error::FromHttpResponseError<Self::EndpointError>>
81 {
82 use ruma_common::api::EndpointError;
83
84 if http_response.status().as_u16() < 400 {
85 let (metadata, content) =
86 crate::authenticated_media::try_from_multipart_mixed_response(http_response)?;
87 Ok(Self { metadata, content })
88 } else {
89 Err(ruma_common::api::error::FromHttpResponseError::Server(
90 ruma_common::api::error::MatrixError::from_http_response(http_response),
91 ))
92 }
93 }
94 }
95
96 #[cfg(feature = "server")]
97 impl ruma_common::api::OutgoingResponse for Response {
98 fn try_into_http_response<T: Default + bytes::BufMut>(
99 self,
100 ) -> Result<http::Response<T>, ruma_common::api::error::IntoHttpError> {
101 crate::authenticated_media::try_into_multipart_mixed_response(
102 &self.metadata,
103 &self.content,
104 )
105 }
106 }
107}