media: put old API behind legacy_media import

This commit is contained in:
Lambda 2024-07-21 17:52:57 +00:00
parent 5a5bea3217
commit 64b3c357dd

View file

@ -9,10 +9,7 @@ use phf::{phf_set, Set};
use ruma::{ use ruma::{
api::client::{ api::client::{
error::ErrorKind, error::ErrorKind,
media::{ media::{self as legacy_media, create_content},
create_content, get_content, get_content_as_filename,
get_content_thumbnail, get_media_config,
},
}, },
http_headers::{ContentDisposition, ContentDispositionType}, http_headers::{ContentDisposition, ContentDispositionType},
}; };
@ -120,9 +117,9 @@ fn set_header_or_panic(
/// Returns max upload size. /// Returns max upload size.
#[allow(deprecated)] // unauthenticated media #[allow(deprecated)] // unauthenticated media
pub(crate) async fn get_media_config_route( pub(crate) async fn get_media_config_route(
_body: Ar<get_media_config::v3::Request>, _body: Ar<legacy_media::get_media_config::v3::Request>,
) -> Result<Ra<get_media_config::v3::Response>> { ) -> Result<Ra<legacy_media::get_media_config::v3::Response>> {
Ok(Ra(get_media_config::v3::Response { Ok(Ra(legacy_media::get_media_config::v3::Response {
upload_size: services().globals.max_request_size().into(), upload_size: services().globals.max_request_size().into(),
})) }))
} }
@ -169,12 +166,12 @@ pub(crate) async fn get_remote_content(
mxc: &str, mxc: &str,
server_name: &ruma::ServerName, server_name: &ruma::ServerName,
media_id: String, media_id: String,
) -> Result<get_content::v3::Response, Error> { ) -> Result<legacy_media::get_content::v3::Response, Error> {
let content_response = services() let content_response = services()
.sending .sending
.send_federation_request( .send_federation_request(
server_name, server_name,
get_content::v3::Request { legacy_media::get_content::v3::Request {
allow_remote: false, allow_remote: false,
server_name: server_name.to_owned(), server_name: server_name.to_owned(),
media_id, media_id,
@ -194,7 +191,7 @@ pub(crate) async fn get_remote_content(
) )
.await?; .await?;
Ok(get_content::v3::Response { Ok(legacy_media::get_content::v3::Response {
file: content_response.file, file: content_response.file,
content_disposition: content_response.content_disposition, content_disposition: content_response.content_disposition,
content_type: content_response.content_type, content_type: content_response.content_type,
@ -209,7 +206,7 @@ pub(crate) async fn get_remote_content(
/// - Only allows federation if `allow_remote` is true /// - Only allows federation if `allow_remote` is true
#[allow(deprecated)] // unauthenticated media #[allow(deprecated)] // unauthenticated media
pub(crate) async fn get_content_route( pub(crate) async fn get_content_route(
body: Ar<get_content::v3::Request>, body: Ar<legacy_media::get_content::v3::Request>,
) -> Result<axum::response::Response> { ) -> Result<axum::response::Response> {
get_content_route_ruma(body).await.map(|x| { get_content_route_ruma(body).await.map(|x| {
let mut r = Ra(x).into_response(); let mut r = Ra(x).into_response();
@ -226,8 +223,8 @@ pub(crate) async fn get_content_route(
#[allow(deprecated)] // unauthenticated media #[allow(deprecated)] // unauthenticated media
async fn get_content_route_ruma( async fn get_content_route_ruma(
body: Ar<get_content::v3::Request>, body: Ar<legacy_media::get_content::v3::Request>,
) -> Result<get_content::v3::Response> { ) -> Result<legacy_media::get_content::v3::Response> {
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id); let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
if let Some(FileMeta { if let Some(FileMeta {
@ -236,7 +233,7 @@ async fn get_content_route_ruma(
.. ..
}) = services().media.get(mxc.clone()).await? }) = services().media.get(mxc.clone()).await?
{ {
Ok(get_content::v3::Response { Ok(legacy_media::get_content::v3::Response {
file, file,
content_disposition: Some(content_disposition_for( content_disposition: Some(content_disposition_for(
content_type.as_deref(), content_type.as_deref(),
@ -251,7 +248,7 @@ async fn get_content_route_ruma(
let remote_content_response = let remote_content_response =
get_remote_content(&mxc, &body.server_name, body.media_id.clone()) get_remote_content(&mxc, &body.server_name, body.media_id.clone())
.await?; .await?;
Ok(get_content::v3::Response { Ok(legacy_media::get_content::v3::Response {
file: remote_content_response.file, file: remote_content_response.file,
content_disposition: Some(content_disposition_for( content_disposition: Some(content_disposition_for(
remote_content_response.content_type.as_deref(), remote_content_response.content_type.as_deref(),
@ -272,7 +269,7 @@ async fn get_content_route_ruma(
/// - Only allows federation if `allow_remote` is true /// - Only allows federation if `allow_remote` is true
#[allow(deprecated)] // unauthenticated media #[allow(deprecated)] // unauthenticated media
pub(crate) async fn get_content_as_filename_route( pub(crate) async fn get_content_as_filename_route(
body: Ar<get_content_as_filename::v3::Request>, body: Ar<legacy_media::get_content_as_filename::v3::Request>,
) -> Result<axum::response::Response> { ) -> Result<axum::response::Response> {
get_content_as_filename_route_ruma(body).await.map(|x| { get_content_as_filename_route_ruma(body).await.map(|x| {
let mut r = Ra(x).into_response(); let mut r = Ra(x).into_response();
@ -289,8 +286,8 @@ pub(crate) async fn get_content_as_filename_route(
#[allow(deprecated)] // unauthenticated media #[allow(deprecated)] // unauthenticated media
pub(crate) async fn get_content_as_filename_route_ruma( pub(crate) async fn get_content_as_filename_route_ruma(
body: Ar<get_content_as_filename::v3::Request>, body: Ar<legacy_media::get_content_as_filename::v3::Request>,
) -> Result<get_content_as_filename::v3::Response> { ) -> Result<legacy_media::get_content_as_filename::v3::Response> {
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id); let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
if let Some(FileMeta { if let Some(FileMeta {
@ -299,7 +296,7 @@ pub(crate) async fn get_content_as_filename_route_ruma(
.. ..
}) = services().media.get(mxc.clone()).await? }) = services().media.get(mxc.clone()).await?
{ {
Ok(get_content_as_filename::v3::Response { Ok(legacy_media::get_content_as_filename::v3::Response {
file, file,
content_disposition: Some(content_disposition_for( content_disposition: Some(content_disposition_for(
content_type.as_deref(), content_type.as_deref(),
@ -315,7 +312,7 @@ pub(crate) async fn get_content_as_filename_route_ruma(
get_remote_content(&mxc, &body.server_name, body.media_id.clone()) get_remote_content(&mxc, &body.server_name, body.media_id.clone())
.await?; .await?;
Ok(get_content_as_filename::v3::Response { Ok(legacy_media::get_content_as_filename::v3::Response {
content_disposition: Some(content_disposition_for( content_disposition: Some(content_disposition_for(
remote_content_response.content_type.as_deref(), remote_content_response.content_type.as_deref(),
Some(body.filename.clone()), Some(body.filename.clone()),
@ -336,7 +333,7 @@ pub(crate) async fn get_content_as_filename_route_ruma(
/// - Only allows federation if `allow_remote` is true /// - Only allows federation if `allow_remote` is true
#[allow(deprecated)] // unauthenticated media #[allow(deprecated)] // unauthenticated media
pub(crate) async fn get_content_thumbnail_route( pub(crate) async fn get_content_thumbnail_route(
body: Ar<get_content_thumbnail::v3::Request>, body: Ar<legacy_media::get_content_thumbnail::v3::Request>,
) -> Result<axum::response::Response> { ) -> Result<axum::response::Response> {
get_content_thumbnail_route_ruma(body).await.map(|x| { get_content_thumbnail_route_ruma(body).await.map(|x| {
let mut r = Ra(x).into_response(); let mut r = Ra(x).into_response();
@ -367,8 +364,8 @@ pub(crate) async fn get_content_thumbnail_route(
#[allow(deprecated)] // unauthenticated media #[allow(deprecated)] // unauthenticated media
async fn get_content_thumbnail_route_ruma( async fn get_content_thumbnail_route_ruma(
body: Ar<get_content_thumbnail::v3::Request>, body: Ar<legacy_media::get_content_thumbnail::v3::Request>,
) -> Result<get_content_thumbnail::v3::Response> { ) -> Result<legacy_media::get_content_thumbnail::v3::Response> {
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id); let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
if let Some(FileMeta { if let Some(FileMeta {
@ -388,7 +385,7 @@ async fn get_content_thumbnail_route_ruma(
) )
.await? .await?
{ {
Ok(get_content_thumbnail::v3::Response { Ok(legacy_media::get_content_thumbnail::v3::Response {
file, file,
content_type, content_type,
cross_origin_resource_policy: Some("cross-origin".to_owned()), cross_origin_resource_policy: Some("cross-origin".to_owned()),
@ -400,7 +397,7 @@ async fn get_content_thumbnail_route_ruma(
.sending .sending
.send_federation_request( .send_federation_request(
&body.server_name, &body.server_name,
get_content_thumbnail::v3::Request { legacy_media::get_content_thumbnail::v3::Request {
allow_remote: false, allow_remote: false,
height: body.height, height: body.height,
width: body.width, width: body.width,
@ -426,7 +423,7 @@ async fn get_content_thumbnail_route_ruma(
) )
.await?; .await?;
Ok(get_content_thumbnail::v3::Response { Ok(legacy_media::get_content_thumbnail::v3::Response {
file: get_thumbnail_response.file, file: get_thumbnail_response.file,
content_type: get_thumbnail_response.content_type, content_type: get_thumbnail_response.content_type,
cross_origin_resource_policy: Some("cross-origin".to_owned()), cross_origin_resource_policy: Some("cross-origin".to_owned()),