2 changed files with 97 additions and 57 deletions
-
72src/main.rs
-
82src/process.rs
@ -0,0 +1,82 @@ |
|||||
|
use std::{
|
||||
|
env::{current_dir,
|
||||
|
set_current_dir},
|
||||
|
ffi::OsString,
|
||||
|
io::ErrorKind,
|
||||
|
path::Path,
|
||||
|
process::{Command, ExitStatus}
|
||||
|
};
|
||||
|
|
||||
|
use anyhow::anyhow;
|
||||
|
use http::Uri;
|
||||
|
use shellwords::escape;
|
||||
|
use tokio::fs::{create_dir, remove_dir_all};
|
||||
|
use which::which;
|
||||
|
|
||||
|
use log::{log, Level};
|
||||
|
|
||||
|
|
||||
|
pub(super) async fn enter_download_dir(name: &dyn AsRef<Path>) -> anyhow::Result<OsString> {
|
||||
|
let basename = name.as_ref().file_stem()
|
||||
|
. ok_or(anyhow!("No valid filename given"))?;
|
||||
|
|
||||
|
if let Err(error) = create_dir(basename).await {
|
||||
|
if ErrorKind::AlreadyExists == error.kind() {
|
||||
|
if ! Path::new(basename).is_dir() {
|
||||
|
return Err(anyhow!("Unable to create directory at {:?}", basename));
|
||||
|
}
|
||||
|
} else {
|
||||
|
return Err(anyhow!("Unable to create directory at {:?}", basename));
|
||||
|
}
|
||||
|
}
|
||||
|
set_current_dir(basename)?;
|
||||
|
|
||||
|
Ok(basename.into())
|
||||
|
}
|
||||
|
|
||||
|
pub(super) async fn remove_download_dir(name: &dyn AsRef<Path>) -> anyhow::Result<()> {
|
||||
|
let name = name.as_ref().parent()
|
||||
|
. ok_or(anyhow!("Failed to get parent of download dir"))?;
|
||||
|
set_current_dir(name)?;
|
||||
|
remove_dir_all(name).await?;
|
||||
|
|
||||
|
Ok(())
|
||||
|
}
|
||||
|
|
||||
|
pub(super) async fn ffmpeg(name: &dyn AsRef<Path>, uri: &Uri) -> anyhow::Result<ExitStatus> {
|
||||
|
let outfile = current_dir()?;
|
||||
|
let outfile = outfile.parent().ok_or(anyhow!("Can't get output dir"))?;
|
||||
|
let mut outfile = outfile.canonicalize()?;
|
||||
|
outfile.push(name);
|
||||
|
let outfile = outfile.as_os_str();
|
||||
|
|
||||
|
let which = which("ffmpeg");
|
||||
|
let ffmpeg = which?;
|
||||
|
let ffmpeg = ffmpeg.as_os_str();
|
||||
|
|
||||
|
let index = Path::new(uri.path()).file_name()
|
||||
|
. ok_or(anyhow!("unable to get index filename from url"))?;
|
||||
|
let index = Path::new(index).canonicalize()?;
|
||||
|
let index = index.as_os_str();
|
||||
|
|
||||
|
log!(Level::Debug, "ffmpeg: {:?}", ffmpeg);
|
||||
|
log!(Level::Debug, "index: {:?}", index);
|
||||
|
log!(Level::Debug, "outfile: {:?}", outfile);
|
||||
|
|
||||
|
log!( Level::Debug
|
||||
|
, "execute: {} -allowed_extensions ALL -i {} -c copy {}"
|
||||
|
, escape(ffmpeg.try_into()?)
|
||||
|
, escape(index.try_into()?)
|
||||
|
, escape(outfile.try_into()?) );
|
||||
|
let mut child = Command::new(ffmpeg)
|
||||
|
. arg("-allowed_extensions")
|
||||
|
. arg("ALL")
|
||||
|
. arg("-i")
|
||||
|
. arg(index)
|
||||
|
. arg("-c")
|
||||
|
. arg("copy")
|
||||
|
. arg(outfile)
|
||||
|
. spawn()?;
|
||||
|
|
||||
|
Ok(child.wait()?)
|
||||
|
}
|
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue