2021-07-14 07:07:08 +00:00
|
|
|
use crate::{
|
2022-09-06 23:15:09 +02:00
|
|
|
utils, Error, Result, Ruma, services, service::media::FileMeta,
|
2021-07-14 07:07:08 +00:00
|
|
|
};
|
2020-07-30 18:14:47 +02:00
|
|
|
use ruma::api::client::{
|
|
|
|
error::ErrorKind,
|
2022-02-18 15:33:14 +01:00
|
|
|
media::{
|
2022-01-27 00:25:20 +01:00
|
|
|
create_content, get_content, get_content_as_filename, get_content_thumbnail,
|
|
|
|
get_media_config,
|
|
|
|
},
|
2020-07-30 18:14:47 +02:00
|
|
|
};
|
|
|
|
|
2020-09-25 14:18:36 -04:00
|
|
|
const MXC_LENGTH: usize = 32;
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
/// # `GET /_matrix/media/r0/config`
|
|
|
|
///
|
|
|
|
/// Returns max upload size.
|
2020-10-21 21:28:02 +02:00
|
|
|
pub async fn get_media_config_route(
|
2022-02-18 15:33:14 +01:00
|
|
|
_body: Ruma<get_media_config::v3::Request>,
|
|
|
|
) -> Result<get_media_config::v3::Response> {
|
|
|
|
Ok(get_media_config::v3::Response {
|
2022-09-06 23:15:09 +02:00
|
|
|
upload_size: services().globals.max_request_size().into(),
|
2022-01-22 16:58:32 +01:00
|
|
|
})
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
|
|
|
|
2021-08-31 19:14:37 +02: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-10-21 21:28:02 +02:00
|
|
|
pub async fn create_content_route(
|
2022-04-06 21:31:29 +02:00
|
|
|
body: Ruma<create_content::v3::IncomingRequest>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<create_content::v3::Response> {
|
2020-07-30 18:14:47 +02:00
|
|
|
let mxc = format!(
|
|
|
|
"mxc://{}/{}",
|
2022-09-06 23:15:09 +02:00
|
|
|
services().globals.server_name(),
|
2020-07-30 18:14:47 +02:00
|
|
|
utils::random_string(MXC_LENGTH)
|
|
|
|
);
|
2021-06-06 16:58:32 +04:30
|
|
|
|
2022-09-06 23:15:09 +02:00
|
|
|
services().media
|
2021-06-06 16:58:32 +04:30
|
|
|
.create(
|
|
|
|
mxc.clone(),
|
2022-10-05 15:33:57 +02:00
|
|
|
body
|
2021-06-06 16:58:32 +04:30
|
|
|
.filename
|
|
|
|
.as_ref()
|
|
|
|
.map(|filename| "inline; filename=".to_owned() + filename)
|
|
|
|
.as_deref(),
|
2022-10-05 15:33:57 +02:00
|
|
|
body.content_type.as_deref(),
|
2021-06-06 16:58:32 +04:30
|
|
|
&body.file,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(create_content::v3::Response {
|
2021-04-05 21:25:10 +02:00
|
|
|
content_uri: mxc.try_into().expect("Invalid mxc:// URI"),
|
2020-12-19 16:00:11 +01:00
|
|
|
blurhash: None,
|
2022-01-22 16:58:32 +01:00
|
|
|
})
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
|
|
|
|
2022-01-27 16:12:39 +01:00
|
|
|
pub async fn get_remote_content(
|
|
|
|
mxc: &str,
|
|
|
|
server_name: &ruma::ServerName,
|
2022-01-27 17:08:04 +01:00
|
|
|
media_id: &str,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<get_content::v3::Response, Error> {
|
2022-09-06 23:15:09 +02:00
|
|
|
let content_response = services()
|
2022-01-27 16:12:39 +01:00
|
|
|
.sending
|
|
|
|
.send_federation_request(
|
|
|
|
server_name,
|
2022-02-18 15:33:14 +01:00
|
|
|
get_content::v3::Request {
|
2022-01-27 16:12:39 +01:00
|
|
|
allow_remote: false,
|
|
|
|
server_name,
|
2022-01-27 17:08:04 +01:00
|
|
|
media_id,
|
2022-01-27 16:12:39 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2022-09-06 23:15:09 +02:00
|
|
|
services().media
|
2022-01-27 16:12:39 +01:00
|
|
|
.create(
|
|
|
|
mxc.to_string(),
|
2022-10-05 15:33:57 +02:00
|
|
|
content_response.content_disposition.as_deref(),
|
|
|
|
content_response.content_type.as_deref(),
|
2022-01-27 16:12:39 +01:00
|
|
|
&content_response.file,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2022-01-27 17:00:08 +01:00
|
|
|
Ok(content_response)
|
2022-01-27 16:12:39 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 00:27:13 +01:00
|
|
|
/// # `GET /_matrix/media/r0/download/{serverName}/{mediaId}`
|
2021-08-31 19:14:37 +02:00
|
|
|
///
|
|
|
|
/// Load media from our server or over federation.
|
|
|
|
///
|
|
|
|
/// - Only allows federation if `allow_remote` is true
|
2020-09-14 14:20:38 +02:00
|
|
|
pub async fn get_content_route(
|
2022-04-06 21:31:29 +02:00
|
|
|
body: Ruma<get_content::v3::IncomingRequest>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<get_content::v3::Response> {
|
2020-09-15 08:16:20 +02:00
|
|
|
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
|
2020-09-14 14:20:38 +02:00
|
|
|
|
2020-07-30 18:14:47 +02:00
|
|
|
if let Some(FileMeta {
|
2021-05-30 21:55:43 +02:00
|
|
|
content_disposition,
|
2020-07-30 18:14:47 +02:00
|
|
|
content_type,
|
|
|
|
file,
|
2022-09-07 13:25:51 +02:00
|
|
|
}) = services().media.get(mxc.clone()).await?
|
2020-07-30 18:14:47 +02:00
|
|
|
{
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(get_content::v3::Response {
|
2020-07-30 18:14:47 +02:00
|
|
|
file,
|
2020-11-18 08:36:12 -05:00
|
|
|
content_type,
|
2021-05-30 21:55:43 +02:00
|
|
|
content_disposition,
|
2022-01-22 16:58:32 +01:00
|
|
|
})
|
2022-09-06 23:15:09 +02:00
|
|
|
} else if &*body.server_name != services().globals.server_name() && body.allow_remote {
|
2022-01-27 17:08:04 +01:00
|
|
|
let remote_content_response =
|
2022-09-06 23:15:09 +02:00
|
|
|
get_remote_content(&mxc, &body.server_name, &body.media_id).await?;
|
2022-01-22 16:58:32 +01:00
|
|
|
Ok(remote_content_response)
|
2020-07-30 18:14:47 +02:00
|
|
|
} else {
|
|
|
|
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 00:25:20 +01:00
|
|
|
/// # `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
|
|
|
|
pub async fn get_content_as_filename_route(
|
2022-04-06 21:31:29 +02:00
|
|
|
body: Ruma<get_content_as_filename::v3::IncomingRequest>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<get_content_as_filename::v3::Response> {
|
2022-01-27 00:25:20 +01:00
|
|
|
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
|
|
|
|
|
|
|
|
if let Some(FileMeta {
|
|
|
|
content_disposition: _,
|
|
|
|
content_type,
|
|
|
|
file,
|
2022-09-07 13:25:51 +02:00
|
|
|
}) = services().media.get(mxc.clone()).await?
|
2022-01-27 00:25:20 +01:00
|
|
|
{
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(get_content_as_filename::v3::Response {
|
2022-01-27 00:25:20 +01:00
|
|
|
file,
|
|
|
|
content_type,
|
|
|
|
content_disposition: Some(format!("inline; filename={}", body.filename)),
|
2022-01-22 16:58:32 +01:00
|
|
|
})
|
2022-09-06 23:15:09 +02:00
|
|
|
} else if &*body.server_name != services().globals.server_name() && body.allow_remote {
|
2022-01-27 17:08:04 +01:00
|
|
|
let remote_content_response =
|
2022-09-06 23:15:09 +02:00
|
|
|
get_remote_content(&mxc, &body.server_name, &body.media_id).await?;
|
2022-01-27 16:12:39 +01:00
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(get_content_as_filename::v3::Response {
|
2022-01-27 16:12:39 +01:00
|
|
|
content_disposition: Some(format!("inline: filename={}", body.filename)),
|
2022-01-27 17:00:08 +01:00
|
|
|
content_type: remote_content_response.content_type,
|
2022-01-27 17:08:04 +01:00
|
|
|
file: remote_content_response.file,
|
2022-01-22 16:58:32 +01:00
|
|
|
})
|
2022-01-27 00:25:20 +01:00
|
|
|
} else {
|
|
|
|
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 00:27:13 +01:00
|
|
|
/// # `GET /_matrix/media/r0/thumbnail/{serverName}/{mediaId}`
|
2021-08-31 19:14:37 +02:00
|
|
|
///
|
|
|
|
/// Load media thumbnail from our server or over federation.
|
|
|
|
///
|
|
|
|
/// - Only allows federation if `allow_remote` is true
|
2020-09-14 14:20:38 +02:00
|
|
|
pub async fn get_content_thumbnail_route(
|
2022-04-06 21:31:29 +02:00
|
|
|
body: Ruma<get_content_thumbnail::v3::IncomingRequest>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<get_content_thumbnail::v3::Response> {
|
2020-09-15 08:16:20 +02:00
|
|
|
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
|
|
|
|
|
2020-07-30 18:14:47 +02:00
|
|
|
if let Some(FileMeta {
|
|
|
|
content_type, file, ..
|
2022-09-06 23:15:09 +02:00
|
|
|
}) = services()
|
2021-06-06 16:58:32 +04:30
|
|
|
.media
|
|
|
|
.get_thumbnail(
|
2022-09-07 13:25:51 +02:00
|
|
|
mxc.clone(),
|
2021-06-06 16:58:32 +04:30
|
|
|
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?
|
|
|
|
{
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(get_content_thumbnail::v3::Response { file, content_type })
|
2022-09-06 23:15:09 +02:00
|
|
|
} else if &*body.server_name != services().globals.server_name() && body.allow_remote {
|
|
|
|
let get_thumbnail_response = services()
|
2020-12-19 16:00:11 +01:00
|
|
|
.sending
|
|
|
|
.send_federation_request(
|
2021-01-14 14:39:56 -05:00
|
|
|
&body.server_name,
|
2022-02-18 15:33:14 +01:00
|
|
|
get_content_thumbnail::v3::Request {
|
2020-12-19 16:00:11 +01:00
|
|
|
allow_remote: false,
|
|
|
|
height: body.height,
|
|
|
|
width: body.width,
|
2020-12-31 08:40:49 -05:00
|
|
|
method: body.method.clone(),
|
2020-12-19 16:00:11 +01:00
|
|
|
server_name: &body.server_name,
|
|
|
|
media_id: &body.media_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
2020-09-14 14:20:38 +02:00
|
|
|
|
2022-09-06 23:15:09 +02:00
|
|
|
services().media
|
2021-06-06 16:58:32 +04:30
|
|
|
.upload_thumbnail(
|
|
|
|
mxc,
|
2022-10-05 15:33:57 +02:00
|
|
|
None,
|
|
|
|
get_thumbnail_response.content_type.as_deref(),
|
2021-06-06 16:58:32 +04:30
|
|
|
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 14:20:38 +02:00
|
|
|
|
2022-01-22 16:58:32 +01:00
|
|
|
Ok(get_thumbnail_response)
|
2020-07-30 18:14:47 +02:00
|
|
|
} else {
|
|
|
|
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
|
|
|
|
}
|
|
|
|
}
|