proper setting and getting local files

This commit is contained in:
SpikeHD
2022-06-01 18:52:32 -07:00
parent a1f768140d
commit aedd7ecf3c
5 changed files with 34 additions and 19 deletions

View File

@@ -48,7 +48,6 @@ fn main() {
system_helpers::run_program,
system_helpers::run_jar,
system_helpers::open_in_browser,
system_helpers::get_local_bg_path,
system_helpers::copy_file,
proxy::set_proxy_addr,
proxy::generate_ca_files,
@@ -144,8 +143,8 @@ async fn req_get(url: String) -> String {
}
#[tauri::command]
async fn get_bg_file(bg_path: String) -> String {
let install_loc = system_helpers::install_location();
async fn get_bg_file(bg_path: String, appdata: String) -> String {
let copy_loc = appdata;
let query = web::query("https://api.grasscutters.xyz/cultivation/query").await;
let response_data: APIQuery = match serde_json::from_str(&query) {
Ok(data) => data,
@@ -158,8 +157,8 @@ async fn get_bg_file(bg_path: String) -> String {
let file_name = response_data.bg_file.to_string();
// First we see if the file already exists in our local bg folder.
if file_helpers::dir_exists(format!("{}\\bg\\{}", install_loc, file_name).as_str()) {
return format!("{}\\{}", install_loc, response_data.bg_file.as_str());
if file_helpers::dir_exists(format!("{}\\bg\\{}", copy_loc, file_name).as_str()) {
return format!("{}\\{}", copy_loc, response_data.bg_file.as_str());
}
// Now we check if the bg folder, which is one directory above the game_path, exists.
@@ -177,12 +176,12 @@ async fn get_bg_file(bg_path: String) -> String {
}
// The image exists, lets copy it to our local '\bg' folder.
let bg_img_path_local = format!("{}\\bg\\{}", install_loc, file_name.as_str());
let bg_img_path_local = format!("{}\\bg\\{}", copy_loc, file_name.as_str());
return match std::fs::copy(bg_img_path, bg_img_path_local) {
Ok(_) => {
// Copy was successful, lets return true.
format!("{}\\{}", install_loc, response_data.bg_file.as_str())
format!("{}\\{}", copy_loc, response_data.bg_file.as_str())
}
Err(e) => {
// Copy failed, lets return false

View File

@@ -3,7 +3,8 @@ use std::thread;
use std::process::Command;
use tauri;
use open;
use crate::system_helpers;
use crate::file_helpers;
#[tauri::command]
pub fn run_program(path: String) {
@@ -56,15 +57,15 @@ pub fn open_in_browser(url: String) {
};
}
#[tauri::command]
pub fn get_local_bg_path() -> String {
// Get the local BG folder.
return format!("{}/bg", system_helpers::install_location())
}
#[tauri::command]
pub fn copy_file(path: String, new_path: String) -> bool {
let filename = &path.split("/").last().unwrap();
let mut new_path_buf = std::path::PathBuf::from(&new_path);
// If the new path doesn't exist, create it.
if !file_helpers::dir_exists(new_path_buf.pop().to_string().as_str()) {
std::fs::create_dir_all(&new_path).unwrap();
}
// Copy old to new
match std::fs::copy(&path, format!("{}/{}", new_path, filename)) {