2024-07-02 15:43:10 -04:00
|
|
|
#![allow(deprecated)]
|
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
use axum::extract::State;
|
2024-07-07 14:56:18 -04:00
|
|
|
use axum_client_ip::InsecureClientIp;
|
2024-07-22 07:43:51 +00:00
|
|
|
use conduit::{
|
2024-08-15 20:08:53 +00:00
|
|
|
err,
|
2024-08-12 22:53:07 +00:00
|
|
|
utils::{self, content_disposition::make_content_disposition, math::ruma_from_usize},
|
2024-08-15 20:08:53 +00:00
|
|
|
Err, Result,
|
2024-07-22 07:43:51 +00:00
|
|
|
};
|
2024-08-16 01:13:20 +00:00
|
|
|
use ruma::{
|
|
|
|
api::client::media::{
|
|
|
|
create_content, get_content, get_content_as_filename, get_content_thumbnail, get_media_config,
|
|
|
|
get_media_preview,
|
|
|
|
},
|
|
|
|
Mxc,
|
2020-07-30 18:14:47 +02:00
|
|
|
};
|
2024-08-27 01:15:31 +00:00
|
|
|
use service::media::{Dim, FileMeta, MXC_LENGTH};
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2024-07-22 07:43:51 +00:00
|
|
|
use crate::{Ruma, RumaResponse};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-04-24 18:13:15 -07:00
|
|
|
/// Cache control for immutable objects
|
2024-05-07 12:53:30 -04:00
|
|
|
const CACHE_CONTROL_IMMUTABLE: &str = "public,max-age=31536000,immutable";
|
2024-04-24 18:13:15 -07:00
|
|
|
|
2024-04-28 18:30:19 -04:00
|
|
|
const CORP_CROSS_ORIGIN: &str = "cross-origin";
|
|
|
|
|
2024-01-16 20:44:20 -05:00
|
|
|
/// # `GET /_matrix/media/v3/config`
|
2021-08-31 19:14:37 +02:00
|
|
|
///
|
|
|
|
/// Returns max upload size.
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn get_media_config_route(
|
2024-07-16 08:05:25 +00:00
|
|
|
State(services): State<crate::State>, _body: Ruma<get_media_config::v3::Request>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<get_media_config::v3::Response> {
|
|
|
|
Ok(get_media_config::v3::Response {
|
2024-07-16 08:05:25 +00:00
|
|
|
upload_size: ruma_from_usize(services.globals.config.max_request_size),
|
2022-01-22 16:58:32 +01:00
|
|
|
})
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
|
|
|
|
2024-03-15 22:40:12 -04:00
|
|
|
/// # `GET /_matrix/media/v1/config`
|
|
|
|
///
|
|
|
|
/// This is a legacy endpoint ("/v1/") that some very old homeservers and/or
|
|
|
|
/// clients may call. conduwuit adds these for compatibility purposes.
|
|
|
|
/// See <https://spec.matrix.org/legacy/legacy/#id27>
|
|
|
|
///
|
|
|
|
/// Returns max upload size.
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn get_media_config_v1_route(
|
2024-07-16 08:05:25 +00:00
|
|
|
State(services): State<crate::State>, body: Ruma<get_media_config::v3::Request>,
|
2024-03-15 22:40:12 -04:00
|
|
|
) -> Result<RumaResponse<get_media_config::v3::Response>> {
|
2024-07-16 08:05:25 +00:00
|
|
|
get_media_config_route(State(services), body)
|
|
|
|
.await
|
|
|
|
.map(RumaResponse)
|
2024-03-15 22:40:12 -04:00
|
|
|
}
|
|
|
|
|
2024-02-09 23:16:06 -05:00
|
|
|
/// # `GET /_matrix/media/v3/preview_url`
|
|
|
|
///
|
|
|
|
/// Returns URL preview.
|
2024-07-07 14:56:18 -04:00
|
|
|
#[tracing::instrument(skip_all, fields(%client), name = "url_preview")]
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn get_media_preview_route(
|
2024-07-16 08:05:25 +00:00
|
|
|
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
|
|
|
|
body: Ruma<get_media_preview::v3::Request>,
|
2024-02-09 23:16:06 -05:00
|
|
|
) -> Result<get_media_preview::v3::Response> {
|
2024-07-07 14:56:18 -04:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
|
2024-02-09 23:16:06 -05:00
|
|
|
let url = &body.url;
|
2024-07-30 05:29:32 +00:00
|
|
|
if !services.media.url_preview_allowed(url) {
|
2024-08-15 20:08:53 +00:00
|
|
|
return Err!(Request(Forbidden(
|
|
|
|
debug_warn!(%sender_user, %url, "URL is not allowed to be previewed")
|
|
|
|
)));
|
2024-02-09 23:16:06 -05:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-08-15 20:08:53 +00:00
|
|
|
let preview = services.media.get_url_preview(url).await.map_err(|e| {
|
|
|
|
err!(Request(Unknown(
|
|
|
|
debug_error!(%sender_user, %url, "Failed to fetch a URL preview: {e}")
|
|
|
|
)))
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let res = serde_json::value::to_raw_value(&preview).map_err(|e| {
|
|
|
|
err!(Request(Unknown(
|
|
|
|
debug_error!(%sender_user, %url, "Failed to parse a URL preview: {e}")
|
|
|
|
)))
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(get_media_preview::v3::Response::from_raw_value(res))
|
2024-02-09 23:16:06 -05:00
|
|
|
}
|
|
|
|
|
2024-03-15 22:40:12 -04:00
|
|
|
/// # `GET /_matrix/media/v1/preview_url`
|
|
|
|
///
|
|
|
|
/// This is a legacy endpoint ("/v1/") that some very old homeservers and/or
|
|
|
|
/// clients may call. conduwuit adds these for compatibility purposes.
|
|
|
|
/// See <https://spec.matrix.org/legacy/legacy/#id27>
|
|
|
|
///
|
|
|
|
/// Returns URL preview.
|
2024-07-07 14:56:18 -04:00
|
|
|
#[tracing::instrument(skip_all, fields(%client), name = "url_preview")]
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn get_media_preview_v1_route(
|
2024-07-16 08:05:25 +00:00
|
|
|
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
|
|
|
|
body: Ruma<get_media_preview::v3::Request>,
|
2024-03-15 22:40:12 -04:00
|
|
|
) -> Result<RumaResponse<get_media_preview::v3::Response>> {
|
2024-07-16 08:05:25 +00:00
|
|
|
get_media_preview_route(State(services), InsecureClientIp(client), body)
|
2024-07-07 14:56:18 -04:00
|
|
|
.await
|
|
|
|
.map(RumaResponse)
|
2024-03-15 22:40:12 -04:00
|
|
|
}
|
|
|
|
|
2024-01-16 20:44:20 -05:00
|
|
|
/// # `POST /_matrix/media/v3/upload`
|
2021-08-31 19:14:37 +02:00
|
|
|
///
|
|
|
|
/// Permanently save media in the server.
|
|
|
|
///
|
|
|
|
/// - Some metadata will be saved in the database
|
|
|
|
/// - Media will be saved in the media/ directory
|
2024-07-07 14:56:18 -04:00
|
|
|
#[tracing::instrument(skip_all, fields(%client), name = "media_upload")]
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn create_content_route(
|
2024-07-16 08:05:25 +00:00
|
|
|
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
|
|
|
|
body: Ruma<create_content::v3::Request>,
|
2024-04-22 23:48:57 -04:00
|
|
|
) -> Result<create_content::v3::Response> {
|
2024-03-17 01:42:30 -04:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2024-08-12 22:53:07 +00:00
|
|
|
let content_disposition = make_content_disposition(None, body.content_type.as_deref(), body.filename.as_deref());
|
2024-08-16 01:13:20 +00:00
|
|
|
let mxc = Mxc {
|
|
|
|
server_name: services.globals.server_name(),
|
|
|
|
media_id: &utils::random_string(MXC_LENGTH),
|
|
|
|
};
|
2024-08-12 22:53:07 +00:00
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
services
|
2022-10-05 20:34:31 +02:00
|
|
|
.media
|
2021-06-06 16:58:32 +04:30
|
|
|
.create(
|
2024-06-28 23:23:59 +00:00
|
|
|
&mxc,
|
2024-08-16 01:13:20 +00:00
|
|
|
Some(sender_user),
|
2024-08-12 22:53:07 +00:00
|
|
|
Some(&content_disposition),
|
2024-06-03 18:52:02 -04:00
|
|
|
body.content_type.as_deref(),
|
2021-06-06 16:58:32 +04:30
|
|
|
&body.file,
|
|
|
|
)
|
|
|
|
.await?;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(create_content::v3::Response {
|
2024-08-16 01:13:20 +00:00
|
|
|
content_uri: mxc.to_string().into(),
|
2020-12-19 16:00:11 +01:00
|
|
|
blurhash: None,
|
2022-01-22 16:58:32 +01:00
|
|
|
})
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
|
|
|
|
2024-03-08 12:10:29 -05:00
|
|
|
/// # `POST /_matrix/media/v1/upload`
|
|
|
|
///
|
|
|
|
/// Permanently save media in the server.
|
|
|
|
///
|
|
|
|
/// This is a legacy endpoint ("/v1/") that some very old homeservers and/or
|
|
|
|
/// clients may call. conduwuit adds these for compatibility purposes.
|
2024-03-08 12:23:36 -05:00
|
|
|
/// See <https://spec.matrix.org/legacy/legacy/#id27>
|
2024-03-08 12:10:29 -05:00
|
|
|
///
|
|
|
|
/// - Some metadata will be saved in the database
|
|
|
|
/// - Media will be saved in the media/ directory
|
2024-07-07 14:56:18 -04:00
|
|
|
#[tracing::instrument(skip_all, fields(%client), name = "media_upload")]
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn create_content_v1_route(
|
2024-07-16 08:05:25 +00:00
|
|
|
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
|
|
|
|
body: Ruma<create_content::v3::Request>,
|
2024-03-08 12:10:29 -05:00
|
|
|
) -> Result<RumaResponse<create_content::v3::Response>> {
|
2024-07-16 08:05:25 +00:00
|
|
|
create_content_route(State(services), InsecureClientIp(client), body)
|
2024-07-07 14:56:18 -04:00
|
|
|
.await
|
|
|
|
.map(RumaResponse)
|
2022-01-27 16:12:39 +01:00
|
|
|
}
|
|
|
|
|
2024-01-17 19:54:17 -05:00
|
|
|
/// # `GET /_matrix/media/v3/download/{serverName}/{mediaId}`
|
2021-08-31 19:14:37 +02:00
|
|
|
///
|
|
|
|
/// Load media from our server or over federation.
|
|
|
|
///
|
|
|
|
/// - Only allows federation if `allow_remote` is true
|
2024-01-21 20:30:39 -05:00
|
|
|
/// - Only redirects if `allow_redirect` is true
|
|
|
|
/// - Uses client-provided `timeout_ms` if available, else defaults to 20
|
|
|
|
/// seconds
|
2024-07-07 14:56:18 -04:00
|
|
|
#[tracing::instrument(skip_all, fields(%client), name = "media_get")]
|
|
|
|
pub(crate) async fn get_content_route(
|
2024-07-16 08:05:25 +00:00
|
|
|
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
|
|
|
|
body: Ruma<get_content::v3::Request>,
|
2024-07-07 14:56:18 -04:00
|
|
|
) -> Result<get_content::v3::Response> {
|
2024-08-16 01:13:20 +00:00
|
|
|
let mxc = Mxc {
|
|
|
|
server_name: &body.server_name,
|
|
|
|
media_id: &body.media_id,
|
|
|
|
};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2020-07-30 18:14:47 +02:00
|
|
|
if let Some(FileMeta {
|
2024-06-24 08:27:06 +00:00
|
|
|
content,
|
2020-07-30 18:14:47 +02:00
|
|
|
content_type,
|
2024-05-07 21:32:06 -04:00
|
|
|
content_disposition,
|
2024-07-16 08:05:25 +00:00
|
|
|
}) = services.media.get(&mxc).await?
|
2020-07-30 18:14:47 +02:00
|
|
|
{
|
2024-08-12 22:53:07 +00:00
|
|
|
let content_disposition = make_content_disposition(content_disposition.as_ref(), content_type.as_deref(), None);
|
2024-05-07 16:36:22 -04:00
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(get_content::v3::Response {
|
2024-08-15 20:08:53 +00:00
|
|
|
file: content.expect("entire file contents"),
|
2024-08-12 22:53:07 +00:00
|
|
|
content_type: content_type.map(Into::into),
|
|
|
|
content_disposition: Some(content_disposition),
|
|
|
|
cross_origin_resource_policy: Some(CORP_CROSS_ORIGIN.into()),
|
2024-04-24 18:13:15 -07:00
|
|
|
cache_control: Some(CACHE_CONTROL_IMMUTABLE.into()),
|
2022-01-22 16:58:32 +01:00
|
|
|
})
|
2024-07-22 07:43:51 +00:00
|
|
|
} else if !services.globals.server_is_ours(&body.server_name) && body.allow_remote {
|
2024-08-15 20:08:53 +00:00
|
|
|
let response = services
|
|
|
|
.media
|
2024-08-16 01:13:20 +00:00
|
|
|
.fetch_remote_content_legacy(&mxc, body.allow_redirect, body.timeout_ms)
|
2024-08-15 20:08:53 +00:00
|
|
|
.await
|
|
|
|
.map_err(|e| err!(Request(NotFound(debug_warn!(%mxc, "Fetching media failed: {e:?}")))))?;
|
2024-05-07 21:32:06 -04:00
|
|
|
|
2024-08-12 22:53:07 +00:00
|
|
|
let content_disposition =
|
|
|
|
make_content_disposition(response.content_disposition.as_ref(), response.content_type.as_deref(), None);
|
2024-05-07 21:32:06 -04:00
|
|
|
|
|
|
|
Ok(get_content::v3::Response {
|
|
|
|
file: response.file,
|
2024-06-03 18:52:02 -04:00
|
|
|
content_type: response.content_type,
|
2024-08-12 22:53:07 +00:00
|
|
|
content_disposition: Some(content_disposition),
|
|
|
|
cross_origin_resource_policy: Some(CORP_CROSS_ORIGIN.into()),
|
|
|
|
cache_control: Some(CACHE_CONTROL_IMMUTABLE.into()),
|
2024-04-24 19:05:33 -07:00
|
|
|
})
|
2020-07-30 18:14:47 +02:00
|
|
|
} else {
|
2024-08-01 10:58:27 +00:00
|
|
|
Err!(Request(NotFound("Media not found.")))
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-08 12:10:29 -05:00
|
|
|
/// # `GET /_matrix/media/v1/download/{serverName}/{mediaId}`
|
|
|
|
///
|
|
|
|
/// Load media from our server or over federation.
|
|
|
|
///
|
|
|
|
/// This is a legacy endpoint ("/v1/") that some very old homeservers and/or
|
|
|
|
/// clients may call. conduwuit adds these for compatibility purposes.
|
2024-03-08 12:23:36 -05:00
|
|
|
/// See <https://spec.matrix.org/legacy/legacy/#id27>
|
2024-03-08 12:10:29 -05:00
|
|
|
///
|
|
|
|
/// - Only allows federation if `allow_remote` is true
|
|
|
|
/// - Only redirects if `allow_redirect` is true
|
|
|
|
/// - Uses client-provided `timeout_ms` if available, else defaults to 20
|
|
|
|
/// seconds
|
2024-07-07 14:56:18 -04:00
|
|
|
#[tracing::instrument(skip_all, fields(%client), name = "media_get")]
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn get_content_v1_route(
|
2024-07-16 08:05:25 +00:00
|
|
|
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
|
|
|
|
body: Ruma<get_content::v3::Request>,
|
2024-03-08 12:10:29 -05:00
|
|
|
) -> Result<RumaResponse<get_content::v3::Response>> {
|
2024-07-16 08:05:25 +00:00
|
|
|
get_content_route(State(services), InsecureClientIp(client), body)
|
2024-07-07 14:56:18 -04:00
|
|
|
.await
|
|
|
|
.map(RumaResponse)
|
2024-03-08 12:10:29 -05:00
|
|
|
}
|
|
|
|
|
2024-01-17 19:54:17 -05:00
|
|
|
/// # `GET /_matrix/media/v3/download/{serverName}/{mediaId}/{fileName}`
|
2022-01-27 00:25:20 +01:00
|
|
|
///
|
|
|
|
/// Load media from our server or over federation, permitting desired filename.
|
|
|
|
///
|
|
|
|
/// - Only allows federation if `allow_remote` is true
|
2024-01-21 20:30:39 -05:00
|
|
|
/// - Only redirects if `allow_redirect` is true
|
|
|
|
/// - Uses client-provided `timeout_ms` if available, else defaults to 20
|
|
|
|
/// seconds
|
2024-07-07 14:56:18 -04:00
|
|
|
#[tracing::instrument(skip_all, fields(%client), name = "media_get")]
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn get_content_as_filename_route(
|
2024-07-16 08:05:25 +00:00
|
|
|
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
|
|
|
|
body: Ruma<get_content_as_filename::v3::Request>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<get_content_as_filename::v3::Response> {
|
2024-08-16 01:13:20 +00:00
|
|
|
let mxc = Mxc {
|
|
|
|
server_name: &body.server_name,
|
|
|
|
media_id: &body.media_id,
|
|
|
|
};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-01-27 00:25:20 +01:00
|
|
|
if let Some(FileMeta {
|
2024-06-24 08:27:06 +00:00
|
|
|
content,
|
2024-01-14 22:39:08 -05:00
|
|
|
content_type,
|
2024-05-07 21:32:06 -04:00
|
|
|
content_disposition,
|
2024-07-16 08:05:25 +00:00
|
|
|
}) = services.media.get(&mxc).await?
|
2022-01-27 00:25:20 +01:00
|
|
|
{
|
2024-08-12 22:53:07 +00:00
|
|
|
let content_disposition =
|
|
|
|
make_content_disposition(content_disposition.as_ref(), content_type.as_deref(), Some(&body.filename));
|
2024-05-07 16:36:22 -04:00
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(get_content_as_filename::v3::Response {
|
2024-08-15 20:08:53 +00:00
|
|
|
file: content.expect("entire file contents"),
|
2024-08-12 22:53:07 +00:00
|
|
|
content_type: content_type.map(Into::into),
|
|
|
|
content_disposition: Some(content_disposition),
|
|
|
|
cross_origin_resource_policy: Some(CORP_CROSS_ORIGIN.into()),
|
2024-04-24 18:13:15 -07:00
|
|
|
cache_control: Some(CACHE_CONTROL_IMMUTABLE.into()),
|
2022-01-22 16:58:32 +01:00
|
|
|
})
|
2024-07-22 07:43:51 +00:00
|
|
|
} else if !services.globals.server_is_ours(&body.server_name) && body.allow_remote {
|
2024-08-15 20:08:53 +00:00
|
|
|
let response = services
|
|
|
|
.media
|
2024-08-16 01:13:20 +00:00
|
|
|
.fetch_remote_content_legacy(&mxc, body.allow_redirect, body.timeout_ms)
|
2024-08-15 20:08:53 +00:00
|
|
|
.await
|
|
|
|
.map_err(|e| err!(Request(NotFound(debug_warn!(%mxc, "Fetching media failed: {e:?}")))))?;
|
|
|
|
|
|
|
|
let content_disposition =
|
|
|
|
make_content_disposition(response.content_disposition.as_ref(), response.content_type.as_deref(), None);
|
|
|
|
|
|
|
|
Ok(get_content_as_filename::v3::Response {
|
|
|
|
content_disposition: Some(content_disposition),
|
|
|
|
content_type: response.content_type,
|
|
|
|
file: response.file,
|
|
|
|
cross_origin_resource_policy: Some(CORP_CROSS_ORIGIN.into()),
|
|
|
|
cache_control: Some(CACHE_CONTROL_IMMUTABLE.into()),
|
|
|
|
})
|
2022-01-27 00:25:20 +01:00
|
|
|
} else {
|
2024-08-01 10:58:27 +00:00
|
|
|
Err!(Request(NotFound("Media not found.")))
|
2022-01-27 00:25:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-08 12:10:29 -05:00
|
|
|
/// # `GET /_matrix/media/v1/download/{serverName}/{mediaId}/{fileName}`
|
|
|
|
///
|
|
|
|
/// Load media from our server or over federation, permitting desired filename.
|
|
|
|
///
|
|
|
|
/// This is a legacy endpoint ("/v1/") that some very old homeservers and/or
|
|
|
|
/// clients may call. conduwuit adds these for compatibility purposes.
|
2024-03-08 12:23:36 -05:00
|
|
|
/// See <https://spec.matrix.org/legacy/legacy/#id27>
|
2024-03-08 12:10:29 -05:00
|
|
|
///
|
|
|
|
/// - Only allows federation if `allow_remote` is true
|
|
|
|
/// - Only redirects if `allow_redirect` is true
|
|
|
|
/// - Uses client-provided `timeout_ms` if available, else defaults to 20
|
|
|
|
/// seconds
|
2024-07-07 14:56:18 -04:00
|
|
|
#[tracing::instrument(skip_all, fields(%client), name = "media_get")]
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn get_content_as_filename_v1_route(
|
2024-07-16 08:05:25 +00:00
|
|
|
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
|
|
|
|
body: Ruma<get_content_as_filename::v3::Request>,
|
2024-03-08 12:10:29 -05:00
|
|
|
) -> Result<RumaResponse<get_content_as_filename::v3::Response>> {
|
2024-07-16 08:05:25 +00:00
|
|
|
get_content_as_filename_route(State(services), InsecureClientIp(client), body)
|
2024-07-07 14:56:18 -04:00
|
|
|
.await
|
|
|
|
.map(RumaResponse)
|
2024-03-08 12:10:29 -05:00
|
|
|
}
|
|
|
|
|
2024-01-17 19:54:17 -05:00
|
|
|
/// # `GET /_matrix/media/v3/thumbnail/{serverName}/{mediaId}`
|
2021-08-31 19:14:37 +02:00
|
|
|
///
|
|
|
|
/// Load media thumbnail from our server or over federation.
|
|
|
|
///
|
|
|
|
/// - Only allows federation if `allow_remote` is true
|
2024-01-21 20:30:39 -05:00
|
|
|
/// - Only redirects if `allow_redirect` is true
|
|
|
|
/// - Uses client-provided `timeout_ms` if available, else defaults to 20
|
|
|
|
/// seconds
|
2024-07-07 14:56:18 -04:00
|
|
|
#[tracing::instrument(skip_all, fields(%client), name = "media_thumbnail_get")]
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn get_content_thumbnail_route(
|
2024-07-16 08:05:25 +00:00
|
|
|
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
|
|
|
|
body: Ruma<get_content_thumbnail::v3::Request>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<get_content_thumbnail::v3::Response> {
|
2024-08-16 01:13:20 +00:00
|
|
|
let mxc = Mxc {
|
|
|
|
server_name: &body.server_name,
|
|
|
|
media_id: &body.media_id,
|
|
|
|
};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-08-27 01:15:31 +00:00
|
|
|
let dim = Dim::from_ruma(body.width, body.height, body.method.clone())?;
|
2020-07-30 18:14:47 +02:00
|
|
|
if let Some(FileMeta {
|
2024-06-24 08:27:06 +00:00
|
|
|
content,
|
2020-07-30 18:14:47 +02:00
|
|
|
content_type,
|
2024-05-07 21:32:06 -04:00
|
|
|
content_disposition,
|
2024-08-27 01:15:31 +00:00
|
|
|
}) = services.media.get_thumbnail(&mxc, &dim).await?
|
2021-06-06 16:58:32 +04:30
|
|
|
{
|
2024-08-12 22:53:07 +00:00
|
|
|
let content_disposition = make_content_disposition(content_disposition.as_ref(), content_type.as_deref(), None);
|
2024-05-07 16:36:22 -04:00
|
|
|
|
2022-10-11 21:51:20 +02:00
|
|
|
Ok(get_content_thumbnail::v3::Response {
|
2024-08-15 20:08:53 +00:00
|
|
|
file: content.expect("entire file contents"),
|
2024-08-12 22:53:07 +00:00
|
|
|
content_type: content_type.map(Into::into),
|
|
|
|
cross_origin_resource_policy: Some(CORP_CROSS_ORIGIN.into()),
|
2024-04-24 18:13:15 -07:00
|
|
|
cache_control: Some(CACHE_CONTROL_IMMUTABLE.into()),
|
2024-08-12 22:53:07 +00:00
|
|
|
content_disposition: Some(content_disposition),
|
2022-10-11 21:51:20 +02:00
|
|
|
})
|
2024-07-22 07:43:51 +00:00
|
|
|
} else if !services.globals.server_is_ours(&body.server_name) && body.allow_remote {
|
2024-08-15 20:08:53 +00:00
|
|
|
let response = services
|
|
|
|
.media
|
2024-08-16 01:13:20 +00:00
|
|
|
.fetch_remote_thumbnail_legacy(&body)
|
2024-04-24 19:05:33 -07:00
|
|
|
.await
|
2024-08-15 20:08:53 +00:00
|
|
|
.map_err(|e| err!(Request(NotFound(debug_warn!(%mxc, "Fetching media failed: {e:?}")))))?;
|
|
|
|
|
|
|
|
let content_disposition =
|
|
|
|
make_content_disposition(response.content_disposition.as_ref(), response.content_type.as_deref(), None);
|
|
|
|
|
|
|
|
Ok(get_content_thumbnail::v3::Response {
|
|
|
|
file: response.file,
|
|
|
|
content_type: response.content_type,
|
|
|
|
cross_origin_resource_policy: Some(CORP_CROSS_ORIGIN.into()),
|
|
|
|
cache_control: Some(CACHE_CONTROL_IMMUTABLE.into()),
|
|
|
|
content_disposition: Some(content_disposition),
|
|
|
|
})
|
2020-07-30 18:14:47 +02:00
|
|
|
} else {
|
2024-08-01 10:58:27 +00:00
|
|
|
Err!(Request(NotFound("Media not found.")))
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
|
|
|
}
|
2024-02-09 23:16:06 -05:00
|
|
|
|
2024-03-08 12:10:29 -05:00
|
|
|
/// # `GET /_matrix/media/v1/thumbnail/{serverName}/{mediaId}`
|
|
|
|
///
|
|
|
|
/// Load media thumbnail from our server or over federation.
|
|
|
|
///
|
|
|
|
/// This is a legacy endpoint ("/v1/") that some very old homeservers and/or
|
|
|
|
/// clients may call. conduwuit adds these for compatibility purposes.
|
2024-03-08 12:23:36 -05:00
|
|
|
/// See <https://spec.matrix.org/legacy/legacy/#id27>
|
2024-03-08 12:10:29 -05:00
|
|
|
///
|
|
|
|
/// - Only allows federation if `allow_remote` is true
|
|
|
|
/// - Only redirects if `allow_redirect` is true
|
|
|
|
/// - Uses client-provided `timeout_ms` if available, else defaults to 20
|
|
|
|
/// seconds
|
2024-07-07 14:56:18 -04:00
|
|
|
#[tracing::instrument(skip_all, fields(%client), name = "media_thumbnail_get")]
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn get_content_thumbnail_v1_route(
|
2024-07-16 08:05:25 +00:00
|
|
|
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
|
|
|
|
body: Ruma<get_content_thumbnail::v3::Request>,
|
2024-03-08 12:10:29 -05:00
|
|
|
) -> Result<RumaResponse<get_content_thumbnail::v3::Response>> {
|
2024-07-16 08:05:25 +00:00
|
|
|
get_content_thumbnail_route(State(services), InsecureClientIp(client), body)
|
2024-07-07 14:56:18 -04:00
|
|
|
.await
|
|
|
|
.map(RumaResponse)
|
2024-04-24 17:15:55 -07:00
|
|
|
}
|