add func complete

This commit is contained in:
cast1e
2024-10-13 16:56:11 +08:00
parent c9df5b2317
commit ac40bee45e
8 changed files with 118 additions and 53 deletions
+69 -2
View File
@@ -2,9 +2,11 @@ mod setup;
mod reader;
use std::{fs, path};
use serde_json;
use serde_json::{self,json};
use std::path::Path;
use tauri::ipc::Response;
use serde::{Deserialize,Serialize};
use chrono::Local;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
@@ -12,7 +14,7 @@ pub fn run() {
.setup(setup::init)
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_dialog::init())
.invoke_handler(tauri::generate_handler![read_resource_dir,get_file])
.invoke_handler(tauri::generate_handler![read_resource_dir,get_file,new_wallpaper])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
@@ -37,4 +39,69 @@ async fn get_file(path:String) -> Response{
return tauri::ipc::Response::new(data);
}
tauri::ipc::Response::new(String::from(""))
}
#[derive(Deserialize)]
struct AddInfo {
name: String,
preview: String,
media: String,
description: String
}
#[derive(Serialize)]
struct Info{
media_type:String,
description:String,
created:i64
}
#[derive(Serialize)]
struct Opt{
mute:bool
}
#[derive(Serialize)]
struct Config{
name:String,
info:Info,
option:Opt
}
#[tauri::command]
async fn new_wallpaper(info:AddInfo) -> String{
let base_url = String::from("./resource");
let current_time:i64 = Local::now().timestamp();
let folder = format!("{}/{}",base_url,current_time);
if fs::create_dir(Path::new(&folder)).is_err(){
return String::from("Error creating folder.");
}
if let Ok(false) = fs::exists(Path::new(&info.preview)){
return String::from("Source Image doesn't exist.");
}
if fs::copy(Path::new(&info.preview), Path::new(&format!("{}/preview.jpg",folder))).is_err(){
return String::from("Error copy image.");
}
let config =json!( Config{
name:info.name,
info:Info { media_type: String::from("video"), description: info.description, created: current_time},
option:Opt{mute:true}
});
if fs::write(Path::new(&format!("{}/config.json",folder)), config.to_string()).is_err(){
return String::from("Error write config.");
}
if fs::create_dir(Path::new(&format!("{}/res",folder))).is_err(){
return String::from("Error creating res folder.");
}
let media = Path::new(&info.media);
if let Some(filename) = media.file_name().and_then(|f| f.to_str()){
if fs::copy(Path::new(&info.media), Path::new(&format!("{}/res/{}",folder,filename))).is_err(){
return String::from("Error copy media.");
}
}
else{
return String::from("Invalid media path.");
}
String::from("Success")
}