use std::io::{Read, Seek};
use crate::common::detection::FileFormat;
pub fn detect_odf_format_from_mimetype(mimetype: &[u8]) -> Option<FileFormat> {
let mime_str = String::from_utf8_lossy(mimetype).trim().to_string();
match mime_str.as_str() {
"application/vnd.oasis.opendocument.text" => Some(FileFormat::Odt),
"application/vnd.oasis.opendocument.spreadsheet" => Some(FileFormat::Ods),
"application/vnd.oasis.opendocument.presentation" => Some(FileFormat::Odp),
_ => None,
}
}
pub fn detect_odf_format(bytes: &[u8]) -> Option<FileFormat> {
if bytes.len() < 4 || &bytes[0..4] != crate::common::detection::utils::ZIP_SIGNATURE {
return None;
}
let cursor = std::io::Cursor::new(bytes);
detect_odf_format_from_reader(&mut cursor.clone())
}
pub fn detect_odf_format_from_reader<R: Read + Seek>(
reader: &mut R
) -> Option<FileFormat> {
use std::io::Cursor;
let mut buffer = Vec::new();
if reader.read_to_end(&mut buffer).is_err() {
return None;
}
let _ = reader.seek(std::io::SeekFrom::Start(0));
let cursor = Cursor::new(&buffer);
let zip_result = zip::ZipArchive::new(cursor);
if let Ok(mut archive) = zip_result {
if let Ok(mimetype) = crate::common::detection::utils::read_zip_file(&mut archive, "mimetype") {
return detect_odf_format_from_mimetype(&mimetype);
}
}
None
}