diff --git a/README.md b/README.md index 8481f4b..f72f67b 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,6 @@ cargo install flarumdown ``` bash RUST_LOG=warn flarumdown -s '/path/to/flarum.sqlite' \ -t '/path/to/target' \ - -u '/path/to/flarum/public' \ -i index \ -r http://[202:68d0:f0d5:b88d:1d1a:555e:2f6b:3148] \ -r http://[505:6847:c778:61a1:5c6d:e802:d291:8191] \ diff --git a/src/config.rs b/src/config.rs index d2fb9b9..d7b6d9d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,10 +8,12 @@ pub struct Config { #[arg(short, long)] pub source: PathBuf, - /// Path to export FoF/upload tags from - /// * tip: root should be the path to `flarum/public` dir + /// Path to export FoF/upload files from + /// tips: + /// * root should be the path to `flarum/public` dir + /// * use rsync instead of this option for longer SSD life #[arg(short, long)] - pub upload: PathBuf, + pub upload: Option, /// Path to export markdown #[arg(short, long)] diff --git a/src/main.rs b/src/main.rs index c5fcdcf..1462795 100644 --- a/src/main.rs +++ b/src/main.rs @@ -190,37 +190,47 @@ fn main() -> Result<()> { post }); for upload in &uploads { - let path_source = { - let mut p = PathBuf::from(&config.upload); - p.push(upload); - match p.canonicalize() { - Ok(canonical) => { - if canonical.starts_with(&config.upload) { - canonical - } else { - warn!( - "Possible traversal request: `{}` (post #{}, user #{})", - canonical.to_string_lossy(), - post.id, - post.user_id - ); - continue; - } - } - Err(e) => { - error!("{e}: `{}` (post #{})", p.to_string_lossy(), post.id); - continue; - } - } - }; let path_target = { let mut p = PathBuf::from(&config.target); p.push(upload); p }; - if !path_target.exists() { - create_dir_all(path_target.parent().unwrap())?; - copy(path_source, path_target)?; + // upload option is active, create files copy in the destinations + if let Some(ref upload_source) = config.upload { + let path_source = { + let mut p = PathBuf::from(upload_source); + p.push(upload); + match p.canonicalize() { + Ok(canonical) => { + if canonical.starts_with(upload_source) { + canonical + } else { + warn!( + "Possible traversal injection: `{}` (post #{}, user #{})", + canonical.to_string_lossy(), + post.id, + post.user_id + ); + continue; + } + } + Err(e) => { + error!("{e}: `{}` (post #{})", p.to_string_lossy(), post.id); + continue; + } + } + }; + if !path_target.exists() { + create_dir_all(path_target.parent().unwrap())?; + copy(path_source, path_target)?; + } + // task delegated to rsync (manually pre-copied FoF/upload destinations must exist) + } else if !path_target.exists() { + warn!( + "Referenced file does not exist: `{}` (post #{})", + path_target.to_string_lossy(), + post.id + ) } } content.push("---\n".into())