add legacy v1 routes for the remaining media endpoints

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-03-15 22:40:12 -04:00 committed by June
parent c6cf3589f4
commit 8d8467a4ea
3 changed files with 69 additions and 1 deletions

View file

@ -31,6 +31,22 @@ pub async fn get_media_config_route(
})
}
/// # `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.
pub async fn get_media_config_v1_route(
_body: Ruma<get_media_config::v3::Request>,
) -> Result<RumaResponse<get_media_config::v3::Response>> {
Ok(get_media_config::v3::Response {
upload_size: services().globals.max_request_size().into(),
}
.into())
}
/// # `GET /_matrix/media/v3/preview_url`
///
/// Returns URL preview.
@ -71,6 +87,50 @@ pub async fn get_media_preview_route(
}
}
/// # `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.
pub async fn get_media_preview_v1_route(
body: Ruma<get_media_preview::v3::Request>,
) -> Result<RumaResponse<get_media_preview::v3::Response>> {
let url = &body.url;
if !url_preview_allowed(url) {
return Err(Error::BadRequest(ErrorKind::Forbidden, "URL is not allowed to be previewed"));
}
match get_url_preview(url).await {
Ok(preview) => {
let res = serde_json::value::to_raw_value(&preview).map_err(|e| {
error!("Failed to convert UrlPreviewData into a serde json value: {}", e);
Error::BadRequest(
ErrorKind::LimitExceeded {
retry_after_ms: Some(Duration::from_secs(5)),
},
"Failed to generate a URL preview, try again later.",
)
})?;
Ok(get_media_preview::v3::Response::from_raw_value(res).into())
},
Err(e) => {
warn!("Failed to generate a URL preview: {e}");
// there doesn't seem to be an agreed-upon error code in the spec.
// the only response codes in the preview_url spec page are 200 and 429.
Err(Error::BadRequest(
ErrorKind::LimitExceeded {
retry_after_ms: Some(Duration::from_secs(5)),
},
"Failed to generate a URL preview, try again later.",
))
},
}
}
/// # `POST /_matrix/media/v3/upload`
///
/// Permanently save media in the server.

View file

@ -1112,7 +1112,7 @@ impl KeyValueDatabase {
_ = terminate.recv() => {
debug!(target: "database-cleanup","Received SIGTERM");
}
};
}
#[cfg(not(unix))]
{

View file

@ -668,6 +668,14 @@ fn routes() -> Router {
.ruma_route(client_server::get_media_preview_route)
.ruma_route(client_server::create_content_route)
// legacy v1 media routes
.route(
"/_matrix/media/v1/url_preview",
get(client_server::get_media_preview_v1_route)
)
.route(
"/_matrix/media/v1/config",
get(client_server::get_media_config_v1_route)
)
.route(
"/_matrix/media/v1/upload",
post(client_server::create_content_v1_route)