use std::path::{Path, PathBuf};
use std::process::Command;
#[cfg(feature = "logging")]
use log::trace;
pub fn pdf_to_cmp_pdf<P>(inpath: P) -> Option<PathBuf>
where
P: AsRef<Path>,
{
let inpath = inpath.as_ref();
let result = if inpath.extension() == Some("pdf".as_ref()) {
Some(inpath.with_extension("cmp.pdf"))
} else {
None
};
#[cfg(feature = "logging")]
trace!("pdf_to_cmp_pdf({:?}) = {:?}", inpath, result);
result
}
pub fn pdf_into_subdir<P, Q>(inpath: P, subdir: Q) -> Option<PathBuf>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
let inpath = inpath.as_ref();
let subdir = subdir.as_ref();
let result = if inpath.extension() == Some("pdf".as_ref()) {
Some(
inpath
.parent()
.unwrap_or("".as_ref())
.join(subdir)
.join(inpath.file_name()?),
)
} else {
None
};
#[cfg(feature = "logging")]
trace!("pdf_into_subdir({:?}, {:?}) = {:?}", inpath, subdir, result);
result
}
pub fn pdf_subdir<P, Q>(inpath: P, subdir: Q) -> Option<PathBuf>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
let inpath = inpath.as_ref();
let subdir = subdir.as_ref();
let result = if inpath.extension()? == "pdf" {
Some(inpath.parent().unwrap_or("".as_ref()).join(subdir))
} else {
None
};
#[cfg(feature = "logging")]
trace!("pdf_subdir({:?}, {:?}) = {:?}", inpath, subdir, result);
result
}
pub fn gs_command<P, Q>(inpath: P, outpath: Q) -> Command
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
#[cfg(feature = "logging")]
trace!("gs_command({:?}, {:?})", inpath.as_ref(), outpath.as_ref());
let mut cmd = Command::new("gs");
cmd.args(
[
"-q",
"-dBATCH",
"-dSAFER",
"-dNOPAUSE",
"-sDEVICE=pdfwrite",
"-dCompatibilityLevel=1.4",
"-dPDFSETTINGS=/ebook",
"-dAutoRotatePages=/None",
"-dColorImageDownsampleType=/Bicubic",
"-dColorImageResolution=135",
"-dGrayImageDownsampleType=/Bicubic",
"-dGrayImageResolution=135",
"-dMonoImageDownsampleType=/Bicubic",
"-dMonoImageResolution=135",
]
.iter(),
)
.arg(format!(
"-sOutputFile={}",
outpath.as_ref().to_string_lossy().to_string()
))
.arg(inpath.as_ref().to_string_lossy().to_string());
cmd
}
pub fn dry_run_command<P, Q>(inpath: P, outpath: Q) -> Command
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
#[cfg(target_os = "windows")]
trace!(
"dry_run_command({:?}, {:?})",
inpath.as_ref(),
outpath.as_ref()
);
let mut cmd = Command::new("args");
cmd.args(
[
"-q",
"-dBATCH",
"-dSAFER",
"-dNOPAUSE",
"-sDEVICE=pdfwrite",
"-dCompatibilityLevel=1.4",
"-dPDFSETTINGS=/ebook",
"-dAutoRotatePages=/None",
"-dColorImageDownsampleType=/Bicubic",
"-dColorImageResolution=135",
"-dGrayImageDownsampleType=/Bicubic",
"-dGrayImageResolution=135",
"-dMonoImageDownsampleType=/Bicubic",
"-dMonoImageResolution=135",
]
.iter(),
)
.arg(format!(
"-sOutputFile={}",
outpath.as_ref().to_string_lossy().to_string()
))
.arg(inpath.as_ref().to_string_lossy().to_string());
cmd
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pdf_to_cmp_pdf() {
use pdf_to_cmp_pdf as f;
for p in &["", "/", "./", "../"] {
for d in &["", "dir/", "spaced dir/", "dotted.dir/"] {
for n in &[
"name",
"spaced name",
"dotted.name",
".hidden",
".pdf",
"strange'name",
] {
let before = format!("{}{}{}.pdf", p, d, n);
let after = format!("{}{}{}.cmp.pdf", p, d, n);
assert_eq!(f(before), Some(after.into()));
let before = format!("{}{}{}", p, d, n);
assert_eq!(f(before), None);
let before = format!("{}{}{}.ext", p, d, n);
assert_eq!(f(before), None);
}
}
}
}
#[test]
fn test_pdf_into_subdir() {
use pdf_into_subdir as f;
for p in &["", "/", "./", "../"] {
for d in &["", "dir/", "spaced dir/", "dotted.dir/"] {
for s in &["sub/", "spaced sub/", "dotted.sub/"] {
for n in &[
"name",
"spaced name",
"dotted.name",
".hidden",
".pdf",
"strange'name",
] {
let before = format!("{}{}{}.pdf", p, d, n);
let after = format!("{}{}{}{}.pdf", p, d, s, n);
assert_eq!(f(before, s), Some(after.into()));
let before = format!("{}{}{}", p, d, n);
assert_eq!(f(before, s), None);
let before = format!("{}{}{}.ext", p, d, n);
assert_eq!(f(before, s), None);
}
}
}
}
}
#[test]
fn test_pdf_subdir() {
use pdf_subdir as f;
for p in &["", "/", "./", "../"] {
for d in &["", "dir/", "spaced dir/", "dotted.dir/"] {
for s in &["sub/", "spaced sub/", "dotted.sub/"] {
for n in &[
"name",
"spaced name",
"dotted.name",
".hidden",
".pdf",
"strange'name",
] {
let before = format!("{}{}{}.pdf", p, d, n);
let after = format!("{}{}{}", p, d, s);
assert_eq!(f(before, s), Some(after.into()));
let before = format!("{}{}{}", p, d, n);
assert_eq!(f(before, s), None);
let before = format!("{}{}{}.ext", p, d, n);
assert_eq!(f(before, s), None);
}
}
}
}
}
}