Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new flag: Enable renaming during file upload if duplicate exists #1453

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ Options:

[env: OVERWRITE_FILES=]

-R, --rename-duplicate
Enable renaming files during file upload if duplicate exists

[env: RENAME_DUPLICATE_FILES=]

-r, --enable-tar
Enable uncompressed tar archive generation

Expand Down Expand Up @@ -497,7 +502,7 @@ You can provide `-i` multiple times to bind to multiple interfaces at the same t

This is mostly a note for me on how to release this thing:

- Make sure `CHANGELOG.md` is up to date.
- Make sure [CHANGELOG.md](./CHANGELOG.md) is up to date.
- `cargo release <version>`
- `cargo release --execute <version>`
- Releases will automatically be deployed by GitHub Actions.
Expand Down
9 changes: 9 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ pub struct CliArgs {
#[arg(short = 'o', long = "overwrite-files", env = "OVERWRITE_FILES")]
pub overwrite_files: bool,

/// Enable renaming files during file upload if duplicate exists
svenstaro marked this conversation as resolved.
Show resolved Hide resolved
#[arg(
short = 'R',
long = "rename-duplicate",
env = "RENAME_DUPLICATE_FILES",
conflicts_with = "overwrite_files"
)]
pub rename_duplicate: bool,

/// Enable uncompressed tar archive generation
#[arg(short = 'r', long = "enable-tar", env = "MINISERVE_ENABLE_TAR")]
pub enable_tar: bool,
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ pub struct MiniserveConfig {
/// Enable upload to override existing files
pub overwrite_files: bool,

/// Enable renaming files during file upload if duplicate exists
pub rename_duplicate: bool,

/// If false, creation of uncompressed tar archives is disabled
pub tar_enabled: bool,

Expand Down Expand Up @@ -289,6 +292,7 @@ impl MiniserveConfig {
spa: args.spa,
pretty_urls: args.pretty_urls,
overwrite_files: args.overwrite_files,
rename_duplicate: args.rename_duplicate,
show_qrcode: args.qrcode,
mkdir_enabled: args.mkdir_enabled,
file_upload: args.allowed_upload_dir.is_some(),
Expand Down
4 changes: 3 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ pub enum RuntimeError {
MultipartError(String),

/// Might occur during file upload
#[error("File already exists, and the overwrite_files option has not been set")]
#[error(
"File already exists, and the overwrite_files or rename_duplicate option has not been set"
)]
DuplicateFileError,

/// Upload not allowed
Expand Down
39 changes: 35 additions & 4 deletions src/file_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,34 @@ use crate::{
/// Returns total bytes written to file.
async fn save_file(
field: actix_multipart::Field,
file_path: PathBuf,
mut file_path: PathBuf,
overwrite_files: bool,
rename_duplicate: bool,
) -> Result<u64, RuntimeError> {
if !overwrite_files && file_path.exists() {
return Err(RuntimeError::DuplicateFileError);
if file_path.exists() {
// clap makes sure both overwrite_files and rename_duplicate cannot be true
if rename_duplicate {
// optionally should we use Path::file_prefix, which
// extracts the portion of the file name before the
// first .?
let file_name = file_path.file_stem().unwrap_or_default().to_string_lossy();
let file_ext = file_path.extension().map(|s| s.to_string_lossy());
let filepaths = (1..).map(|i| {
if let Some(ext) = &file_ext {
file_path.with_file_name(format!("{}-{}.{}", file_name, i, ext))
} else {
file_path.with_file_name(format!("{}-{}", file_name, i))
}
});
for fp in filepaths {
if !fp.exists() {
file_path = fp;
break;
}
}
svenstaro marked this conversation as resolved.
Show resolved Hide resolved
} else if !overwrite_files {
return Err(RuntimeError::DuplicateFileError);
}
}

let file = match File::create(&file_path).await {
Expand Down Expand Up @@ -57,6 +80,7 @@ async fn handle_multipart(
mut field: actix_multipart::Field,
path: PathBuf,
overwrite_files: bool,
rename_duplicate: bool,
allow_mkdir: bool,
allow_hidden_paths: bool,
allow_symlinks: bool,
Expand Down Expand Up @@ -168,7 +192,13 @@ async fn handle_multipart(
}
}

save_file(field, path.join(filename_path), overwrite_files).await
save_file(
field,
path.join(filename_path),
overwrite_files,
rename_duplicate,
)
.await
}

/// Query parameters used by upload and rm APIs
Expand Down Expand Up @@ -225,6 +255,7 @@ pub async fn upload_file(
field,
non_canonicalized_target_dir.clone(),
conf.overwrite_files,
conf.rename_duplicate,
conf.mkdir_enabled,
conf.show_hidden,
!conf.no_symlinks,
Expand Down