add process_oss_files

This commit is contained in:
2019-12-07 19:54:02 +08:00
parent 25ad3ca457
commit 8ac0b56bc1

View File

@@ -12,6 +12,7 @@ use rust_util::{
XResult,
util_msg::*,
};
use oss_util::*;
// use config_util::*;
// use pgp_util::OpenPGPTool;
use opt::{
@@ -50,3 +51,56 @@ fn main() -> XResult<()> {
Ok(())
}
// TODO call it!
pub fn process_oss_files(oss_client: &OSSClient, bucket_name: &str, path: &str, meta_file_name: &str, new_file: &str, limit: usize) -> XResult<()> {
// TODO not found?
let meta_file_key = &format!("{}/{}", path, meta_file_name);
let meta_file_content = oss_client.get_file_content(bucket_name, meta_file_key)?;
let (removed_file, new_meta_file_content) = process_new_backup_file(&meta_file_content, new_file, limit)?;
oss_client.put_file_content(bucket_name, meta_file_key, &new_meta_file_content)?;
if !removed_file.is_empty() {
for rm_file in removed_file {
let rm_file_key = &format!("{}/{}", path, rm_file);
oss_client.delete_file(bucket_name, rm_file_key)?;
}
}
Ok(())
}
pub fn process_new_backup_file(backup_content_json: &str, new_item: &str, limit: usize) -> XResult<(Vec<String>, String)> {
let mut removed_vec: Vec<String> = vec![];
let mut parsed_vec = parse_json_array(backup_content_json)?;
while parsed_vec.len() + 1 > limit {
removed_vec.push(parsed_vec.remove(0));
}
parsed_vec.push(new_item.to_string());
let stringifyed_json = stringity_json_array(&parsed_vec)?;
Ok((removed_vec, stringifyed_json.to_string()))
}
pub fn stringity_json_array(vec: &Vec<String>) -> XResult<String> {
let mut json_arr = json::JsonValue::new_array();
for v in vec {
json_arr.push(json::JsonValue::from(v.as_str()))?;
}
Ok(json::stringify_pretty(json_arr, 4))
}
pub fn parse_json_array(arr: &str) -> XResult<Vec<String>> {
let mut vec: Vec<String> = vec![];
if arr != "" {
let json_arr = &json::parse(&arr)?;
if json_arr.is_array() {
for a in json_arr.members() {
match a.as_str() {
None => (),
Some(s) => vec.push(s.to_string()),
};
}
}
}
Ok(vec)
}