Skip to content

Commit

Permalink
media: drop Content-Type detection support
Browse files Browse the repository at this point in the history
Signed-off-by: strawberry <[email protected]>
  • Loading branch information
girlbossceo committed Jun 4, 2024
1 parent 308793e commit 696317b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 35 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
11 changes: 3 additions & 8 deletions src/api/client_server/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
)
})
Expand Down Expand Up @@ -188,7 +188,7 @@ pub(crate) async fn get_content_route(body: Ruma<get_content::v3::Request>) -> 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,
Expand All @@ -212,7 +212,6 @@ pub(crate) async fn get_content_route(body: Ruma<get_content::v3::Request>) -> R
})?;

let content_disposition = Some(make_content_disposition(
&response.file,
&response.content_type,
response.content_disposition,
None,
Expand Down Expand Up @@ -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()),
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,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
Expand Down
26 changes: 11 additions & 15 deletions src/core/utils/content_disposition.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::debug_info;

const ATTACHMENT: &str = "attachment";
const INLINE: &str = "inline";

Expand Down Expand Up @@ -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<String>) -> &'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<String>) -> &'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.as_str()) {
INLINE
} else {
ATTACHMENT
Expand All @@ -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<String>, content_disposition: Option<String>, req_filename: Option<String>,
content_type: &Option<String>, content_disposition: Option<String>, req_filename: Option<String>,
) -> String {
let filename: String;

Expand All @@ -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))
}
}

Expand Down

0 comments on commit 696317b

Please sign in to comment.