Merge branch 'master' into reduce-mxc-length

This commit is contained in:
Timo Kösters 2020-10-14 11:48:25 +02:00
commit b6ed018d16
46 changed files with 3326 additions and 1920 deletions

View file

@ -1,5 +1,7 @@
use super::State;
use crate::{database::media::FileMeta, utils, ConduitResult, Database, Error, Ruma};
use crate::{
database::media::FileMeta, server_server, utils, ConduitResult, Database, Error, Ruma,
};
use ruma::api::client::{
error::ErrorKind,
r0::media::{create_content, get_content, get_content_thumbnail, get_media_config},
@ -27,7 +29,7 @@ pub fn get_media_config_route(
)]
pub fn create_content_route(
db: State<'_, Database>,
body: Ruma<create_content::Request>,
body: Ruma<create_content::Request<'_>>,
) -> ConduitResult<create_content::Response> {
let mxc = format!(
"mxc://{}/{}",
@ -36,7 +38,7 @@ pub fn create_content_route(
);
db.media.create(
mxc.clone(),
body.filename.as_ref(),
&body.filename.as_deref(),
&body.content_type,
&body.file,
)?;
@ -46,24 +48,19 @@ pub fn create_content_route(
#[cfg_attr(
feature = "conduit_bin",
get(
"/_matrix/media/r0/download/<_server_name>/<_media_id>",
data = "<body>"
)
get("/_matrix/media/r0/download/<_>/<_>", data = "<body>")
)]
pub fn get_content_route(
pub async fn get_content_route(
db: State<'_, Database>,
body: Ruma<get_content::Request>,
_server_name: String,
_media_id: String,
body: Ruma<get_content::Request<'_>>,
) -> ConduitResult<get_content::Response> {
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
if let Some(FileMeta {
filename,
content_type,
file,
}) = db
.media
.get(format!("mxc://{}/{}", body.server_name, body.media_id))?
}) = db.media.get(&mxc)?
{
Ok(get_content::Response {
file,
@ -71,6 +68,26 @@ pub fn get_content_route(
content_disposition: filename.unwrap_or_default(), // TODO: Spec says this should be optional
}
.into())
} else if &*body.server_name != db.globals.server_name() && body.allow_remote {
let get_content_response = server_server::send_request(
&db.globals,
body.server_name.clone(),
get_content::Request {
allow_remote: false,
server_name: &body.server_name,
media_id: &body.media_id,
},
)
.await?;
db.media.create(
mxc,
&Some(&get_content_response.content_disposition),
&get_content_response.content_type,
&get_content_response.file,
)?;
Ok(get_content_response.into())
} else {
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
}
@ -78,21 +95,18 @@ pub fn get_content_route(
#[cfg_attr(
feature = "conduit_bin",
get(
"/_matrix/media/r0/thumbnail/<_server_name>/<_media_id>",
data = "<body>"
)
get("/_matrix/media/r0/thumbnail/<_>/<_>", data = "<body>")
)]
pub fn get_content_thumbnail_route(
pub async fn get_content_thumbnail_route(
db: State<'_, Database>,
body: Ruma<get_content_thumbnail::Request>,
_server_name: String,
_media_id: String,
body: Ruma<get_content_thumbnail::Request<'_>>,
) -> ConduitResult<get_content_thumbnail::Response> {
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
if let Some(FileMeta {
content_type, file, ..
}) = db.media.get_thumbnail(
format!("mxc://{}/{}", body.server_name, body.media_id),
mxc.clone(),
body.width
.try_into()
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?,
@ -101,6 +115,31 @@ pub fn get_content_thumbnail_route(
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?,
)? {
Ok(get_content_thumbnail::Response { file, content_type }.into())
} else if &*body.server_name != db.globals.server_name() && body.allow_remote {
let get_thumbnail_response = server_server::send_request(
&db.globals,
body.server_name.clone(),
get_content_thumbnail::Request {
allow_remote: false,
height: body.height,
width: body.width,
method: body.method,
server_name: &body.server_name,
media_id: &body.media_id,
},
)
.await?;
db.media.upload_thumbnail(
mxc,
&None,
&get_thumbnail_response.content_type,
body.width.try_into().expect("all UInts are valid u32s"),
body.height.try_into().expect("all UInts are valid u32s"),
&get_thumbnail_response.file,
)?;
Ok(get_thumbnail_response.into())
} else {
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
}