From b781771a9b42b5a3260a2298b6ec59f96cc23269 Mon Sep 17 00:00:00 2001 From: strawberry Date: Mon, 3 Jun 2024 23:14:31 -0400 Subject: [PATCH] media: drop Content-Type detection support Signed-off-by: strawberry --- Cargo.lock | 7 ------- Cargo.toml | 4 ---- src/api/client_server/media.rs | 11 +++-------- src/core/Cargo.toml | 1 - src/core/utils/content_disposition.rs | 26 +++++++++++--------------- 5 files changed, 14 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 78badeb9..b2b00be5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -666,7 +666,6 @@ dependencies = [ "http 1.1.0", "http-body-util", "image", - "infer", "ipaddress", "itertools 0.13.0", "libloading", @@ -1685,12 +1684,6 @@ dependencies = [ "serde", ] -[[package]] -name = "infer" -version = "0.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc150e5ce2330295b8616ce0e3f53250e53af31759a9dbedad1621ba29151847" - [[package]] name = "inlinable_string" version = "0.1.15" diff --git a/Cargo.toml b/Cargo.toml index 22df188d..254b6513 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,10 +28,6 @@ name = "conduit" [workspace.dependencies.sanitize-filename] version = "0.5.0" -[workspace.dependencies.infer] -version = "0.16" -default-features = false - [workspace.dependencies.jsonwebtoken] version = "9.3.0" diff --git a/src/api/client_server/media.rs b/src/api/client_server/media.rs index 84a51f33..f457edfa 100644 --- a/src/api/client_server/media.rs +++ b/src/api/client_server/media.rs @@ -139,7 +139,7 @@ pub(crate) async fn create_content_route( .map(|filename| { format!( "{}; filename={}", - content_disposition_type(&body.file, &body.content_type), + content_disposition_type(&body.content_type), sanitise_filename(filename.to_owned()) ) }) @@ -188,7 +188,7 @@ pub(crate) async fn get_content_route(body: Ruma) -> R content_disposition, }) = services().media.get(mxc.clone()).await? { - let content_disposition = Some(make_content_disposition(&file, &content_type, content_disposition, None)); + let content_disposition = Some(make_content_disposition(&content_type, content_disposition, None)); Ok(get_content::v3::Response { file, @@ -212,7 +212,6 @@ pub(crate) async fn get_content_route(body: Ruma) -> R })?; let content_disposition = Some(make_content_disposition( - &response.file, &response.content_type, response.content_disposition, None, @@ -268,7 +267,6 @@ pub(crate) async fn get_content_as_filename_route( }) = services().media.get(mxc.clone()).await? { let content_disposition = Some(make_content_disposition( - &file, &content_type, content_disposition, Some(body.filename.clone()), @@ -293,7 +291,6 @@ pub(crate) async fn get_content_as_filename_route( { Ok(remote_content_response) => { let content_disposition = Some(make_content_disposition( - &remote_content_response.file, &remote_content_response.content_type, remote_content_response.content_disposition, None, @@ -365,7 +362,7 @@ pub(crate) async fn get_content_thumbnail_route( ) .await? { - let content_disposition = Some(make_content_disposition(&file, &content_type, content_disposition, None)); + let content_disposition = Some(make_content_disposition(&content_type, content_disposition, None)); Ok(get_content_thumbnail::v3::Response { file, @@ -418,7 +415,6 @@ pub(crate) async fn get_content_thumbnail_route( .await?; let content_disposition = Some(make_content_disposition( - &get_thumbnail_response.file, &get_thumbnail_response.content_type, get_thumbnail_response.content_disposition, None, @@ -489,7 +485,6 @@ async fn get_remote_content( .await?; let content_disposition = Some(make_content_disposition( - &content_response.file, &content_response.content_type, content_response.content_disposition, None, diff --git a/src/core/Cargo.toml b/src/core/Cargo.toml index e725966f..f2ccf43e 100644 --- a/src/core/Cargo.toml +++ b/src/core/Cargo.toml @@ -69,7 +69,6 @@ figment.workspace = true http-body-util.workspace = true http.workspace = true image.workspace = true -infer.workspace = true ipaddress.workspace = true itertools.workspace = true libloading.workspace = true diff --git a/src/core/utils/content_disposition.rs b/src/core/utils/content_disposition.rs index 90a85c25..c9045b32 100644 --- a/src/core/utils/content_disposition.rs +++ b/src/core/utils/content_disposition.rs @@ -1,5 +1,3 @@ -use crate::debug_info; - const ATTACHMENT: &str = "attachment"; const INLINE: &str = "inline"; @@ -35,19 +33,17 @@ const ALLOWED_INLINE_CONTENT_TYPES: [&str; 26] = [ ]; /// Returns a Content-Disposition of `attachment` or `inline`, depending on the -/// *parsed* contents of the file uploaded via format magic keys using `infer` -/// crate (basically libmagic without needing libmagic). +/// Content-Type against MSC2702 list of safe inline Content-Types +/// (`ALLOWED_INLINE_CONTENT_TYPES`) #[must_use] -#[tracing::instrument(skip(buf))] -pub fn content_disposition_type(buf: &[u8], content_type: &Option) -> &'static str { - let Some(file_type) = infer::get(buf) else { - debug_info!("Failed to infer the file's contents, assuming attachment for Content-Disposition"); +#[tracing::instrument] +pub fn content_disposition_type(content_type: &Option) -> &'static str { + let Some(content_type) = content_type else { + // no Content-Type was given to us, assume attachment return ATTACHMENT; }; - debug_info!("detected MIME type: {}", file_type.mime_type()); - - if ALLOWED_INLINE_CONTENT_TYPES.contains(&file_type.mime_type()) { + if ALLOWED_INLINE_CONTENT_TYPES.contains(&content_type.to_lowercase().as_str()) { INLINE } else { ATTACHMENT @@ -74,9 +70,9 @@ pub fn sanitise_filename(filename: String) -> String { /// `Content-Disposition: attachment/inline; filename=filename.ext` /// /// else: `Content-Disposition: attachment/inline` -#[tracing::instrument(skip(file))] +#[tracing::instrument] pub fn make_content_disposition( - file: &[u8], content_type: &Option, content_disposition: Option, req_filename: Option, + content_type: &Option, content_disposition: Option, req_filename: Option, ) -> String { let filename: String; @@ -98,10 +94,10 @@ pub fn make_content_disposition( if !filename.is_empty() { // Content-Disposition: attachment/inline; filename=filename.ext - format!("{}; filename={}", content_disposition_type(file, content_type), filename) + format!("{}; filename={}", content_disposition_type(content_type), filename) } else { // Content-Disposition: attachment/inline - String::from(content_disposition_type(file, content_type)) + String::from(content_disposition_type(content_type)) } }