2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-11-10 06:25:00 +00:00

Safely create parent tmp dir when needed

This commit is contained in:
asonix 2020-10-16 17:48:42 -05:00
parent b64e3615be
commit 4ce7b79b86
4 changed files with 26 additions and 4 deletions

View file

@ -61,6 +61,15 @@ async fn safe_move_file(from: PathBuf, to: PathBuf) -> Result<(), UploadError> {
Ok(()) 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 // Try writing to a file
#[instrument(skip(bytes))] #[instrument(skip(bytes))]
async fn safe_save_file(path: PathBuf, bytes: bytes::Bytes) -> Result<(), UploadError> { async fn safe_save_file(path: PathBuf, bytes: bytes::Bytes) -> Result<(), UploadError> {

View file

@ -6,7 +6,7 @@ use actix_web::web;
use bytes::Bytes; use bytes::Bytes;
use magick_rust::MagickWand; use magick_rust::MagickWand;
use std::path::PathBuf; use std::path::PathBuf;
use tracing::{debug, instrument, Span}; use tracing::{debug, instrument, Span, error};
pub(crate) trait Processor { pub(crate) trait Processor {
fn name() -> &'static str fn name() -> &'static str
@ -231,6 +231,7 @@ pub(crate) async fn prepare_image(
let orig_path = original_path_str.clone(); let orig_path = original_path_str.clone();
let tmpfile = crate::tmp_file(); let tmpfile = crate::tmp_file();
crate::safe_create_parent(tmpfile.clone()).await?;
let tmpfile2 = tmpfile.clone(); let tmpfile2 = tmpfile.clone();
let res = web::block(move || { let res = web::block(move || {
@ -241,6 +242,7 @@ pub(crate) async fn prepare_image(
.await; .await;
if let Err(e) = res { if let Err(e) = res {
error!("transcode error: {:?}", e);
actix_fs::remove_file(tmpfile2).await?; actix_fs::remove_file(tmpfile2).await?;
return Err(e.into()); return Err(e.into());
} }

View file

@ -88,6 +88,13 @@ fn validate_format(file: &str, format: &str) -> Result<(), UploadError> {
Ok(()) 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 // import & export image using the image crate
#[instrument] #[instrument]
pub(crate) async fn validate_image( 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()?) { let content_type = match (prescribed_format, meta.get_media_type()?) {
(_, MediaType::Gif) => { (_, MediaType::Gif) => {
let newfile = tmp_file(); let newfile = tmp_file();
safe_create_parent(&newfile)?;
validate_frames(&tmpfile, &newfile)?; validate_frames(&tmpfile, &newfile)?;
video_mp4() video_mp4()
@ -129,6 +137,7 @@ pub(crate) async fn validate_image(
if webp == "image/webp" => if webp == "image/webp" =>
{ {
let newfile = tmp_file(); let newfile = tmp_file();
safe_create_parent(&newfile)?;
let newfile_str = ptos(&newfile)?; let newfile_str = ptos(&newfile)?;
// clean metadata by writing new webp, since exiv2 doesn't support webp yet // 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), _) => { (Some(format), _) => {
let newfile = tmp_file(); let newfile = tmp_file();
safe_create_parent(&newfile)?;
let newfile_str = ptos(&newfile)?; let newfile_str = ptos(&newfile)?;
{ {
let mut wand = MagickWand::new(); 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" => { (_, MediaType::Other(mp4)) if mp4 == "video/mp4" || mp4 == "video/quicktime" => {
let newfile = tmp_file(); let newfile = tmp_file();
safe_create_parent(&newfile)?;
validate_frames(&tmpfile, &newfile)?; validate_frames(&tmpfile, &newfile)?;
video_mp4() video_mp4()

View file

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