1
0
Fork 0
mirror of https://forgejo.ellis.link/continuwuation/continuwuity.git synced 2025-07-28 10:48:30 +00:00
continuwuity/src/api/client_server/media.rs

226 lines
7.2 KiB
Rust
Raw Normal View History

2023-06-25 19:31:40 +02:00
use std::time::Duration;
2022-10-05 20:34:31 +02:00
use crate::{service::media::FileMeta, services, utils, Error, Result, Ruma};
2020-07-30 18:14:47 +02:00
use ruma::api::client::{
error::ErrorKind,
2022-02-18 15:33:14 +01:00
media::{
create_content, get_content, get_content_as_filename, get_content_thumbnail,
get_media_config,
},
2020-07-30 18:14:47 +02:00
};
/// generated MXC ID (`media-id`) length
const MXC_LENGTH: usize = 32;
2020-07-30 18:14:47 +02:00
/// # `GET /_matrix/media/v3/config`
2021-08-31 19:14:37 +02:00
///
/// Returns max upload size.
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 {
upload_size: services().globals.max_request_size().into(),
})
2020-07-30 18:14:47 +02:00
}
/// # `POST /_matrix/media/v3/upload`
2021-08-31 19:14:37 +02:00
///
/// Permanently save media in the server.
///
/// - Some metadata will be saved in the database
/// - Media will be saved in the media/ directory
pub async fn create_content_route(
2022-12-14 13:09:10 +01:00
body: Ruma<create_content::v3::Request>,
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://{}/{}",
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-10-05 20:34:31 +02:00
services()
.media
2021-06-06 16:58:32 +04:30
.create(
mxc.clone(),
2022-10-05 20:34:31 +02:00
body.filename
2021-06-06 16:58:32 +04:30
.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
let content_uri = mxc.into();
2022-02-18 15:33:14 +01:00
Ok(create_content::v3::Response {
content_uri,
2020-12-19 16:00:11 +01:00
blurhash: None,
})
2020-07-30 18:14:47 +02:00
}
/// helper method to fetch remote media from other servers over federation
pub async fn get_remote_content(
mxc: &str,
server_name: &ruma::ServerName,
2022-12-14 13:09:10 +01:00
media_id: String,
2022-02-18 15:33:14 +01:00
) -> Result<get_content::v3::Response, Error> {
let content_response = services()
.sending
.send_federation_request(
server_name,
2022-02-18 15:33:14 +01:00
get_content::v3::Request {
allow_remote: false,
2022-12-14 13:09:10 +01:00
server_name: server_name.to_owned(),
2022-01-27 17:08:04 +01:00
media_id,
2023-06-25 19:31:40 +02:00
timeout_ms: Duration::from_secs(20),
allow_redirect: false,
},
)
.await?;
2022-10-05 20:34:31 +02:00
services()
.media
.create(
2022-11-21 09:51:39 +01:00
mxc.to_owned(),
2022-10-05 15:33:57 +02:00
content_response.content_disposition.as_deref(),
content_response.content_type.as_deref(),
&content_response.file,
)
.await?;
Ok(content_response)
}
/// # `GET /_matrix/media/v3/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
pub async fn get_content_route(
2022-12-14 13:09:10 +01:00
body: Ruma<get_content::v3::Request>,
2022-02-18 15:33:14 +01:00
) -> Result<get_content::v3::Response> {
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
2020-07-30 18:14:47 +02:00
if let Some(FileMeta {
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,
content_type,
content_disposition,
cross_origin_resource_policy: Some("cross-origin".to_owned()),
})
} 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-12-14 13:09:10 +01:00
get_remote_content(&mxc, &body.server_name, body.media_id.clone()).await?;
Ok(remote_content_response)
2020-07-30 18:14:47 +02:00
} else {
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
}
}
/// # `GET /_matrix/media/v3/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-12-14 13:09:10 +01:00
body: Ruma<get_content_as_filename::v3::Request>,
2022-02-18 15:33:14 +01:00
) -> Result<get_content_as_filename::v3::Response> {
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
if let Some(FileMeta {
content_type, file, ..
2022-09-07 13:25:51 +02:00
}) = services().media.get(mxc.clone()).await?
{
2022-02-18 15:33:14 +01:00
Ok(get_content_as_filename::v3::Response {
file,
content_type,
content_disposition: Some(format!("inline; filename={}", body.filename)),
cross_origin_resource_policy: Some("cross-origin".to_owned()),
})
} 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-12-14 13:09:10 +01:00
get_remote_content(&mxc, &body.server_name, body.media_id.clone()).await?;
2022-02-18 15:33:14 +01:00
Ok(get_content_as_filename::v3::Response {
content_disposition: Some(format!("inline: filename={}", body.filename)),
content_type: remote_content_response.content_type,
2022-01-27 17:08:04 +01:00
file: remote_content_response.file,
cross_origin_resource_policy: Some("cross-origin".to_owned()),
})
} else {
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
}
}
/// # `GET /_matrix/media/v3/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
pub async fn get_content_thumbnail_route(
2022-12-14 13:09:10 +01:00
body: Ruma<get_content_thumbnail::v3::Request>,
2022-02-18 15:33:14 +01:00
) -> Result<get_content_thumbnail::v3::Response> {
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, ..
}) = 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?
{
Ok(get_content_thumbnail::v3::Response {
file,
content_type,
cross_origin_resource_policy: Some("cross-origin".to_owned()),
})
} 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(
&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,
method: body.method.clone(),
2022-12-14 13:09:10 +01:00
server_name: body.server_name.clone(),
media_id: body.media_id.clone(),
2023-06-25 19:31:40 +02:00
timeout_ms: Duration::from_secs(20),
allow_redirect: false,
2020-12-19 16:00:11 +01:00
},
)
.await?;
2022-10-05 20:34:31 +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?;
Ok(get_thumbnail_response)
2020-07-30 18:14:47 +02:00
} else {
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
}
}