pub mod jpeg;
pub mod png;
use jpeg::JpegScrubber;
use png::PngScrubber;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ScrubError {
#[error("Unsupported file type: {0}")]
UnsupportedFileType(String),
#[error("File parsing failed: {0}")]
ParsingError(String),
#[error("I/O error: {0}")]
IoError(#[from] std::io::Error),
#[error("An unknown error occurred")]
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MetadataEntry {
pub key: String,
pub value: String,
pub category: String, }
#[derive(Debug)]
pub struct ScrubResult {
pub cleaned_file_bytes: Vec<u8>,
pub metadata_removed: Vec<MetadataEntry>,
}
pub trait Scrubber {
fn new(file_bytes: Vec<u8>) -> Result<Self, ScrubError>
where
Self: Sized;
fn view_metadata(&self) -> Result<Vec<MetadataEntry>, ScrubError>;
fn scrub(&self) -> Result<ScrubResult, ScrubError>;
}
pub fn scrubber_for_file(file_bytes: Vec<u8>) -> Result<Box<dyn Scrubber>, ScrubError> {
if file_bytes.len() > 8 && file_bytes[0..8] == [137, 80, 78, 71, 13, 10, 26, 10] {
let scrubber = PngScrubber::new(file_bytes)?;
return Ok(Box::new(scrubber));
}
if file_bytes.len() > 2 && file_bytes[0..2] == [0xFF, 0xD8] {
let scrubber = JpegScrubber::new(file_bytes)?;
return Ok(Box::new(scrubber));
}
Err(ScrubError::UnsupportedFileType(
"Could not determine file type.".to_string(),
))
}