diff --git a/backend/src/thumbnailer.rs b/backend/src/thumbnailer.rs index 42b78f9..015c751 100644 --- a/backend/src/thumbnailer.rs +++ b/backend/src/thumbnailer.rs @@ -1,9 +1,8 @@ use std::collections::HashSet; use std::fs; use std::path::Path; -use std::process::Command; -use anyhow::{bail, Result}; -use shlex::Shlex; +use anyhow::{Result}; +use crate::utils::run_cmd; #[derive(Debug)] pub struct Thumbnailer { @@ -45,19 +44,11 @@ impl Thumbnailer { /// Generate thumbnail pub fn gen(&self, orig: &str, new: &str, pixels: i32) -> Result<()> { - let cmd = self.exec + run_cmd(&*self.exec .replace("%s", &*format!("'{pixels}'")) .replace("%u", &shlex::quote(orig)) .replace("%i", &shlex::quote(orig)) - .replace("%o", &shlex::quote(new)); - let args: Vec = Shlex::new(&*cmd).collect(); - let out = Command::new(args[0].to_owned()).args(&args[1..]).output()?; - if !out.status.success() { - error!("Command failed: {cmd}"); - error!("Command output: {:?}", out); - bail!(String::from_utf8(out.stderr)?); - } - debug!("Command output: {:?}", out); + .replace("%o", &shlex::quote(new)))?; Ok(()) } diff --git a/backend/src/utils.rs b/backend/src/utils.rs index 9bfedbd..9100d38 100644 --- a/backend/src/utils.rs +++ b/backend/src/utils.rs @@ -1,5 +1,8 @@ use std::{fs, io}; use std::path::{PathBuf}; +use std::process::{Command, Output}; +use anyhow::{Result, bail}; +use shlex::Shlex; pub fn write_sf>(path: &PathBuf, contents: C) -> io::Result<()> { // Create parent if it has parent @@ -9,3 +12,15 @@ pub fn write_sf>(path: &PathBuf, contents: C) -> io::Result<()> { fs::write(path, contents) } + +pub fn run_cmd(cmd: &str) -> Result { + let args: Vec = Shlex::new(&*cmd).collect(); + let out = Command::new(args[0].to_owned()).args(&args[1..]).output()?; + if !out.status.success() { + error!("Command failed: {cmd}"); + error!("Command output: {:?}", out); + bail!(String::from_utf8(out.stderr)?); + } + debug!("Command output: {:?}", out); + Ok(out) +}