1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2024-12-29 09:44:33 +01:00

Merge branch 'media-download-with-filename' into 'next'

Media download with filename

See merge request famedly/conduit!266
This commit is contained in:
Timo Kösters 2022-01-27 15:44:56 +00:00
commit 63309e52f8
2 changed files with 93 additions and 27 deletions

View file

@ -4,7 +4,10 @@ use crate::{
}; };
use ruma::api::client::{ use ruma::api::client::{
error::ErrorKind, error::ErrorKind,
r0::media::{create_content, get_content, get_content_thumbnail, get_media_config}, r0::media::{
create_content, get_content, get_content_as_filename, get_content_thumbnail,
get_media_config,
},
}; };
use std::convert::TryInto; use std::convert::TryInto;
@ -71,7 +74,39 @@ pub async fn create_content_route(
.into()) .into())
} }
/// # `POST /_matrix/media/r0/download/{serverName}/{mediaId}` pub async fn get_remote_content(
db: &DatabaseGuard,
mxc: &str,
server_name: &ruma::ServerName,
media_id: &str
) -> ConduitResult<get_content::Response> {
let content_response = db
.sending
.send_federation_request(
&db.globals,
server_name,
get_content::Request {
allow_remote: false,
server_name,
media_id
},
)
.await?;
db.media
.create(
mxc.to_string(),
&db.globals,
&content_response.content_disposition.as_deref(),
&content_response.content_type.as_deref(),
&content_response.file,
)
.await?;
Ok(content_response.into())
}
/// # `GET /_matrix/media/r0/download/{serverName}/{mediaId}`
/// ///
/// Load media from our server or over federation. /// Load media from our server or over federation.
/// ///
@ -100,36 +135,66 @@ pub async fn get_content_route(
} }
.into()) .into())
} else if &*body.server_name != db.globals.server_name() && body.allow_remote { } else if &*body.server_name != db.globals.server_name() && body.allow_remote {
let get_content_response = db let remote_content_response = get_remote_content(
.sending &db,
.send_federation_request( &mxc,
&db.globals, &body.server_name,
&body.server_name, &body.media_id
get_content::Request { ).await?;
allow_remote: false, Ok(remote_content_response)
server_name: &body.server_name,
media_id: &body.media_id,
},
)
.await?;
db.media
.create(
mxc,
&db.globals,
&get_content_response.content_disposition.as_deref(),
&get_content_response.content_type.as_deref(),
&get_content_response.file,
)
.await?;
Ok(get_content_response.into())
} else { } else {
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found.")) Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
} }
} }
/// # `POST /_matrix/media/r0/thumbnail/{serverName}/{mediaId}` /// # `GET /_matrix/media/r0/download/{serverName}/{mediaId}/{fileName}`
///
/// Load media from our server or over federation, permitting desired filename.
///
/// - Only allows federation if `allow_remote` is true
#[cfg_attr(
feature = "conduit_bin",
get("/_matrix/media/r0/download/<_>/<_>/<_>", data = "<body>")
)]
#[tracing::instrument(skip(db, body))]
pub async fn get_content_as_filename_route(
db: DatabaseGuard,
body: Ruma<get_content_as_filename::Request<'_>>,
) -> ConduitResult<get_content_as_filename::Response> {
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
if let Some(FileMeta {
content_disposition: _,
content_type,
file,
}) = db.media.get(&db.globals, &mxc).await?
{
Ok(get_content_as_filename::Response {
file,
content_type,
content_disposition: Some(format!("inline; filename={}", body.filename)),
}
.into())
} else if &*body.server_name != db.globals.server_name() && body.allow_remote {
let remote_content_response = get_remote_content(
&db,
&mxc,
&body.server_name,
&body.media_id
).await?;
Ok(get_content_as_filename::Response {
content_disposition: Some(format!("inline: filename={}", body.filename)),
content_type: remote_content_response.0.content_type,
file: remote_content_response.0.file
}
.into())
} else {
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
}
}
/// # `GET /_matrix/media/r0/thumbnail/{serverName}/{mediaId}`
/// ///
/// Load media thumbnail from our server or over federation. /// Load media thumbnail from our server or over federation.
/// ///

View file

@ -129,6 +129,7 @@ fn setup_rocket(config: Figment, data: Arc<RwLock<Database>>) -> rocket::Rocket<
client_server::send_event_to_device_route, client_server::send_event_to_device_route,
client_server::get_media_config_route, client_server::get_media_config_route,
client_server::create_content_route, client_server::create_content_route,
client_server::get_content_as_filename_route,
client_server::get_content_route, client_server::get_content_route,
client_server::get_content_thumbnail_route, client_server::get_content_thumbnail_route,
client_server::get_devices_route, client_server::get_devices_route,