Skip to content

Commit aa4d173

Browse files
committed
fix(blobs_to_data_url) :: parse .docx blobs correctly
Word documents always fell back to `applicaiton/zip` as the six-byte windows did not match a five-byte litral.
1 parent 8bdd3c1 commit aa4d173

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
```
1414

1515
SQLPage now keeps the variable value, producing `https://api.example.com/john.doe` as expected.
16+
- Word documents stored as BLOBs are now detected as `.docx`.
1617

1718
## v0.46
1819

src/webserver/database/blob_to_data_url.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn detect_mime_type(bytes: &[u8]) -> &'static str {
3131
// Check for Office document types in ZIP central directory
3232
if bytes.len() >= 50 {
3333
let central_dir = &bytes[30..bytes.len().min(50)];
34-
if central_dir.windows(6).any(|w| w == b"word/") {
34+
if central_dir.windows(5).any(|w| w == b"word/") {
3535
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
3636
}
3737
if central_dir.windows(3).any(|w| w == b"xl/") {
@@ -189,6 +189,34 @@ mod tests {
189189
);
190190
}
191191

192+
fn zip_starting_with(first_entry_name: &[u8]) -> Vec<u8> {
193+
let mut blob = b"PK\x03\x04".to_vec();
194+
blob.resize(30, 0);
195+
blob.extend_from_slice(first_entry_name);
196+
blob.resize(50, 0);
197+
blob
198+
}
199+
200+
#[test]
201+
fn test_detect_office_documents() {
202+
assert_eq!(
203+
detect_mime_type(&zip_starting_with(b"word/document.xml")),
204+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
205+
);
206+
assert_eq!(
207+
detect_mime_type(&zip_starting_with(b"xl/workbook.xml")),
208+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
209+
);
210+
assert_eq!(
211+
detect_mime_type(&zip_starting_with(b"ppt/presentation.xml")),
212+
"application/vnd.openxmlformats-officedocument.presentationml.presentation"
213+
);
214+
assert_eq!(
215+
detect_mime_type(&zip_starting_with(b"other/thing.txt")),
216+
"application/zip"
217+
);
218+
}
219+
192220
#[test]
193221
fn decodes_base64_and_percent_encoded_data_urls() {
194222
assert_eq!(

0 commit comments

Comments
 (0)