2021-09-30 09:41:21 +00:00
|
|
|
use std::borrow::Borrow;
|
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{Error};
|
2020-02-28 06:30:35 +00:00
|
|
|
use serde_json::Value;
|
|
|
|
|
2021-11-19 12:48:38 +00:00
|
|
|
use pbs_api_types::HumanByte;
|
|
|
|
|
2021-09-30 09:41:21 +00:00
|
|
|
pub fn strip_server_file_extension(name: &str) -> &str {
|
2020-02-28 06:30:35 +00:00
|
|
|
if name.ends_with(".didx") || name.ends_with(".fidx") || name.ends_with(".blob") {
|
2021-09-30 09:41:21 +00:00
|
|
|
&name[..name.len()-5]
|
2020-02-28 06:30:35 +00:00
|
|
|
} else {
|
2021-09-30 09:41:21 +00:00
|
|
|
name // should not happen
|
2020-02-28 06:30:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 09:41:21 +00:00
|
|
|
pub fn render_backup_file_list<S: Borrow<str>>(files: &[S]) -> String {
|
|
|
|
let mut files: Vec<&str> = files.iter()
|
|
|
|
.map(|v| strip_server_file_extension(v.borrow()))
|
2020-02-28 06:30:35 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
files.sort();
|
|
|
|
|
2021-09-30 09:35:12 +00:00
|
|
|
files.join(" ")
|
2020-02-28 06:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn render_epoch(value: &Value, _record: &Value) -> Result<String, Error> {
|
|
|
|
if value.is_null() { return Ok(String::new()); }
|
|
|
|
let text = match value.as_i64() {
|
|
|
|
Some(epoch) => {
|
2021-10-08 09:19:37 +00:00
|
|
|
if let Ok(epoch_string) = proxmox_time::strftime_local("%c", epoch as i64) {
|
2020-09-12 13:10:47 +00:00
|
|
|
epoch_string
|
|
|
|
} else {
|
|
|
|
epoch.to_string()
|
2020-09-11 12:34:36 +00:00
|
|
|
}
|
|
|
|
},
|
2020-02-28 06:30:35 +00:00
|
|
|
None => {
|
|
|
|
value.to_string()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(text)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn render_task_status(value: &Value, record: &Value) -> Result<String, Error> {
|
|
|
|
if record["endtime"].is_null() {
|
|
|
|
Ok(value.as_str().unwrap_or("running").to_string())
|
|
|
|
} else {
|
|
|
|
Ok(value.as_str().unwrap_or("unknown").to_string())
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 14:08:15 +00:00
|
|
|
|
|
|
|
pub fn render_bool_with_default_true(value: &Value, _record: &Value) -> Result<String, Error> {
|
|
|
|
let value = value.as_bool().unwrap_or(true);
|
|
|
|
Ok((if value { "1" } else { "0" }).to_string())
|
|
|
|
}
|
2020-07-23 07:36:02 +00:00
|
|
|
|
2020-10-20 09:43:48 +00:00
|
|
|
pub fn render_bytes_human_readable(value: &Value, _record: &Value) -> Result<String, Error> {
|
|
|
|
if value.is_null() { return Ok(String::new()); }
|
|
|
|
let text = match value.as_u64() {
|
|
|
|
Some(bytes) => {
|
|
|
|
HumanByte::from(bytes).to_string()
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
value.to_string()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(text)
|
|
|
|
}
|