move image files to local bg folder

This commit is contained in:
SpikeHD
2022-06-01 18:08:50 -07:00
parent da6d05c7d0
commit 53f42be967
4 changed files with 45 additions and 12 deletions

View File

@@ -29,6 +29,12 @@ lazy_static! {
fn main() {
process_watcher();
// Make BG folder if it doesn't exist
let bg_folder = format!("{}/bg", system_helpers::install_location());
if !std::path::Path::new(&bg_folder).exists() {
std::fs::create_dir_all(&bg_folder).unwrap();
}
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![
enable_process_watcher,
@@ -42,6 +48,8 @@ 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,
unzip::unzip,
@@ -137,6 +145,7 @@ 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();
let query = web::query("https://api.grasscutters.xyz/cultivation/query").await;
let response_data: APIQuery = match serde_json::from_str(&query) {
Ok(data) => data,
@@ -149,9 +158,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\\{}", file_name).as_str()) {
let cwd = std::env::current_dir().unwrap();
return format!("{}\\{}", cwd.display(), response_data.bg_file.as_str());
if file_helpers::dir_exists(format!("{}\\bg\\{}", install_loc, file_name).as_str()) {
return format!("{}\\{}", install_loc, response_data.bg_file.as_str());
}
// Now we check if the bg folder, which is one directory above the game_path, exists.
@@ -169,13 +177,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\\{}", file_name.as_str());
let bg_img_path_local = format!("{}\\bg\\{}", install_loc, file_name.as_str());
return match std::fs::copy(bg_img_path, bg_img_path_local) {
Ok(_) => {
// Copy was successful, lets return true.
let cwd = std::env::current_dir().unwrap();
format!("{}\\{}", cwd.display(), response_data.bg_file.as_str())
format!("{}\\{}", install_loc, response_data.bg_file.as_str())
}
Err(e) => {
// Copy failed, lets return false

View File

@@ -3,6 +3,7 @@ use std::thread;
use std::process::Command;
use tauri;
use open;
use crate::system_helpers;
#[tauri::command]
pub fn run_program(path: String) {
@@ -55,6 +56,26 @@ 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();
// Copy old to new
match std::fs::copy(&path, format!("{}/{}", new_path, filename)) {
Ok(_) => true,
Err(e) => {
println!("Failed to copy file: {}", e);
false
}
}
}
pub fn install_location() -> String {
let mut exe_path = std::env::current_exe().unwrap();

View File

@@ -88,11 +88,11 @@ class App extends React.Component<IProps, IState> {
bgLoc && this.setState({
bgFile: bgLoc
})
}, this.forceUpdate)
}
} else this.setState({
bgFile: custom_bg
})
}, this.forceUpdate)
if (!cert_generated) {
// Generate the certificate
@@ -109,15 +109,13 @@ class App extends React.Component<IProps, IState> {
isDownloading: downloadHandler.getDownloads().filter(d => d.status !== 'finished')?.length > 0
})
}, 1000)
console.log('mounting app component with background: ' + this.state.bgFile)
}
render() {
return (
<div className="App" style={
this.state.bgFile ? {
background: `url(${this.state.bgFile}) fixed`,
background: `url("${this.state.bgFile}") fixed`,
} : {}
}>
<TopBar

View File

@@ -6,6 +6,7 @@ import './Options.css'
import { setConfigOption, getConfig, getConfigOption } from '../../../utils/configuration'
import Checkbox from '../common/Checkbox'
import Divider from './Divider'
import { invoke } from '@tauri-apps/api'
interface IProps {
closeFn: () => void;
@@ -80,8 +81,14 @@ export default class Options extends React.Component<IProps, IState> {
})
}
setCustomBackground(value: string) {
async setCustomBackground(value: string) {
setConfigOption('customBackground', value)
// Copy the file over to the local directory
invoke('copy_file', {
path: value.replace(/\\/g, '/'),
newPath: await invoke('get_local_bg_path')
})
}
render() {