mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-17 15:51:23 +01:00
cargo update ruma httparse
This adds authenticated media APIs.
This commit is contained in:
parent
556f2157a2
commit
5a5bea3217
5 changed files with 78 additions and 47 deletions
|
|
@ -6,12 +6,15 @@ use http::{
|
|||
HeaderName, HeaderValue,
|
||||
};
|
||||
use phf::{phf_set, Set};
|
||||
use ruma::api::client::{
|
||||
error::ErrorKind,
|
||||
media::{
|
||||
create_content, get_content, get_content_as_filename,
|
||||
get_content_thumbnail, get_media_config,
|
||||
use ruma::{
|
||||
api::client::{
|
||||
error::ErrorKind,
|
||||
media::{
|
||||
create_content, get_content, get_content_as_filename,
|
||||
get_content_thumbnail, get_media_config,
|
||||
},
|
||||
},
|
||||
http_headers::{ContentDisposition, ContentDispositionType},
|
||||
};
|
||||
use tracing::error;
|
||||
|
||||
|
|
@ -77,16 +80,17 @@ fn content_security_policy() -> HeaderValue {
|
|||
// Doing this correctly is tricky, so I'm skipping it for now.
|
||||
fn content_disposition_for(
|
||||
content_type: Option<&str>,
|
||||
filename: Option<&str>,
|
||||
) -> String {
|
||||
match (
|
||||
content_type.is_some_and(|x| INLINE_CONTENT_TYPES.contains(x)),
|
||||
filename: Option<String>,
|
||||
) -> ContentDisposition {
|
||||
let disposition_type = match content_type {
|
||||
Some(x) if INLINE_CONTENT_TYPES.contains(x) => {
|
||||
ContentDispositionType::Inline
|
||||
}
|
||||
_ => ContentDispositionType::Attachment,
|
||||
};
|
||||
ContentDisposition {
|
||||
disposition_type,
|
||||
filename,
|
||||
) {
|
||||
(true, None) => "inline".to_owned(),
|
||||
(true, Some(x)) => format!("inline; filename={x}"),
|
||||
(false, None) => "attachment".to_owned(),
|
||||
(false, Some(x)) => format!("attachment; filename={x}"),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -114,6 +118,7 @@ fn set_header_or_panic(
|
|||
/// # `GET /_matrix/media/r0/config`
|
||||
///
|
||||
/// Returns max upload size.
|
||||
#[allow(deprecated)] // unauthenticated media
|
||||
pub(crate) async fn get_media_config_route(
|
||||
_body: Ar<get_media_config::v3::Request>,
|
||||
) -> Result<Ra<get_media_config::v3::Response>> {
|
||||
|
|
@ -142,9 +147,12 @@ pub(crate) async fn create_content_route(
|
|||
.create(
|
||||
mxc.clone(),
|
||||
body.filename
|
||||
.as_ref()
|
||||
.map(|filename| format!("inline; filename={filename}"))
|
||||
.as_deref(),
|
||||
.clone()
|
||||
.map(|filename| ContentDisposition {
|
||||
disposition_type: ContentDispositionType::Inline,
|
||||
filename: Some(filename),
|
||||
})
|
||||
.as_ref(),
|
||||
body.content_type.as_deref(),
|
||||
&body.file,
|
||||
)
|
||||
|
|
@ -156,6 +164,7 @@ pub(crate) async fn create_content_route(
|
|||
}))
|
||||
}
|
||||
|
||||
#[allow(deprecated)] // unauthenticated media
|
||||
pub(crate) async fn get_remote_content(
|
||||
mxc: &str,
|
||||
server_name: &ruma::ServerName,
|
||||
|
|
@ -179,7 +188,7 @@ pub(crate) async fn get_remote_content(
|
|||
.media
|
||||
.create(
|
||||
mxc.to_owned(),
|
||||
content_response.content_disposition.as_deref(),
|
||||
content_response.content_disposition.as_ref(),
|
||||
content_response.content_type.as_deref(),
|
||||
&content_response.file,
|
||||
)
|
||||
|
|
@ -198,6 +207,7 @@ pub(crate) async fn get_remote_content(
|
|||
/// Load media from our server or over federation.
|
||||
///
|
||||
/// - Only allows federation if `allow_remote` is true
|
||||
#[allow(deprecated)] // unauthenticated media
|
||||
pub(crate) async fn get_content_route(
|
||||
body: Ar<get_content::v3::Request>,
|
||||
) -> Result<axum::response::Response> {
|
||||
|
|
@ -214,6 +224,7 @@ pub(crate) async fn get_content_route(
|
|||
})
|
||||
}
|
||||
|
||||
#[allow(deprecated)] // unauthenticated media
|
||||
async fn get_content_route_ruma(
|
||||
body: Ar<get_content::v3::Request>,
|
||||
) -> Result<get_content::v3::Response> {
|
||||
|
|
@ -259,6 +270,7 @@ async fn get_content_route_ruma(
|
|||
/// Load media from our server or over federation, permitting desired filename.
|
||||
///
|
||||
/// - Only allows federation if `allow_remote` is true
|
||||
#[allow(deprecated)] // unauthenticated media
|
||||
pub(crate) async fn get_content_as_filename_route(
|
||||
body: Ar<get_content_as_filename::v3::Request>,
|
||||
) -> Result<axum::response::Response> {
|
||||
|
|
@ -275,6 +287,7 @@ pub(crate) async fn get_content_as_filename_route(
|
|||
})
|
||||
}
|
||||
|
||||
#[allow(deprecated)] // unauthenticated media
|
||||
pub(crate) async fn get_content_as_filename_route_ruma(
|
||||
body: Ar<get_content_as_filename::v3::Request>,
|
||||
) -> Result<get_content_as_filename::v3::Response> {
|
||||
|
|
@ -290,7 +303,7 @@ pub(crate) async fn get_content_as_filename_route_ruma(
|
|||
file,
|
||||
content_disposition: Some(content_disposition_for(
|
||||
content_type.as_deref(),
|
||||
Some(body.filename.as_str()),
|
||||
Some(body.filename.clone()),
|
||||
)),
|
||||
content_type,
|
||||
cross_origin_resource_policy: Some("cross-origin".to_owned()),
|
||||
|
|
@ -305,7 +318,7 @@ pub(crate) async fn get_content_as_filename_route_ruma(
|
|||
Ok(get_content_as_filename::v3::Response {
|
||||
content_disposition: Some(content_disposition_for(
|
||||
remote_content_response.content_type.as_deref(),
|
||||
Some(body.filename.as_str()),
|
||||
Some(body.filename.clone()),
|
||||
)),
|
||||
content_type: remote_content_response.content_type,
|
||||
file: remote_content_response.file,
|
||||
|
|
@ -321,6 +334,7 @@ pub(crate) async fn get_content_as_filename_route_ruma(
|
|||
/// Load media thumbnail from our server or over federation.
|
||||
///
|
||||
/// - Only allows federation if `allow_remote` is true
|
||||
#[allow(deprecated)] // unauthenticated media
|
||||
pub(crate) async fn get_content_thumbnail_route(
|
||||
body: Ar<get_content_thumbnail::v3::Request>,
|
||||
) -> Result<axum::response::Response> {
|
||||
|
|
@ -342,6 +356,7 @@ pub(crate) async fn get_content_thumbnail_route(
|
|||
&mut r,
|
||||
CONTENT_DISPOSITION,
|
||||
content_disposition_for(content_type.as_deref(), None)
|
||||
.to_string()
|
||||
.try_into()
|
||||
.expect("generated header value should be valid"),
|
||||
);
|
||||
|
|
@ -350,6 +365,7 @@ pub(crate) async fn get_content_thumbnail_route(
|
|||
})
|
||||
}
|
||||
|
||||
#[allow(deprecated)] // unauthenticated media
|
||||
async fn get_content_thumbnail_route_ruma(
|
||||
body: Ar<get_content_thumbnail::v3::Request>,
|
||||
) -> Result<get_content_thumbnail::v3::Response> {
|
||||
|
|
@ -393,6 +409,7 @@ async fn get_content_thumbnail_route_ruma(
|
|||
media_id: body.media_id.clone(),
|
||||
timeout_ms: Duration::from_secs(20),
|
||||
allow_redirect: false,
|
||||
animated: Some(false),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
|
|
|||
|
|
@ -212,7 +212,7 @@ async fn ar_from_request_inner(
|
|||
|
||||
let origin_signatures = BTreeMap::from_iter([(
|
||||
x_matrix.key.to_string(),
|
||||
CanonicalJsonValue::String(x_matrix.sig),
|
||||
CanonicalJsonValue::String(x_matrix.sig.to_string()),
|
||||
)]);
|
||||
|
||||
let signatures = BTreeMap::from_iter([(
|
||||
|
|
|
|||
|
|
@ -244,11 +244,12 @@ where
|
|||
.unwrap();
|
||||
|
||||
let key_id = OwnedSigningKeyId::try_from(key_id.clone()).unwrap();
|
||||
let signature = signature.as_str().unwrap().to_owned();
|
||||
let signature = Base64::parse(signature.as_str().unwrap())
|
||||
.expect("generated signature should be valid base64");
|
||||
|
||||
http_request.headers_mut().typed_insert(Authorization(XMatrix::new(
|
||||
services().globals.server_name().to_owned(),
|
||||
Some(destination.to_owned()),
|
||||
destination.to_owned(),
|
||||
key_id,
|
||||
signature,
|
||||
)));
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
use std::io::Cursor;
|
||||
|
||||
use image::imageops::FilterType;
|
||||
use ruma::http_headers::ContentDisposition;
|
||||
use tokio::{
|
||||
fs::File,
|
||||
io::{AsyncReadExt, AsyncWriteExt},
|
||||
|
|
@ -35,7 +36,7 @@ impl Service {
|
|||
pub(crate) async fn create(
|
||||
&self,
|
||||
mxc: String,
|
||||
content_disposition: Option<&str>,
|
||||
content_disposition: Option<&ContentDisposition>,
|
||||
content_type: Option<&str>,
|
||||
file: &[u8],
|
||||
) -> Result<()> {
|
||||
|
|
@ -44,7 +45,7 @@ impl Service {
|
|||
mxc,
|
||||
0,
|
||||
0,
|
||||
content_disposition,
|
||||
content_disposition.map(ContentDisposition::to_string).as_deref(),
|
||||
content_type,
|
||||
)?;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue