use crate::{read_bytes, Mime};
use fnv::FnvHashMap;
use std::fs::File;
pub(crate) struct BaseType;
impl crate::Checker for BaseType {
fn match_bytes(&self, bytes: &[u8], mimetype: &str) -> bool {
if mimetype == "application/octet-stream" || mimetype == "all/allfiles" {
return true;
}
if mimetype == "text/plain" {
is_text_plain_from_u8(bytes)
} else {
false
}
}
fn match_file(&self, file: &File, mimetype: &str) -> bool {
let Ok(meta) = file.metadata() else {
return false;
};
match mimetype {
"all/all" => true,
"all/allfiles" | "application/octet-stream" => meta.is_file(),
"inode/directory" => meta.is_dir(),
"text/plain" => is_text_plain_from_file(file),
_ => false,
}
}
fn get_supported(&self) -> Vec<Mime> {
super::init::get_supported()
}
fn get_subclasses(&self) -> Vec<(Mime, Mime)> {
super::init::get_subclasses()
}
fn get_aliaslist(&self) -> FnvHashMap<Mime, Mime> {
super::init::get_aliaslist()
}
}
fn is_text_plain_from_u8(bytes: &[u8]) -> bool {
memchr::memchr(0, bytes).is_none()
}
fn is_text_plain_from_file(file: &File) -> bool {
let Ok(bytes) = read_bytes(file, 512) else {
return false;
};
is_text_plain_from_u8(&bytes)
}