Skip to content

Commit

Permalink
Merge pull request #1310 from ViRb3/master
Browse files Browse the repository at this point in the history
Ignore readonly metadata and handle errors instead
  • Loading branch information
svenstaro authored Jan 13, 2024
2 parents 4569f41 + 405a250 commit 0b52969
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/file_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
io::Write,
path::{Component, Path, PathBuf},
};
use std::io::ErrorKind;

use actix_web::{http::header, web, HttpRequest, HttpResponse};
use futures::TryStreamExt;
Expand All @@ -27,9 +28,13 @@ async fn save_file(
return Err(ContextualError::DuplicateFileError);
}

let file = std::fs::File::create(&file_path).map_err(|e| {
ContextualError::IoError(format!("Failed to create {}", file_path.display()), e)
})?;
let file = match std::fs::File::create(&file_path) {
Err(err) if err.kind() == ErrorKind::PermissionDenied => Err(ContextualError::InsufficientPermissionsError(
file_path.display().to_string(),
)),
Err(err) => Err(ContextualError::IoError(format!("Failed to create {}", file_path.display()), err)),
Ok(v) => Ok(v)
}?;

let (_, written_len) = field
.map_err(|x| ContextualError::MultipartError(x.to_string()))
Expand Down Expand Up @@ -62,9 +67,6 @@ async fn handle_multipart(
"cannot upload file to {}, since it's not a directory",
&path.display()
))),
Ok(metadata) if metadata.permissions().readonly() => Err(
ContextualError::InsufficientPermissionsError(path.display().to_string()),
),
Ok(_) => Ok(()),
}?;

Expand Down Expand Up @@ -127,11 +129,13 @@ async fn handle_multipart(
}
}

std::fs::create_dir_all(&absolute_path).map_err(|e| {
ContextualError::IoError(format!("Failed to create {}", user_given_path.display()), e)
})?;

return Ok(0);
return match std::fs::create_dir_all(&absolute_path) {
Err(err) if err.kind() == ErrorKind::PermissionDenied => Err(ContextualError::InsufficientPermissionsError(
path.display().to_string(),
)),
Err(err) => Err(ContextualError::IoError(format!("Failed to create {}", user_given_path.display()), err)),
Ok(_) => Ok(0),
};
}

let filename = field.content_disposition().get_filename().ok_or_else(|| {
Expand Down

0 comments on commit 0b52969

Please sign in to comment.