feat: update info

This commit is contained in:
2023-09-06 00:34:26 +08:00
parent d1d5c166de
commit d96ebdd5ba
6 changed files with 80 additions and 57 deletions

View File

@@ -3,13 +3,14 @@ use std::fs::File;
use std::ops::Add;
use std::path::PathBuf;
use std::time::{Duration, SystemTime};
use rust_util::{information, opt_result, simple_error, success, XResult};
use rust_util::{iff, information, opt_result, simple_error, success, XResult};
use crate::file;
pub fn info(path: PathBuf, raw_meta: bool) -> XResult<()> {
let path_display = format!("{}", path.display());
let mut file_in = opt_result!(File::open(path), "Open file: {} failed: {}", &path_display);
let meta = opt_result!(file::read_tiny_encrypt_meta(&mut file_in), "Read file: {}, failed: {}", &path_display);
let mut meta = opt_result!(file::read_tiny_encrypt_meta(&mut file_in), "Read file: {}, failed: {}", &path_display);
meta.normalize();
if raw_meta {
success!("Meta data:\n{}", serde_json::to_string_pretty(&meta).expect("SHOULD NOT HAPPEN"));
@@ -29,11 +30,14 @@ pub fn info(path: PathBuf, raw_meta: bool) -> XResult<()> {
infos.push(format!("{}: {}", header("Last modified"), fmt.format_local(from_unix_epoch(meta.file_last_modified))));
infos.push(format!("{}: {}", header("Enc file created"), fmt.format_local(from_unix_epoch(meta.created))));
infos.push(format!("{}: KMS: {}, PGP: {}",
header("Envelops"),
to_yes_or_no(&meta.envelop),
to_yes_or_no(&meta.pgp_envelop)
));
meta.envelops.as_ref().map(|envelops| envelops.iter().enumerate().for_each(|(i, envelop)| {
infos.push(format!("{}: {}{}{}",
header(&format!("Envelop #{}", i + 1)),
envelop.r#type.to_uppercase(),
iff!(envelop.kid.is_empty(), "".into(), format!(", Kid: {}", envelop.kid)),
iff!(envelop.desc.is_none(), "".into(), format!(", Desc: {}", envelop.desc.as_ref().unwrap()))
));
}));
meta.pgp_fingerprint.map(|fingerprint| {
infos.push(format!("{}: {}", header("PGP fingerprint"), fingerprint));
});
@@ -41,6 +45,13 @@ pub fn info(path: PathBuf, raw_meta: bool) -> XResult<()> {
infos.push(format!("{}: {}", header("Comment"), comment));
});
infos.push(format!("{}: {}", header("Encrypted comment"), to_yes_or_no(&meta.encrypted_comment)));
infos.push(format!("{}: {}", header("Encrypted meta"), to_yes_or_no(&meta.encrypted_meta)));
let encryption_algorithm = if let Some(encryption_algorithm) = &meta.encryption_algorithm {
encryption_algorithm.to_string()
} else {
"AES/GCM (default)".to_string()
};
infos.push(format!("{}: {}", header("Encryption algorithm"), encryption_algorithm));
success!("{}\n", infos.join("\n"));
Ok(())
@@ -51,7 +62,7 @@ fn from_unix_epoch(t: u64) -> SystemTime {
}
fn header(h: &str) -> String {
let width = 18;
let width = 21;
h.to_string() + ".".repeat(max(width - h.len(), 0)).as_str()
}