2021-07-14 07:07:08 +00:00
|
|
|
use crate::{
|
|
|
|
database::media::FileMeta, database::DatabaseGuard, utils, ConduitResult, Error, Ruma,
|
|
|
|
};
|
2020-07-30 16:14:47 +00:00
|
|
|
use ruma::api::client::{
|
|
|
|
error::ErrorKind,
|
|
|
|
r0::media::{create_content, get_content, get_content_thumbnail, get_media_config},
|
|
|
|
};
|
2021-07-14 07:07:08 +00:00
|
|
|
use std::convert::TryInto;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
use rocket::{get, post};
|
|
|
|
|
2020-09-25 18:18:36 +00:00
|
|
|
const MXC_LENGTH: usize = 32;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2021-08-31 17:14:37 +00:00
|
|
|
/// # `GET /_matrix/media/r0/config`
|
|
|
|
///
|
|
|
|
/// Returns max upload size.
|
2020-07-30 16:14:47 +00:00
|
|
|
#[cfg_attr(feature = "conduit_bin", get("/_matrix/media/r0/config"))]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn get_media_config_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<get_media_config::Response> {
|
|
|
|
Ok(get_media_config::Response {
|
|
|
|
upload_size: db.globals.max_request_size().into(),
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
|
2021-08-31 17:14:37 +00:00
|
|
|
/// # `POST /_matrix/media/r0/upload`
|
|
|
|
///
|
|
|
|
/// Permanently save media in the server.
|
|
|
|
///
|
|
|
|
/// - Some metadata will be saved in the database
|
|
|
|
/// - Media will be saved in the media/ directory
|
2020-07-30 16:14:47 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/media/r0/upload", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn create_content_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<create_content::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<create_content::Response> {
|
|
|
|
let mxc = format!(
|
|
|
|
"mxc://{}/{}",
|
|
|
|
db.globals.server_name(),
|
|
|
|
utils::random_string(MXC_LENGTH)
|
|
|
|
);
|
2021-06-06 12:28:32 +00:00
|
|
|
|
|
|
|
db.media
|
|
|
|
.create(
|
|
|
|
mxc.clone(),
|
|
|
|
&db.globals,
|
|
|
|
&body
|
|
|
|
.filename
|
|
|
|
.as_ref()
|
|
|
|
.map(|filename| "inline; filename=".to_owned() + filename)
|
|
|
|
.as_deref(),
|
|
|
|
&body.content_type.as_deref(),
|
|
|
|
&body.file,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2021-08-02 08:13:34 +00:00
|
|
|
db.flush()?;
|
2020-10-21 19:28:02 +00:00
|
|
|
|
2020-12-19 15:00:11 +00:00
|
|
|
Ok(create_content::Response {
|
2021-04-05 19:25:10 +00:00
|
|
|
content_uri: mxc.try_into().expect("Invalid mxc:// URI"),
|
2020-12-19 15:00:11 +00:00
|
|
|
blurhash: None,
|
|
|
|
}
|
|
|
|
.into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 17:14:37 +00:00
|
|
|
/// # `POST /_matrix/media/r0/download/{serverName}/{mediaId}`
|
|
|
|
///
|
|
|
|
/// Load media from our server or over federation.
|
|
|
|
///
|
|
|
|
/// - Only allows federation if `allow_remote` is true
|
2020-07-30 16:14:47 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
2020-09-15 06:16:20 +00:00
|
|
|
get("/_matrix/media/r0/download/<_>/<_>", data = "<body>")
|
2020-07-30 16:14:47 +00:00
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-09-14 12:20:38 +00:00
|
|
|
pub async fn get_content_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<get_content::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<get_content::Response> {
|
2020-09-15 06:16:20 +00:00
|
|
|
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
|
2020-09-14 12:20:38 +00:00
|
|
|
|
2020-07-30 16:14:47 +00:00
|
|
|
if let Some(FileMeta {
|
2021-05-30 19:55:43 +00:00
|
|
|
content_disposition,
|
2020-07-30 16:14:47 +00:00
|
|
|
content_type,
|
|
|
|
file,
|
2021-06-04 03:36:12 +00:00
|
|
|
}) = db.media.get(&db.globals, &mxc).await?
|
2020-07-30 16:14:47 +00:00
|
|
|
{
|
|
|
|
Ok(get_content::Response {
|
|
|
|
file,
|
2020-11-18 13:36:12 +00:00
|
|
|
content_type,
|
2021-05-30 19:55:43 +00:00
|
|
|
content_disposition,
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
.into())
|
2020-09-15 06:16:20 +00:00
|
|
|
} else if &*body.server_name != db.globals.server_name() && body.allow_remote {
|
2020-12-19 15:00:11 +00:00
|
|
|
let get_content_response = db
|
|
|
|
.sending
|
|
|
|
.send_federation_request(
|
|
|
|
&db.globals,
|
2021-01-14 19:39:56 +00:00
|
|
|
&body.server_name,
|
2020-12-19 15:00:11 +00:00
|
|
|
get_content::Request {
|
|
|
|
allow_remote: false,
|
|
|
|
server_name: &body.server_name,
|
|
|
|
media_id: &body.media_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
2020-09-14 12:20:38 +00:00
|
|
|
|
2021-06-06 12:28:32 +00:00
|
|
|
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?;
|
2020-09-14 12:20:38 +00:00
|
|
|
|
|
|
|
Ok(get_content_response.into())
|
2020-07-30 16:14:47 +00:00
|
|
|
} else {
|
|
|
|
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 17:14:37 +00:00
|
|
|
/// # `POST /_matrix/media/r0/thumbnail/{serverName}/{mediaId}`
|
|
|
|
///
|
|
|
|
/// Load media thumbnail from our server or over federation.
|
|
|
|
///
|
|
|
|
/// - Only allows federation if `allow_remote` is true
|
2020-07-30 16:14:47 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
2020-09-15 06:16:20 +00:00
|
|
|
get("/_matrix/media/r0/thumbnail/<_>/<_>", data = "<body>")
|
2020-07-30 16:14:47 +00:00
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-09-14 12:20:38 +00:00
|
|
|
pub async fn get_content_thumbnail_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<get_content_thumbnail::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<get_content_thumbnail::Response> {
|
2020-09-15 06:16:20 +00:00
|
|
|
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
|
|
|
|
|
2020-07-30 16:14:47 +00:00
|
|
|
if let Some(FileMeta {
|
|
|
|
content_type, file, ..
|
2021-06-06 12:28:32 +00:00
|
|
|
}) = db
|
|
|
|
.media
|
|
|
|
.get_thumbnail(
|
|
|
|
mxc.clone(),
|
|
|
|
&db.globals,
|
|
|
|
body.width
|
|
|
|
.try_into()
|
|
|
|
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?,
|
|
|
|
body.height
|
|
|
|
.try_into()
|
|
|
|
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?,
|
|
|
|
)
|
|
|
|
.await?
|
|
|
|
{
|
2020-11-18 13:36:12 +00:00
|
|
|
Ok(get_content_thumbnail::Response { file, content_type }.into())
|
2020-09-15 06:16:20 +00:00
|
|
|
} else if &*body.server_name != db.globals.server_name() && body.allow_remote {
|
2020-12-19 15:00:11 +00:00
|
|
|
let get_thumbnail_response = db
|
|
|
|
.sending
|
|
|
|
.send_federation_request(
|
|
|
|
&db.globals,
|
2021-01-14 19:39:56 +00:00
|
|
|
&body.server_name,
|
2020-12-19 15:00:11 +00:00
|
|
|
get_content_thumbnail::Request {
|
|
|
|
allow_remote: false,
|
|
|
|
height: body.height,
|
|
|
|
width: body.width,
|
2020-12-31 13:40:49 +00:00
|
|
|
method: body.method.clone(),
|
2020-12-19 15:00:11 +00:00
|
|
|
server_name: &body.server_name,
|
|
|
|
media_id: &body.media_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
2020-09-14 12:20:38 +00:00
|
|
|
|
2021-06-06 12:28:32 +00:00
|
|
|
db.media
|
|
|
|
.upload_thumbnail(
|
|
|
|
mxc,
|
|
|
|
&db.globals,
|
|
|
|
&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,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-09-14 12:20:38 +00:00
|
|
|
|
|
|
|
Ok(get_thumbnail_response.into())
|
2020-07-30 16:14:47 +00:00
|
|
|
} else {
|
|
|
|
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
|
|
|
|
}
|
|
|
|
}
|