evaluator: Add confirmation dialog for file overwriting

This commit is contained in:
Vinícius Rodrigues Miguel 2021-03-28 14:52:09 -03:00
parent 03d6fc1e60
commit 40fb926d80
3 changed files with 15 additions and 3 deletions

View File

@ -37,7 +37,8 @@ impl TarDecompressor {
let file_path = PathBuf::from(into).join(file.path()?); let file_path = PathBuf::from(into).join(file.path()?);
if file_path.exists() { if file_path.exists() {
let file_path_str = &*file_path.to_string_lossy(); let file_path_str = &*file_path.to_string_lossy();
if confirm.ask(Some(file_path_str))? { if !confirm.ask(Some(file_path_str))? {
// The user does not want to overwrite the file
continue; continue;
} }
} }
@ -51,7 +52,7 @@ impl TarDecompressor {
file.size() file.size()
); );
let file_path = fs::canonicalize(into.join(file.path()?))?; let file_path = fs::canonicalize(file_path)?;
files_unpacked.push(file_path); files_unpacked.push(file_path);
} }

View File

@ -53,7 +53,8 @@ impl ZipDecompressor {
let file_path = into.join(file_path); let file_path = into.join(file_path);
if file_path.exists() { if file_path.exists() {
let file_path_str = &*file_path.as_path().to_string_lossy(); let file_path_str = &*file_path.as_path().to_string_lossy();
if confirm.ask(Some(file_path_str))? { if !confirm.ask(Some(file_path_str))? {
// The user does not want to overwrite the file
continue; continue;
} }
} }

View File

@ -13,6 +13,7 @@ use crate::{
TarDecompressor, ZipDecompressor, TarDecompressor, ZipDecompressor,
}, },
extension::{CompressionFormat, Extension}, extension::{CompressionFormat, Extension},
dialogs::Confirmation,
file::File, file::File,
utils, utils,
}; };
@ -148,9 +149,18 @@ impl Evaluator {
} }
fn compress_files(files: Vec<PathBuf>, mut output: File) -> crate::Result<()> { fn compress_files(files: Vec<PathBuf>, mut output: File) -> crate::Result<()> {
let confirm = Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE"));
let (first_compressor, second_compressor) = Self::get_compressor(&output)?; let (first_compressor, second_compressor) = Self::get_compressor(&output)?;
let output_path = output.path.clone(); let output_path = output.path.clone();
if output_path.exists() {
let output_path_str = &*output_path.to_string_lossy();
if !confirm.ask(Some(output_path_str))? {
// The user does not want to overwrite the file
return Ok(());
}
}
let bytes = match first_compressor { let bytes = match first_compressor {
Some(first_compressor) => { Some(first_compressor) => {