make upload argument optional (delegate FoF/upload to external rsync logic)

This commit is contained in:
yggverse 2026-03-19 23:24:21 +02:00
parent 42ff89d741
commit 00ab15d850
3 changed files with 41 additions and 30 deletions

View file

@ -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<PathBuf>,
/// Path to export markdown
#[arg(short, long)]

View file

@ -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())