2024-06-25 09:39:06 +01:00
|
|
|
// Unauthenticated media is deprecated
|
|
|
|
#![allow(deprecated)]
|
|
|
|
|
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};
|
2024-08-24 10:27:03 +01:00
|
|
|
use http::header::{CONTENT_DISPOSITION, CONTENT_TYPE};
|
2024-07-15 13:35:37 +02:00
|
|
|
use ruma::{
|
2024-08-24 10:27:03 +01:00
|
|
|
api::{
|
|
|
|
client::{
|
|
|
|
authenticated_media::{
|
|
|
|
get_content, get_content_as_filename, get_content_thumbnail, get_media_config,
|
|
|
|
},
|
|
|
|
error::ErrorKind,
|
|
|
|
media::{self, create_content},
|
2024-07-15 13:35:37 +02:00
|
|
|
},
|
2024-08-24 10:27:03 +01:00
|
|
|
federation::authenticated_media::{self as federation_media, FileOrLocation},
|
2022-01-27 00:25:20 +01:00
|
|
|
},
|
2024-07-15 13:35:37 +02:00
|
|
|
http_headers::{ContentDisposition, ContentDispositionType},
|
2024-08-24 10:27:03 +01:00
|
|
|
media::Method,
|
|
|
|
ServerName, UInt,
|
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(
|
2024-08-24 10:27:03 +01:00
|
|
|
_body: Ruma<media::get_media_config::v3::Request>,
|
|
|
|
) -> Result<media::get_media_config::v3::Response> {
|
|
|
|
Ok(media::get_media_config::v3::Response {
|
|
|
|
upload_size: services().globals.max_request_size().into(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// # `GET /_matrix/client/v1/media/config`
|
|
|
|
///
|
|
|
|
/// Returns max upload size.
|
|
|
|
pub async fn get_media_config_auth_route(
|
|
|
|
_body: Ruma<get_media_config::v1::Request>,
|
|
|
|
) -> Result<get_media_config::v1::Response> {
|
|
|
|
Ok(get_media_config::v1::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-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://{}/{}",
|
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-10-05 20:34:31 +02:00
|
|
|
services()
|
|
|
|
.media
|
2021-06-06 16:58:32 +04:30
|
|
|
.create(
|
|
|
|
mxc.clone(),
|
2024-07-15 13:35:37 +02:00
|
|
|
Some(
|
|
|
|
ContentDisposition::new(ContentDispositionType::Inline)
|
|
|
|
.with_filename(body.filename.clone()),
|
|
|
|
),
|
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 {
|
2024-01-25 20:33:15 -08:00
|
|
|
content_uri: mxc.into(),
|
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,
|
2024-08-24 10:27:03 +01:00
|
|
|
server_name: &ServerName,
|
2022-12-14 13:09:10 +01:00
|
|
|
media_id: String,
|
2024-08-24 10:27:03 +01:00
|
|
|
) -> Result<get_content::v1::Response, Error> {
|
|
|
|
let media::get_content::v3::Response {
|
|
|
|
file,
|
|
|
|
content_type,
|
|
|
|
content_disposition,
|
|
|
|
..
|
|
|
|
} = services()
|
2022-01-27 16:12:39 +01:00
|
|
|
.sending
|
|
|
|
.send_federation_request(
|
|
|
|
server_name,
|
2024-08-24 10:27:03 +01:00
|
|
|
media::get_content::v3::Request {
|
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),
|
2024-08-24 10:27:03 +01:00
|
|
|
allow_remote: false,
|
|
|
|
allow_redirect: true,
|
2022-01-27 16:12:39 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2022-10-05 20:34:31 +02:00
|
|
|
services()
|
|
|
|
.media
|
2022-01-27 16:12:39 +01:00
|
|
|
.create(
|
2022-11-21 09:51:39 +01:00
|
|
|
mxc.to_owned(),
|
2024-08-24 10:27:03 +01:00
|
|
|
content_disposition.clone(),
|
|
|
|
content_type.as_deref(),
|
|
|
|
&file,
|
2022-01-27 16:12:39 +01:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2024-08-24 10:27:03 +01:00
|
|
|
Ok(get_content::v1::Response {
|
|
|
|
file,
|
|
|
|
content_type,
|
|
|
|
content_disposition,
|
|
|
|
})
|
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(
|
2024-08-24 10:27:03 +01:00
|
|
|
body: Ruma<media::get_content::v3::Request>,
|
|
|
|
) -> Result<media::get_content::v3::Response> {
|
|
|
|
let get_content::v1::Response {
|
|
|
|
file,
|
|
|
|
content_disposition,
|
|
|
|
content_type,
|
|
|
|
} = get_content(&body.server_name, body.media_id.clone(), body.allow_remote).await?;
|
|
|
|
|
|
|
|
Ok(media::get_content::v3::Response {
|
|
|
|
file,
|
|
|
|
content_type,
|
|
|
|
content_disposition,
|
|
|
|
cross_origin_resource_policy: Some("cross-origin".to_owned()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// # `GET /_matrix/client/v1/media/download/{serverName}/{mediaId}`
|
|
|
|
///
|
|
|
|
/// Load media from our server or over federation.
|
|
|
|
pub async fn get_content_auth_route(
|
|
|
|
body: Ruma<get_content::v1::Request>,
|
|
|
|
) -> Result<get_content::v1::Response> {
|
|
|
|
get_content(&body.server_name, body.media_id.clone(), true).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_content(
|
|
|
|
server_name: &ServerName,
|
|
|
|
media_id: String,
|
|
|
|
allow_remote: bool,
|
|
|
|
) -> Result<get_content::v1::Response, Error> {
|
|
|
|
let mxc = format!("mxc://{}/{}", server_name, 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,
|
2024-06-03 21:35:20 +01:00
|
|
|
content_type,
|
2020-07-30 18:14:47 +02:00
|
|
|
file,
|
2022-09-07 13:25:51 +02:00
|
|
|
}) = services().media.get(mxc.clone()).await?
|
2020-07-30 18:14:47 +02:00
|
|
|
{
|
2024-08-24 10:27:03 +01:00
|
|
|
Ok(get_content::v1::Response {
|
2020-07-30 18:14:47 +02:00
|
|
|
file,
|
2024-06-03 21:35:20 +01:00
|
|
|
content_type,
|
2024-07-15 13:35:37 +02:00
|
|
|
content_disposition: Some(content_disposition),
|
2022-01-22 16:58:32 +01:00
|
|
|
})
|
2024-08-24 10:27:03 +01:00
|
|
|
} else if server_name != services().globals.server_name() && allow_remote {
|
2022-01-27 17:08:04 +01:00
|
|
|
let remote_content_response =
|
2024-08-24 10:27:03 +01:00
|
|
|
get_remote_content(&mxc, server_name, media_id.clone()).await?;
|
2024-05-05 09:35:49 +02:00
|
|
|
|
2024-08-24 10:27:03 +01:00
|
|
|
Ok(get_content::v1::Response {
|
2024-05-05 09:35:49 +02:00
|
|
|
content_disposition: remote_content_response.content_disposition,
|
2024-06-03 21:35:20 +01:00
|
|
|
content_type: remote_content_response.content_type,
|
2024-05-05 09:35:49 +02:00
|
|
|
file: remote_content_response.file,
|
|
|
|
})
|
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(
|
2024-08-24 10:27:03 +01:00
|
|
|
body: Ruma<media::get_content_as_filename::v3::Request>,
|
|
|
|
) -> Result<media::get_content_as_filename::v3::Response> {
|
|
|
|
let get_content_as_filename::v1::Response {
|
|
|
|
file,
|
|
|
|
content_type,
|
|
|
|
content_disposition,
|
|
|
|
} = get_content_as_filename(
|
|
|
|
&body.server_name,
|
|
|
|
body.media_id.clone(),
|
|
|
|
body.filename.clone(),
|
|
|
|
body.allow_remote,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(media::get_content_as_filename::v3::Response {
|
|
|
|
file,
|
|
|
|
content_type,
|
|
|
|
content_disposition,
|
|
|
|
cross_origin_resource_policy: Some("cross-origin".to_owned()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// # `GET /_matrix/client/v1/media/download/{serverName}/{mediaId}/{fileName}`
|
|
|
|
///
|
|
|
|
/// Load media from our server or over federation, permitting desired filename.
|
|
|
|
pub async fn get_content_as_filename_auth_route(
|
|
|
|
body: Ruma<get_content_as_filename::v1::Request>,
|
|
|
|
) -> Result<get_content_as_filename::v1::Response, Error> {
|
|
|
|
get_content_as_filename(
|
|
|
|
&body.server_name,
|
|
|
|
body.media_id.clone(),
|
|
|
|
body.filename.clone(),
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_content_as_filename(
|
|
|
|
server_name: &ServerName,
|
|
|
|
media_id: String,
|
|
|
|
filename: String,
|
|
|
|
allow_remote: bool,
|
|
|
|
) -> Result<get_content_as_filename::v1::Response, Error> {
|
|
|
|
let mxc = format!("mxc://{}/{}", server_name, media_id);
|
2022-01-27 00:25:20 +01:00
|
|
|
|
2024-06-03 21:35:20 +01:00
|
|
|
if let Some(FileMeta {
|
|
|
|
file, content_type, ..
|
|
|
|
}) = services().media.get(mxc.clone()).await?
|
|
|
|
{
|
2024-08-24 10:27:03 +01:00
|
|
|
Ok(get_content_as_filename::v1::Response {
|
2022-01-27 00:25:20 +01:00
|
|
|
file,
|
2024-06-03 21:35:20 +01:00
|
|
|
content_type,
|
2024-07-15 13:35:37 +02:00
|
|
|
content_disposition: Some(
|
|
|
|
ContentDisposition::new(ContentDispositionType::Inline)
|
2024-08-24 10:27:03 +01:00
|
|
|
.with_filename(Some(filename.clone())),
|
2024-07-15 13:35:37 +02:00
|
|
|
),
|
2022-01-22 16:58:32 +01:00
|
|
|
})
|
2024-08-24 10:27:03 +01:00
|
|
|
} else if server_name != services().globals.server_name() && allow_remote {
|
2022-01-27 17:08:04 +01:00
|
|
|
let remote_content_response =
|
2024-08-24 10:27:03 +01:00
|
|
|
get_remote_content(&mxc, server_name, media_id.clone()).await?;
|
2022-01-27 16:12:39 +01:00
|
|
|
|
2024-08-24 10:27:03 +01:00
|
|
|
Ok(get_content_as_filename::v1::Response {
|
2024-07-15 13:35:37 +02:00
|
|
|
content_disposition: Some(
|
|
|
|
ContentDisposition::new(ContentDispositionType::Inline)
|
2024-08-24 10:27:03 +01:00
|
|
|
.with_filename(Some(filename.clone())),
|
2024-07-15 13:35:37 +02:00
|
|
|
),
|
2024-06-03 21:35:20 +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(
|
2024-08-24 10:27:03 +01:00
|
|
|
body: Ruma<media::get_content_thumbnail::v3::Request>,
|
|
|
|
) -> Result<media::get_content_thumbnail::v3::Response> {
|
|
|
|
let get_content_thumbnail::v1::Response { file, content_type } = get_content_thumbnail(
|
|
|
|
&body.server_name,
|
|
|
|
body.media_id.clone(),
|
|
|
|
body.height,
|
|
|
|
body.width,
|
|
|
|
body.method.clone(),
|
|
|
|
body.animated,
|
|
|
|
body.allow_remote,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(media::get_content_thumbnail::v3::Response {
|
|
|
|
file,
|
|
|
|
content_type,
|
|
|
|
cross_origin_resource_policy: Some("cross-origin".to_owned()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// # `GET /_matrix/client/v1/media/thumbnail/{serverName}/{mediaId}`
|
|
|
|
///
|
|
|
|
/// Load media thumbnail from our server or over federation.
|
|
|
|
pub async fn get_content_thumbnail_auth_route(
|
|
|
|
body: Ruma<get_content_thumbnail::v1::Request>,
|
|
|
|
) -> Result<get_content_thumbnail::v1::Response> {
|
|
|
|
get_content_thumbnail(
|
|
|
|
&body.server_name,
|
|
|
|
body.media_id.clone(),
|
|
|
|
body.height,
|
|
|
|
body.width,
|
|
|
|
body.method.clone(),
|
|
|
|
body.animated,
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_content_thumbnail(
|
|
|
|
server_name: &ServerName,
|
|
|
|
media_id: String,
|
|
|
|
height: UInt,
|
|
|
|
width: UInt,
|
|
|
|
method: Option<Method>,
|
|
|
|
animated: Option<bool>,
|
|
|
|
allow_remote: bool,
|
|
|
|
) -> Result<get_content_thumbnail::v1::Response, Error> {
|
|
|
|
let mxc = format!("mxc://{}/{}", server_name, media_id);
|
2020-09-15 08:16:20 +02:00
|
|
|
|
2020-07-30 18:14:47 +02:00
|
|
|
if let Some(FileMeta {
|
2024-05-05 09:35:49 +02:00
|
|
|
file, content_type, ..
|
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(),
|
2024-08-24 10:27:03 +01:00
|
|
|
width
|
2021-06-06 16:58:32 +04:30
|
|
|
.try_into()
|
|
|
|
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?,
|
2024-08-24 10:27:03 +01:00
|
|
|
height
|
2021-06-06 16:58:32 +04:30
|
|
|
.try_into()
|
|
|
|
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?,
|
|
|
|
)
|
|
|
|
.await?
|
|
|
|
{
|
2024-08-24 10:27:03 +01:00
|
|
|
Ok(get_content_thumbnail::v1::Response { file, content_type })
|
|
|
|
} else if server_name != services().globals.server_name() && allow_remote {
|
|
|
|
let media::get_content_thumbnail::v3::Response {
|
|
|
|
file, content_type, ..
|
|
|
|
} = services()
|
2020-12-19 16:00:11 +01:00
|
|
|
.sending
|
|
|
|
.send_federation_request(
|
2024-08-24 10:27:03 +01:00
|
|
|
server_name,
|
|
|
|
media::get_content_thumbnail::v3::Request {
|
|
|
|
height,
|
|
|
|
width,
|
|
|
|
method: method.clone(),
|
|
|
|
server_name: server_name.to_owned(),
|
|
|
|
media_id: media_id.clone(),
|
2023-06-25 19:31:40 +02:00
|
|
|
timeout_ms: Duration::from_secs(20),
|
|
|
|
allow_redirect: false,
|
2024-08-24 10:27:03 +01:00
|
|
|
animated,
|
|
|
|
allow_remote: 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,
|
2024-08-24 10:27:03 +01:00
|
|
|
content_type.as_deref(),
|
|
|
|
width.try_into().expect("all UInts are valid u32s"),
|
|
|
|
height.try_into().expect("all UInts are valid u32s"),
|
|
|
|
&file,
|
2021-06-06 16:58:32 +04:30
|
|
|
)
|
|
|
|
.await?;
|
2020-09-14 14:20:38 +02:00
|
|
|
|
2024-08-24 10:27:03 +01:00
|
|
|
Ok(get_content_thumbnail::v1::Response { file, content_type })
|
2020-07-30 18:14:47 +02:00
|
|
|
} else {
|
|
|
|
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
|
|
|
|
}
|
|
|
|
}
|
2024-08-24 10:27:03 +01:00
|
|
|
|
|
|
|
async fn get_location_content(url: String) -> Result<get_content::v1::Response, Error> {
|
|
|
|
let client = services().globals.default_client();
|
|
|
|
let response = client.get(url).send().await?;
|
|
|
|
let headers = response.headers();
|
|
|
|
|
|
|
|
let content_type = headers
|
|
|
|
.get(CONTENT_TYPE)
|
|
|
|
.and_then(|header| header.to_str().ok())
|
|
|
|
.map(ToOwned::to_owned);
|
|
|
|
|
|
|
|
let content_disposition = headers
|
|
|
|
.get(CONTENT_DISPOSITION)
|
|
|
|
.map(|header| header.as_bytes())
|
|
|
|
.map(TryFrom::try_from)
|
|
|
|
.and_then(Result::ok);
|
|
|
|
|
|
|
|
let file = response.bytes().await?.to_vec();
|
|
|
|
|
|
|
|
Ok(get_content::v1::Response {
|
|
|
|
file,
|
|
|
|
content_type,
|
|
|
|
content_disposition,
|
|
|
|
})
|
|
|
|
}
|