Skip to content

Commit 8a92c54

Browse files
committed
refactor: Simplify MIME type detection logic in blob_to_data_url.rs
- Remove unnecessary empty byte check and streamline conditions for text-based formats - Enhance readability by consolidating checks for XML and JSON formats - Maintain existing functionality while improving code clarity
1 parent 956f94a commit 8a92c54

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

src/webserver/database/blob_to_data_url.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
/// Returns the most appropriate MIME type for common file formats.
33
#[must_use]
44
pub fn detect_mime_type(bytes: &[u8]) -> &'static str {
5-
if bytes.is_empty() {
6-
return "application/octet-stream";
7-
}
8-
95
// PNG: 89 50 4E 47 0D 0A 1A 0A
106
if bytes.starts_with(b"\x89PNG\r\n\x1a\n") {
117
return "image/png";
@@ -48,21 +44,14 @@ pub fn detect_mime_type(bytes: &[u8]) -> &'static str {
4844
return "application/zip";
4945
}
5046

51-
// Text-based formats - check first few bytes for ASCII patterns
52-
if !bytes.is_empty() {
53-
match bytes[0] {
54-
b'<' => {
55-
if bytes.len() >= 4 && bytes.starts_with(b"<svg") {
56-
return "image/svg+xml";
57-
}
58-
if bytes.len() >= 5 && bytes.starts_with(b"<?xml") {
59-
return "application/xml";
60-
}
61-
return "application/xml";
62-
}
63-
b'{' | b'[' => return "application/json",
64-
_ => {}
65-
}
47+
if bytes.starts_with(b"<?xml") {
48+
return "application/xml";
49+
}
50+
if bytes.starts_with(b"<svg") || bytes.starts_with(b"<!DOCTYPE svg") {
51+
return "image/svg+xml";
52+
}
53+
if bytes.starts_with(b"{") || bytes.starts_with(b"[") {
54+
return "application/json";
6655
}
6756

6857
"application/octet-stream"

0 commit comments

Comments
 (0)