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

211 lines
6.5 KiB
Rust
Raw Normal View History

2021-07-14 07:07:08 +00:00
use crate::{
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::{
create_content, get_content, get_content_as_filename, get_content_thumbnail,
get_media_config,
},
2020-07-30 18:14:47 +02: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.
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
}
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
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://{}/{}",
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
services().media
2021-06-06 16:58:32 +04:30
.create(
mxc.clone(),
&body
.filename
.as_ref()
.map(|filename| "inline; filename=".to_owned() + filename)
.as_deref(),
&body.content_type.as_deref(),
&body.file,
)
.await?;
2020-07-30 18:14:47 +02:00
2022-02-18 15:33:14 +01:00
Ok(create_content::v3::Response {
content_uri: mxc.try_into().expect("Invalid mxc:// URI"),
2020-12-19 16:00:11 +01:00
blurhash: None,
})
2020-07-30 18:14:47 +02: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> {
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,
server_name,
2022-01-27 17:08:04 +01:00
media_id,
},
)
.await?;
services().media
.create(
mxc.to_string(),
&content_response.content_disposition.as_deref(),
&content_response.content_type.as_deref(),
&content_response.file,
)
.await?;
Ok(content_response)
}
/// # `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
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> {
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,
}) = services().media.get(&mxc).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,
})
} else if &*body.server_name != services().globals.server_name() && body.allow_remote {
2022-01-27 17:08:04 +01:00
let remote_content_response =
get_remote_content(&mxc, &body.server_name, &body.media_id).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/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> {
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
if let Some(FileMeta {
content_disposition: _,
content_type,
file,
}) = services().media.get(&mxc).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)),
})
} else if &*body.server_name != services().globals.server_name() && body.allow_remote {
2022-01-27 17:08:04 +01:00
let remote_content_response =
get_remote_content(&mxc, &body.server_name, &body.media_id).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,
})
} else {
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
}
}
/// # `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
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> {
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(
&mxc,
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 })
} 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(),
2020-12-19 16:00:11 +01:00
server_name: &body.server_name,
media_id: &body.media_id,
},
)
.await?;
services().media
2021-06-06 16:58:32 +04:30
.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,
)
.await?;
Ok(get_thumbnail_response)
2020-07-30 18:14:47 +02:00
} else {
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
}
}