Skip to content

Commit

Permalink
mediasan-cli: add output argument
Browse files Browse the repository at this point in the history
  • Loading branch information
jessa0 committed Nov 22, 2024
1 parent 07624b4 commit 0f7220e
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use std::fs;
use std::fs::File;
use std::io;
use std::io::{Read, Seek, Write};
use std::path::PathBuf;

use anyhow::Context;
use clap::{Parser as _, ValueEnum};
use mp4san::SanitizedMetadata;

#[derive(clap::Parser)]
struct Args {
Expand All @@ -12,6 +16,10 @@ struct Args {
#[clap(long, short = 't')]
format: Option<Format>,

/// Path to the file to write sanitized output.
#[clap(long, short = 'o')]
output: Option<PathBuf>,

/// Path to the file to test sanitization on.
file: PathBuf,
}
Expand Down Expand Up @@ -40,12 +48,33 @@ fn main() -> Result<(), anyhow::Error> {
}
};

let file = File::open(args.file).context("Error opening file")?;
let mut infile = File::open(&args.file).context("Error opening file")?;

match format {
Format::Mp4 => mp4san::sanitize(file).map(drop).context("Error parsing mp4 file")?,
Format::Webp => webpsan::sanitize(file).context("Error parsing webp file")?,
}
Format::Mp4 => match mp4san::sanitize(&mut infile).context("Error parsing mp4 file")? {
SanitizedMetadata { metadata: Some(metadata), data } => {
if let Some(output_path) = args.output {
let mut outfile = File::create(output_path).context("Error opening output file")?;
outfile.write(&metadata).context("Error writing output")?;
infile
.seek(io::SeekFrom::Start(data.offset))
.context("Error seeking input")?;
io::copy(&mut infile.take(data.len), &mut outfile).context("Error copying input to output")?;
}
}
SanitizedMetadata { metadata: None, .. } => {
if let Some(output_path) = args.output {
fs::copy(&args.file, output_path).context("Error writing output")?;
}
}
},
Format::Webp => {
webpsan::sanitize(infile).context("Error parsing webp file")?;
if let Some(output_path) = args.output {
fs::copy(args.file, output_path).context("Error writing output")?;
}
}
};

Ok(())
}

0 comments on commit 0f7220e

Please sign in to comment.