use std::fs;
use std::time::SystemTime;
use chrono::{TimeZone, Utc};
use chrono::FixedOffset;
use std::path::Path;
use crate::pubunit;
pub fn get_check_antivirus(osn:&str) -> String{
let mut res = "".to_string();
match osn {
"topsec" =>{
let file_path = "/opt/apps/cn.com.topsec.edragent/files/database/topsec.db";
match fs::metadata(file_path) {
Ok(meta) => {
if meta.is_file() {
let t = SystemTime::from(meta.modified().unwrap());
res = time_to_beijing(&t);
} else {
println!("Not a valid file.");
}
},
Err(_) => {
res = "Failed to get virus lib for the file.".to_string();
}
};
}
_=> res = "err".to_string(),
}
return res.to_string();
}
pub fn get_dir_search(_path:&str) -> Vec<String>{
let mut files = vec![];
let path = Path::new(&_path);
let Ok(_) = pubunit::visit_dirs(path, &|entry, files| {
files.push(entry);
},&mut files) else {
eprintln!("Failed to read the directory!");
return vec![];
};
return files.iter()
.map(|it| it.path().to_string_lossy().to_string().replacen(_path, "", 1))
.collect::<Vec<String>>();
}
pub fn time_to_beijing(st:&SystemTime)-> String{
let t = st.duration_since(std::time::UNIX_EPOCH).unwrap().as_secs();
let dt = Utc.timestamp_opt(t.try_into().unwrap(), 0).unwrap();
let china_timezone = FixedOffset::east_opt(8 * 3600);
let chinatime = dt.with_timezone(&china_timezone.unwrap());
let res = chinatime.format("%Y-%m-%d %H:%M:%S");
return res.to_string();
}