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)) {

View File

@@ -18,6 +18,15 @@
"$DATA/cultivation/*"
]
},
"protocol": {
"all": true,
"asset": true,
"assetScope": [
"$DATA",
"$DATA/cultivation",
"$DATA/cultivation/*"
]
},
"all": true
},
"bundle": {
@@ -54,7 +63,7 @@
}
},
"security": {
"csp": null
"csp": "default-src 'self'; img-src 'self' asset: https://asset.localhost"
},
"updater": {
"active": false

View File

@@ -20,6 +20,7 @@ import { getConfigOption, setConfigOption } from '../utils/configuration'
import { invoke } from '@tauri-apps/api'
import { dataDir } from '@tauri-apps/api/path'
import { appWindow } from '@tauri-apps/api/window'
import { convertFileSrc } from '@tauri-apps/api/tauri'
interface IProps {
[key: string]: never;
@@ -83,7 +84,8 @@ class App extends React.Component<IProps, IState> {
if(game_path) {
// Get the bg by invoking, then set the background to that bg.
const bgLoc: string = await invoke('get_bg_file', {
bgPath: root_path
bgPath: root_path,
appdata: await dataDir()
})
bgLoc && this.setState({
@@ -91,7 +93,7 @@ class App extends React.Component<IProps, IState> {
}, this.forceUpdate)
}
} else this.setState({
bgFile: custom_bg
bgFile: convertFileSrc(custom_bg)
}, this.forceUpdate)
if (!cert_generated) {

View File

@@ -7,6 +7,7 @@ import { setConfigOption, getConfig, getConfigOption } from '../../../utils/conf
import Checkbox from '../common/Checkbox'
import Divider from './Divider'
import { invoke } from '@tauri-apps/api'
import { dataDir } from '@tauri-apps/api/path'
interface IProps {
closeFn: () => void;
@@ -82,12 +83,15 @@ export default class Options extends React.Component<IProps, IState> {
}
async setCustomBackground(value: string) {
setConfigOption('customBackground', value)
const filename = value.replace(/\\/g, '/').split('/').pop()
const localBgPath = (await dataDir() as string).replace(/\\/g, '/')
setConfigOption('customBackground', `${localBgPath}/cultivation/bg/${filename}`)
// Copy the file over to the local directory
invoke('copy_file', {
path: value.replace(/\\/g, '/'),
newPath: await invoke('get_local_bg_path')
newPath: `${localBgPath}cultivation/bg/`
})
}