Skip to content

Commit

Permalink
cli: Add support for webpsan
Browse files Browse the repository at this point in the history
  • Loading branch information
oftheforest committed Oct 26, 2023
1 parent 71119d6 commit 2a1e57c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "mp4san-cli"
name = "mediasan-cli"
edition = "2021"
version = "0.1.0"

Expand All @@ -9,5 +9,6 @@ publish = false
anyhow = "1.0.68"
clap = { version = "4.0.32", features = ["derive"] }
mp4san = { path = "../mp4san" }
webpsan = { path = "../webpsan" }
env_logger = "0.10.0"
log = "0.4.17"
31 changes: 28 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@ use std::fs::File;
use std::path::PathBuf;

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

#[derive(clap::Parser)]
struct Args {
/// The format of the media file.
///
/// If not specified, a guess will be made based on the file extension.
#[clap(long, short = 't')]
format: Option<Format>,

/// Path to the file to test sanitization on.
file: PathBuf,
}

#[derive(Debug, Clone, Copy, ValueEnum)]
enum Format {
Mp4,
Webp,
}

fn main() -> Result<(), anyhow::Error> {
env_logger::builder()
.filter_level(log::LevelFilter::Info)
Expand All @@ -18,9 +31,21 @@ fn main() -> Result<(), anyhow::Error> {

let args = Args::try_parse().context("Error parsing command line arguments")?;

let file = File::open(args.file).context("Error opening mp4 file")?;
let format = match args.format {
Some(t) => t,
None => {
let extension = args.file.extension().unwrap_or_default();
ValueEnum::from_str(&extension.to_string_lossy(), true)
.map_err(|_| anyhow::anyhow!("can't guess media format (unrecognized extension {extension:?})"))?
}
};

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

mp4san::sanitize(file).context("Error parsing mp4 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")?,
}

Ok(())
}

0 comments on commit 2a1e57c

Please sign in to comment.