use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use crate::error::{Error, Result};
pub const SNIFF_LEN: usize = 8 * 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Format {
Txt,
Markdown,
Csv,
Html,
Rtf,
Docx,
Pptx,
Xlsx,
Odt,
Odp,
Ods,
Epub,
Pdf,
}
impl Format {
pub const ALL: [Format; 13] = [
Format::Txt,
Format::Markdown,
Format::Csv,
Format::Html,
Format::Rtf,
Format::Docx,
Format::Pptx,
Format::Xlsx,
Format::Odt,
Format::Odp,
Format::Ods,
Format::Epub,
Format::Pdf,
];
pub fn as_str(self) -> &'static str {
match self {
Format::Txt => "txt",
Format::Markdown => "markdown",
Format::Csv => "csv",
Format::Html => "html",
Format::Rtf => "rtf",
Format::Docx => "docx",
Format::Pptx => "pptx",
Format::Xlsx => "xlsx",
Format::Odt => "odt",
Format::Odp => "odp",
Format::Ods => "ods",
Format::Epub => "epub",
Format::Pdf => "pdf",
}
}
pub fn from_extension(ext: &str) -> Option<Format> {
let ext = ext.to_ascii_lowercase();
Some(match ext.as_str() {
"txt" | "text" | "log" => Format::Txt,
"md" | "markdown" => Format::Markdown,
"csv" => Format::Csv,
"html" | "htm" | "xhtml" => Format::Html,
"rtf" => Format::Rtf,
"docx" => Format::Docx,
"pptx" => Format::Pptx,
"xlsx" => Format::Xlsx,
"odt" => Format::Odt,
"odp" => Format::Odp,
"ods" => Format::Ods,
"epub" => Format::Epub,
"pdf" => Format::Pdf,
_ => return None,
})
}
}
impl std::fmt::Display for Format {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone)]
pub struct Sniff {
pub extension: Option<String>,
pub head: Vec<u8>,
}
impl Sniff {
pub fn new(extension: Option<String>, head: Vec<u8>) -> Self {
Self { extension, head }
}
pub fn read(path: &Path) -> Result<Sniff> {
use std::io::Read;
let extension = path
.extension()
.and_then(|e| e.to_str())
.map(|e| e.to_ascii_lowercase());
let mut file = std::fs::File::open(path).map_err(|e| Error::io(path, e))?;
let mut head = vec![0u8; SNIFF_LEN];
let mut filled = 0;
while filled < head.len() {
match file.read(&mut head[filled..]) {
Ok(0) => break,
Ok(n) => filled += n,
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
Err(e) => return Err(Error::io(path, e)),
}
}
head.truncate(filled);
Ok(Sniff { extension, head })
}
}
pub fn detect_path(path: &Path) -> Result<Format> {
let sniff = Sniff::read(path)?;
detect(&sniff).ok_or_else(|| Error::Unsupported {
path: PathBuf::from(path),
})
}
pub fn detect(sniff: &Sniff) -> Option<Format> {
if let Some(format) = detect_signature(&sniff.head) {
return Some(format);
}
if let Some(format) = sniff.extension.as_deref().and_then(Format::from_extension) {
return Some(format);
}
if looks_like_html(&sniff.head) {
return Some(Format::Html);
}
None
}
pub fn detect_signature(head: &[u8]) -> Option<Format> {
if head.starts_with(b"%PDF-") {
return Some(Format::Pdf);
}
if head.starts_with(b"{\\rtf") {
return Some(Format::Rtf);
}
if head.starts_with(b"PK\x03\x04") {
return detect_zip(head);
}
None
}
fn detect_zip(head: &[u8]) -> Option<Format> {
if let Some(mimetype) = first_entry_mimetype(head) {
let format = match mimetype.as_str() {
"application/epub+zip" => Format::Epub,
"application/vnd.oasis.opendocument.text" => Format::Odt,
"application/vnd.oasis.opendocument.presentation" => Format::Odp,
"application/vnd.oasis.opendocument.spreadsheet" => Format::Ods,
_ => return None,
};
return Some(format);
}
if contains(head, b"word/") {
return Some(Format::Docx);
}
if contains(head, b"ppt/") {
return Some(Format::Pptx);
}
if contains(head, b"xl/") {
return Some(Format::Xlsx);
}
None
}
fn first_entry_mimetype(head: &[u8]) -> Option<String> {
const NAME_LEN_OFFSET: usize = 26;
const NAME_OFFSET: usize = 30;
let name_len =
u16::from_le_bytes([*head.get(NAME_LEN_OFFSET)?, *head.get(NAME_LEN_OFFSET + 1)?]) as usize;
let extra_len = u16::from_le_bytes([
*head.get(NAME_LEN_OFFSET + 2)?,
*head.get(NAME_LEN_OFFSET + 3)?,
]) as usize;
let name = head.get(NAME_OFFSET..NAME_OFFSET + name_len)?;
if name != b"mimetype" {
return None;
}
let start = NAME_OFFSET + name_len + extra_len;
let value = head.get(start..)?;
let end = value
.iter()
.position(
|b| !matches!(b, b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'.' | b'+' | b'-' | b'/'),
)
.unwrap_or(value.len());
let mimetype = std::str::from_utf8(&value[..end]).ok()?;
(!mimetype.is_empty()).then(|| mimetype.to_string())
}
fn looks_like_html(head: &[u8]) -> bool {
let probe: Vec<u8> = head
.iter()
.take(512)
.map(|b| b.to_ascii_lowercase())
.collect();
contains(&probe, b"<!doctype html") || contains(&probe, b"<html")
}
fn contains(haystack: &[u8], needle: &[u8]) -> bool {
haystack
.windows(needle.len())
.any(|window| window == needle)
}
#[cfg(test)]
mod tests {
use super::*;
fn sniff(ext: &str, head: &[u8]) -> Sniff {
Sniff::new(Some(ext.to_string()), head.to_vec())
}
fn zip_entry(name: &[u8], payload: &[u8]) -> Vec<u8> {
let mut bytes = Vec::new();
bytes.extend_from_slice(b"PK\x03\x04");
bytes.extend_from_slice(&[0u8; 22]); bytes.extend_from_slice(&(name.len() as u16).to_le_bytes());
bytes.extend_from_slice(&0u16.to_le_bytes()); bytes.extend_from_slice(name);
bytes.extend_from_slice(payload);
bytes
}
#[test]
fn extension_maps_to_format() {
assert_eq!(Format::from_extension("MD"), Some(Format::Markdown));
assert_eq!(Format::from_extension("htm"), Some(Format::Html));
assert_eq!(Format::from_extension("xlsx"), Some(Format::Xlsx));
assert_eq!(Format::from_extension("exe"), None);
}
#[test]
fn signature_beats_extension() {
let detected = detect(&sniff("txt", b"%PDF-1.7\n%\xe2\xe3\xcf\xd3"));
assert_eq!(detected, Some(Format::Pdf));
}
#[test]
fn text_formats_come_from_the_extension() {
assert_eq!(detect(&sniff("txt", b"hello")), Some(Format::Txt));
assert_eq!(detect(&sniff("csv", b"a,b\n1,2\n")), Some(Format::Csv));
assert_eq!(detect(&sniff("md", b"# Title\n")), Some(Format::Markdown));
}
#[test]
fn rtf_is_detected_by_signature() {
assert_eq!(detect(&sniff("bin", b"{\\rtf1\\ansi")), Some(Format::Rtf));
}
#[test]
fn ooxml_zip_is_classified_by_part_names() {
let docx = zip_entry(b"[Content_Types].xml", b"<Types/>word/document.xml");
assert_eq!(detect(&sniff("bin", &docx)), Some(Format::Docx));
let pptx = zip_entry(b"[Content_Types].xml", b"<Types/>ppt/presentation.xml");
assert_eq!(detect(&sniff("bin", &pptx)), Some(Format::Pptx));
let xlsx = zip_entry(b"[Content_Types].xml", b"<Types/>xl/workbook.xml");
assert_eq!(detect(&sniff("bin", &xlsx)), Some(Format::Xlsx));
}
#[test]
fn odf_and_epub_are_classified_by_mimetype_entry() {
let epub = zip_entry(b"mimetype", b"application/epub+zip");
assert_eq!(detect(&sniff("bin", &epub)), Some(Format::Epub));
let odt = zip_entry(b"mimetype", b"application/vnd.oasis.opendocument.text");
assert_eq!(detect(&sniff("bin", &odt)), Some(Format::Odt));
let ods = zip_entry(
b"mimetype",
b"application/vnd.oasis.opendocument.spreadsheet",
);
assert_eq!(detect(&sniff("bin", &ods)), Some(Format::Ods));
}
#[test]
fn html_is_sniffed_without_an_extension() {
let sniffed = Sniff::new(None, b"<!DOCTYPE HTML><html><body>hi".to_vec());
assert_eq!(detect(&sniffed), Some(Format::Html));
}
#[test]
fn unknown_type_is_not_detected() {
assert_eq!(detect(&sniff("exe", b"MZ\x90\x00")), None);
assert_eq!(detect(&Sniff::new(None, b"\x00\x01\x02".to_vec())), None);
}
}