2024-07-16 08:05:25 +00:00
|
|
|
use axum::extract::State;
|
2024-07-07 14:56:18 -04:00
|
|
|
use axum_client_ip::InsecureClientIp;
|
2024-07-22 07:43:51 +00:00
|
|
|
use conduit::{
|
2024-08-27 03:35:00 +00:00
|
|
|
utils::{self, content_disposition::make_content_disposition},
|
|
|
|
Result,
|
2020-07-30 18:14:47 +02:00
|
|
|
};
|
2024-08-27 03:35:00 +00:00
|
|
|
use conduit_service::media::MXC_LENGTH;
|
|
|
|
use ruma::{api::client::media::create_content, Mxc};
|
2024-02-09 23:16:06 -05:00
|
|
|
|
2024-08-27 03:35:00 +00:00
|
|
|
use crate::Ruma;
|
2024-03-15 22:40:12 -04:00
|
|
|
|
2024-01-16 20:44:20 -05: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
|
2024-07-07 14:56:18 -04:00
|
|
|
#[tracing::instrument(skip_all, fields(%client), name = "media_upload")]
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn create_content_route(
|
2024-07-16 08:05:25 +00:00
|
|
|
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
|
|
|
|
body: Ruma<create_content::v3::Request>,
|
2024-04-22 23:48:57 -04:00
|
|
|
) -> Result<create_content::v3::Response> {
|
2024-03-17 01:42:30 -04:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2024-08-12 22:53:07 +00:00
|
|
|
let content_disposition = make_content_disposition(None, body.content_type.as_deref(), body.filename.as_deref());
|
2024-08-16 01:13:20 +00:00
|
|
|
let mxc = Mxc {
|
|
|
|
server_name: services.globals.server_name(),
|
|
|
|
media_id: &utils::random_string(MXC_LENGTH),
|
|
|
|
};
|
2024-08-12 22:53:07 +00:00
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
services
|
2022-10-05 20:34:31 +02:00
|
|
|
.media
|
2021-06-06 16:58:32 +04:30
|
|
|
.create(
|
2024-06-28 23:23:59 +00:00
|
|
|
&mxc,
|
2024-08-16 01:13:20 +00:00
|
|
|
Some(sender_user),
|
2024-08-12 22:53:07 +00:00
|
|
|
Some(&content_disposition),
|
2024-06-03 18:52:02 -04:00
|
|
|
body.content_type.as_deref(),
|
2021-06-06 16:58:32 +04:30
|
|
|
&body.file,
|
|
|
|
)
|
|
|
|
.await?;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(create_content::v3::Response {
|
2024-08-16 01:13:20 +00:00
|
|
|
content_uri: mxc.to_string().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
|
|
|
}
|