chore: export

This commit is contained in:
lencx
2023-01-13 01:18:40 +08:00
parent e24fd6a33f
commit e473268df1
8 changed files with 153 additions and 30 deletions

View File

@@ -1,10 +1,11 @@
use crate::{
app::window,
app::{fs_extra, window},
conf::{ChatConfJson, GITHUB_PROMPTS_CSV_URL},
utils,
utils::{self, chat_root, create_file},
};
use log::info;
use std::{collections::HashMap, fs, path::PathBuf};
use regex::Regex;
use std::{collections::HashMap, fs, path::PathBuf, vec};
use tauri::{api, command, AppHandle, Manager, Theme};
use walkdir::WalkDir;
@@ -35,14 +36,16 @@ pub fn fullscreen(app: AppHandle) {
#[command]
pub fn download(_app: AppHandle, name: String, blob: Vec<u8>) {
let path = api::path::download_dir().unwrap().join(name);
let path = chat_root().join(PathBuf::from(name));
create_file(&path).unwrap();
fs::write(&path, blob).unwrap();
utils::open_file(path);
}
#[command]
pub fn save_file(_app: AppHandle, name: String, content: String) {
let path = api::path::download_dir().unwrap().join(name);
let path = chat_root().join(PathBuf::from(name));
create_file(&path).unwrap();
fs::write(&path, content).unwrap();
utils::open_file(path);
}
@@ -174,6 +177,76 @@ pub fn cmd_list() -> Vec<ModelRecord> {
list
}
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct FileMetadata {
pub name: String,
pub ext: String,
pub created: u64,
pub id: String,
}
#[command]
pub fn download_list(filename: Option<String>, id: Option<String>) {
info!("download_list");
let download_path = chat_root().join("chat.download.json");
let content = fs::read_to_string(&download_path).unwrap_or_else(|err| {
info!("download_list_error: {}", err);
fs::write(&download_path, "[]").unwrap();
"[]".to_string()
});
let mut list = serde_json::from_str::<Vec<serde_json::Value>>(&content)
.unwrap_or_else(|err| {
info!("download_list_parse_error: {}", err);
vec![]
});
let list2 = &list;
let mut my_hashmap = HashMap::new();
utils::vec_to_hashmap(list2.clone().into_iter(), "id", &mut my_hashmap);
for entry in WalkDir::new(utils::chat_root().join("download"))
.into_iter()
.filter_entry(|e| !utils::is_hidden(e))
.filter_map(|e| e.ok())
{
let metadata = entry.metadata().unwrap();
if metadata.is_file() {
let file_path = entry.path().display().to_string();
let re = Regex::new(r"(?P<id>[\d\w]+).(?P<ext>\w+)$").unwrap();
let caps = re.captures(&file_path).unwrap();
let fid = &caps["id"];
let fext = &caps["ext"];
let mut file_data = FileMetadata {
name: fid.to_string(),
id: fid.to_string(),
ext: fext.to_string(),
created: fs_extra::system_time_to_ms(metadata.created()),
};
if my_hashmap.get(fid).is_some() && filename.is_some() && id.is_some() {
if let Some(ref v) = id {
if fid == v {
if let Some(ref v2) = filename {
file_data.name = v2.to_string();
}
}
}
}
list.push(serde_json::to_value(file_data).unwrap());
}
}
dbg!(&list);
list.sort_by(|a, b| {
let a1 = a.get("created").unwrap().as_u64().unwrap();
let b1 = b.get("created").unwrap().as_u64().unwrap();
a1.cmp(&b1).reverse()
});
fs::write(download_path, serde_json::to_string_pretty(&list).unwrap()).unwrap();
}
#[command]
pub async fn sync_prompts(app: AppHandle, time: u64) -> Option<Vec<ModelRecord>> {
let res = utils::get_data(GITHUB_PROMPTS_CSV_URL, Some(&app))

View File

@@ -60,7 +60,7 @@ struct UnixMetadata {
#[serde(rename_all = "camelCase")]
pub struct Metadata {
accessed_at_ms: u64,
created_at_ms: u64,
pub created_at_ms: u64,
modified_at_ms: u64,
is_dir: bool,
is_file: bool,
@@ -74,7 +74,7 @@ pub struct Metadata {
file_attributes: u32,
}
fn system_time_to_ms(time: std::io::Result<SystemTime>) -> u64 {
pub fn system_time_to_ms(time: std::io::Result<SystemTime>) -> u64 {
time.map(|t| {
let duration_since_epoch = t.duration_since(UNIX_EPOCH).unwrap();
duration_since_epoch.as_millis() as u64