From 4ce7b79b86a2e192f7e4602ddb3d8705b7955788 Mon Sep 17 00:00:00 2001 From: asonix Date: Fri, 16 Oct 2020 17:48:42 -0500 Subject: [PATCH] Safely create parent tmp dir when needed --- src/main.rs | 9 +++++++++ src/processor.rs | 4 +++- src/validate/mod.rs | 11 +++++++++++ src/validate/transcode.rs | 6 +++--- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index b0441ef..1c70936 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,6 +61,15 @@ async fn safe_move_file(from: PathBuf, to: PathBuf) -> Result<(), UploadError> { Ok(()) } +async fn safe_create_parent(path: PathBuf) -> Result<(), UploadError> { + if let Some(path) = path.parent() { + debug!("Creating directory {:?}", path); + actix_fs::create_dir_all(path.to_owned()).await?; + } + + Ok(()) +} + // Try writing to a file #[instrument(skip(bytes))] async fn safe_save_file(path: PathBuf, bytes: bytes::Bytes) -> Result<(), UploadError> { diff --git a/src/processor.rs b/src/processor.rs index 8c8fd25..2b91f03 100644 --- a/src/processor.rs +++ b/src/processor.rs @@ -6,7 +6,7 @@ use actix_web::web; use bytes::Bytes; use magick_rust::MagickWand; use std::path::PathBuf; -use tracing::{debug, instrument, Span}; +use tracing::{debug, instrument, Span, error}; pub(crate) trait Processor { fn name() -> &'static str @@ -231,6 +231,7 @@ pub(crate) async fn prepare_image( let orig_path = original_path_str.clone(); let tmpfile = crate::tmp_file(); + crate::safe_create_parent(tmpfile.clone()).await?; let tmpfile2 = tmpfile.clone(); let res = web::block(move || { @@ -241,6 +242,7 @@ pub(crate) async fn prepare_image( .await; if let Err(e) = res { + error!("transcode error: {:?}", e); actix_fs::remove_file(tmpfile2).await?; return Err(e.into()); } diff --git a/src/validate/mod.rs b/src/validate/mod.rs index c59ecb6..7f86c74 100644 --- a/src/validate/mod.rs +++ b/src/validate/mod.rs @@ -88,6 +88,13 @@ fn validate_format(file: &str, format: &str) -> Result<(), UploadError> { Ok(()) } +fn safe_create_parent(path: &PathBuf) -> Result<(), UploadError> { + if let Some(path) = path.parent() { + std::fs::create_dir_all(path)?; + } + Ok(()) +} + // import & export image using the image crate #[instrument] pub(crate) async fn validate_image( @@ -105,6 +112,7 @@ pub(crate) async fn validate_image( let content_type = match (prescribed_format, meta.get_media_type()?) { (_, MediaType::Gif) => { let newfile = tmp_file(); + safe_create_parent(&newfile)?; validate_frames(&tmpfile, &newfile)?; video_mp4() @@ -129,6 +137,7 @@ pub(crate) async fn validate_image( if webp == "image/webp" => { let newfile = tmp_file(); + safe_create_parent(&newfile)?; let newfile_str = ptos(&newfile)?; // clean metadata by writing new webp, since exiv2 doesn't support webp yet { @@ -153,6 +162,7 @@ pub(crate) async fn validate_image( } (Some(format), _) => { let newfile = tmp_file(); + safe_create_parent(&newfile)?; let newfile_str = ptos(&newfile)?; { let mut wand = MagickWand::new(); @@ -175,6 +185,7 @@ pub(crate) async fn validate_image( } (_, MediaType::Other(mp4)) if mp4 == "video/mp4" || mp4 == "video/quicktime" => { let newfile = tmp_file(); + safe_create_parent(&newfile)?; validate_frames(&tmpfile, &newfile)?; video_mp4() diff --git a/src/validate/transcode.rs b/src/validate/transcode.rs index 708bdb6..6d02ad5 100644 --- a/src/validate/transcode.rs +++ b/src/validate/transcode.rs @@ -249,9 +249,9 @@ fn transcoder( Ok(Transcoder { stream: input.index(), - filter: filter, - decoder: decoder, - encoder: encoder, + filter, + decoder, + encoder, }) }