From bca435557b579f6d1c43a72a35f2fcf87b3979cb Mon Sep 17 00:00:00 2001 From: phiresky Date: Tue, 11 Jun 2019 14:11:08 +0200 Subject: [PATCH] fix --rga-accurate (matching by mime types) --- exampledir/sqlitedb | Bin 0 -> 8192 bytes src/adapters/ffmpeg.rs | 1 + src/bin/rga-preproc.rs | 6 +++--- src/bin/rga.rs | 25 ++++++++++++++----------- src/preproc.rs | 21 +++++++++++++++------ 5 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 exampledir/sqlitedb diff --git a/exampledir/sqlitedb b/exampledir/sqlitedb new file mode 100644 index 0000000000000000000000000000000000000000..efb082b2f92776bbc45b3576860910be50d4a67b GIT binary patch literal 8192 zcmeI#u?oU45C-78L<9$Ep+nbzg9_rJ;A)j_E^1#Og_H`FK&goMg1&(d;4`>;9Xh+X z2>;Jrl7md&cE|nh)HqZ4`fMAsppckpZ441mOq0%E&`m3?EGdF+X$AERrrK>7`{{T2R B9uWWl literal 0 HcmV?d00001 diff --git a/src/adapters/ffmpeg.rs b/src/adapters/ffmpeg.rs index efc5cbf..dd0e4d3 100644 --- a/src/adapters/ffmpeg.rs +++ b/src/adapters/ffmpeg.rs @@ -125,6 +125,7 @@ impl FileAdapter for FFmpegAdapter { let stdo = cmd.stdout.as_mut().expect("is piped"); let time_re = Regex::new(r".*\d.*-->.*\d.*").unwrap(); let mut time: String = "".to_owned(); + // rewrite subtitle times so they are prefixed in every line for line in BufReader::new(stdo).lines() { let line = line?; // 09:55.195 --> 09:56.730 diff --git a/src/bin/rga-preproc.rs b/src/bin/rga-preproc.rs index 690ddfc..f1b9356 100644 --- a/src/bin/rga-preproc.rs +++ b/src/bin/rga-preproc.rs @@ -4,7 +4,7 @@ use rga::adapters::*; use rga::preproc::*; use std::fs::File; -use std::io::BufReader; + fn main() -> Fallible<()> { env_logger::init(); let empty: Vec = vec![]; @@ -19,7 +19,7 @@ fn main() -> Fallible<()> { std::env::current_dir()?.join(&filepath) }; - let i = File::open(&path)?; + let mut i = File::open(&path)?; let mut o = std::io::stdout(); let cache = if args.no_cache { None @@ -27,7 +27,7 @@ fn main() -> Fallible<()> { Some(rga::preproc_cache::open()?) }; let ai = AdaptInfo { - inp: &mut BufReader::new(i), + inp: &mut i, filepath_hint: &path, is_real_file: true, oup: &mut o, diff --git a/src/bin/rga.rs b/src/bin/rga.rs index 15c09a1..a9bdaf9 100644 --- a/src/bin/rga.rs +++ b/src/bin/rga.rs @@ -4,10 +4,8 @@ use rga::adapters::spawning::map_exe_error; use rga::adapters::*; use rga::args::*; - use std::process::Command; - fn main() -> Fallible<()> { env_logger::init(); @@ -35,14 +33,19 @@ fn main() -> Fallible<()> { return Ok(()); } - let extensions = adapters - .iter() - .flat_map(|a| &a.metadata().fast_matchers) - .filter_map(|m| match m { - FastMatcher::FileExtension(ext) => Some(ext as &str), - }) - .collect::>() - .join(","); + let pre_glob = if !args.accurate { + let extensions = adapters + .iter() + .flat_map(|a| &a.metadata().fast_matchers) + .filter_map(|m| match m { + FastMatcher::FileExtension(ext) => Some(ext as &str), + }) + .collect::>() + .join(","); + format!("*.{{{}}}", extensions) + } else { + "*".to_owned() + }; let exe = std::env::current_exe().expect("Could not get executable location"); let preproc_exe = exe.with_file_name("rga-preproc"); @@ -51,7 +54,7 @@ fn main() -> Fallible<()> { .arg("--pre") .arg(preproc_exe) .arg("--pre-glob") - .arg(format!("*.{{{}}}", extensions)) + .arg(pre_glob) .args(passthrough_args) .spawn() .map_err(|e| map_exe_error(e, "rg", "Please make sure you have ripgrep installed."))?; diff --git a/src/preproc.rs b/src/preproc.rs index 0af12e1..5fb5880 100644 --- a/src/preproc.rs +++ b/src/preproc.rs @@ -5,6 +5,8 @@ use failure::Fallible; use failure::{format_err, Error}; use path_clean::PathClean; use std::convert::TryInto; +use std::io::BufRead; +use std::io::BufReader; use std::io::BufWriter; use std::sync::{Arc, RwLock}; @@ -43,13 +45,20 @@ pub fn rga_preproc(ai: AdaptInfo) -> Result<(), Error> { eprintln!("path_hint: {:?}", filepath_hint); - /*let mimetype = tree_magic::from_filepath(path).ok_or(lerr(format!( - "File {} does not exist", - filename.to_string_lossy() - )))?; - println!("mimetype: {:?}", mimetype);*/ + // todo: figure out when using a bufreader is a good idea and when it is not + // seems to beed for File::open() reads, but not sure about within archives (tar, zip) + let inp = &mut BufReader::with_capacity(1 << 13, inp); + + let mimetype = if args.accurate { + let buf = inp.fill_buf()?; // fill but do not consume! + let mimetype = tree_magic::from_u8(buf); + eprintln!("mimetype: {:?}", mimetype); + Some(mimetype) + } else { + None + }; let adapter = adapters(FileMeta { - mimetype: None, + mimetype, lossy_filename: filename.to_string_lossy().to_string(), }); match adapter {