Browse Source

Safely create parent tmp dir when needed

v0.2.x
asonix 11 months ago
parent
commit
4ce7b79b86
  1. 9
      src/main.rs
  2. 4
      src/processor.rs
  3. 11
      src/validate/mod.rs
  4. 6
      src/validate/transcode.rs

9
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> {

4
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());
}

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

6
src/validate/transcode.rs

@ -249,9 +249,9 @@ fn transcoder(
Ok(Transcoder {
stream: input.index(),
filter: filter,
decoder: decoder,
encoder: encoder,
filter,
decoder,
encoder,
})
}

Loading…
Cancel
Save