opt rm files
This commit is contained in:
30
src/main.rs
30
src/main.rs
@@ -10,6 +10,7 @@ pub mod opt;
|
||||
use std::{
|
||||
fs::{self, File},
|
||||
time::SystemTime,
|
||||
path::Path,
|
||||
};
|
||||
use rust_util::{
|
||||
XResult,
|
||||
@@ -123,6 +124,22 @@ pub fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem
|
||||
|
||||
let temp_zip_file = &format!("temp_file_{}.zip", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs());
|
||||
let temp_pgp_file = &format!("temp_file_{}.gpg", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs());
|
||||
|
||||
let remove_temp_files = || {
|
||||
if Path::new(temp_zip_file).exists() {
|
||||
if options.verbose {
|
||||
print_message(MessageType::DEBUG, &format!("Remove file: {}", temp_zip_file));
|
||||
}
|
||||
fs::remove_file(temp_zip_file).ok();
|
||||
}
|
||||
if Path::new(temp_pgp_file).exists() {
|
||||
if options.verbose {
|
||||
print_message(MessageType::DEBUG, &format!("Remove file: {}", temp_pgp_file));
|
||||
}
|
||||
fs::remove_file(temp_pgp_file).ok();
|
||||
}
|
||||
};
|
||||
|
||||
if options.verbose {
|
||||
print_message(MessageType::DEBUG, &format!("Compress file: {} -> {}", target, temp_zip_file));
|
||||
}
|
||||
@@ -136,15 +153,14 @@ pub fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem
|
||||
}
|
||||
if let Err(e) = open_pgp_tool.encrypt_file(temp_zip_file, temp_pgp_file, false) {
|
||||
print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index));
|
||||
fs::remove_file(temp_zip_file).ok();
|
||||
remove_temp_files();
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let file_temp_pgp_file = match File::open(temp_pgp_file) {
|
||||
Err(e) => {
|
||||
print_message(MessageType::ERROR, &format!("Error in open file: {}, at item index: {}", e, item_index));
|
||||
fs::remove_file(temp_zip_file).ok();
|
||||
fs::remove_file(temp_pgp_file).ok();
|
||||
remove_temp_files();
|
||||
return Ok(());
|
||||
},
|
||||
Ok(file_temp_pgp_file) => file_temp_pgp_file,
|
||||
@@ -153,10 +169,9 @@ pub fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem
|
||||
if options.verbose {
|
||||
print_message(MessageType::DEBUG, &format!("Upload file: {}", temp_pgp_file));
|
||||
}
|
||||
if let Err(e) = oss_client.put_file(bucket, &new_file, 1000, file_temp_pgp_file) {
|
||||
if let Err(e) = oss_client.put_file(bucket, &new_file, oss_util::DEFAULT_URL_VALID_SECS, file_temp_pgp_file) {
|
||||
print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index));
|
||||
fs::remove_file(temp_zip_file).ok();
|
||||
fs::remove_file(temp_pgp_file).ok();
|
||||
remove_temp_files();
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -171,8 +186,7 @@ pub fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem
|
||||
print_message(MessageType::OK, &format!("Success, at item index: {}", item_index));
|
||||
},
|
||||
};
|
||||
fs::remove_file(temp_zip_file).ok();
|
||||
fs::remove_file(temp_pgp_file).ok();
|
||||
remove_temp_files();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ use reqwest::{
|
||||
};
|
||||
use rust_util::*;
|
||||
|
||||
pub const DEFAULT_URL_VALID_SECS: u64 = 1000;
|
||||
|
||||
pub const OSS_VERB_GET: &str = "GET";
|
||||
pub const OSS_VERB_PUT: &str = "PUT";
|
||||
pub const OSS_VERB_DELETE: &str = "DELETE";
|
||||
|
||||
Reference in New Issue
Block a user