From 57b16b89c3d7c345f5474b85dd49f2158ad37a73 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Wed, 29 May 2024 04:17:39 +0100 Subject: [PATCH] chore: only check with tree_magic if inferred_content_type is empty --- rust/pact_matching/src/binary_utils.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/rust/pact_matching/src/binary_utils.rs b/rust/pact_matching/src/binary_utils.rs index 1692c77d..b5caa4fa 100644 --- a/rust/pact_matching/src/binary_utils.rs +++ b/rust/pact_matching/src/binary_utils.rs @@ -39,6 +39,7 @@ where S: Into, { let expected = expected_content_type.into(); + // Use infer crate to detect via magic bytes let inferred_content_type = infer::get(data) .map(|result| result.mime_type()) @@ -49,13 +50,17 @@ where if inferred_match { return Ok(()); } - // Use tree_magic_mini crate to detect via magic bytes using mime-db (requires user to install) - let magic_content_type = tree_magic_mini::from_u8(data); - let magic_match = magic_content_type == expected; - debug!("Matching binary contents by content type: expected '{}', detection method: tree_magic_mini '{}' -> {}", - expected, magic_content_type, magic_match); - if magic_match && magic_content_type != "text/plain" { - return Ok(()); + + let mut magic_content_type = ""; + if inferred_content_type == "" { + // Use tree_magic_mini crate to detect via magic bytes using mime-db (requires user to install) + magic_content_type = tree_magic_mini::from_u8(data); + let magic_match = magic_content_type == expected; + debug!("Matching binary contents by content type: expected '{}', detection method: tree_magic_mini '{}' -> {}", + expected, magic_content_type, magic_match); + if magic_match && magic_content_type != "text/plain" { + return Ok(()); + } } // Where we have detected text/plain, check content type against our own detection from bytes