Implement new GC API & rename to zipUtils

This commit is contained in:
KingRainbow44
2022-05-22 23:32:31 -04:00
parent 3554c98339
commit 8383fcfd1f
6 changed files with 15 additions and 7 deletions

View File

@@ -134,12 +134,12 @@ async fn get_bg_file(bg_path: String) -> String {
} }
}; };
let file_name = response_data.backgroundFile.to_string() + ".png"; let file_name = response_data.bg_file.to_string();
// First we see if the file already exists in our local bg folder // First we see if the file already exists in our local bg folder
if file_helpers::dir_exists(format!(".\\bg\\{}", file_name).as_str()) { if file_helpers::dir_exists(format!(".\\bg\\{}", file_name).as_str()) {
let cwd = std::env::current_dir().unwrap(); let cwd = std::env::current_dir().unwrap();
return format!("{}\\{}", cwd.display(), response_data.backgroundFile.as_str()); return format!("{}\\{}", cwd.display(), response_data.bg_file.as_str());
} }
// Now we check if the bg folder, which is one directory above the game_path, exists. // Now we check if the bg folder, which is one directory above the game_path, exists.
@@ -159,16 +159,16 @@ async fn get_bg_file(bg_path: String) -> String {
// The image exists, lets copy it to our local \bg folder // 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\\{}", file_name.as_str());
match std::fs::copy(bg_img_path, bg_img_path_local) { return match std::fs::copy(bg_img_path, bg_img_path_local) {
Ok(_) => { Ok(_) => {
// Copy was successful, lets return true // Copy was successful, lets return true
let cwd = std::env::current_dir().unwrap(); let cwd = std::env::current_dir().unwrap();
return format!("{}\\{}", cwd.display(), response_data.backgroundFile.as_str()); format!("{}\\{}", cwd.display(), response_data.bg_file.as_str())
} }
Err(e) => { Err(e) => {
// Copy failed, lets return false // Copy failed, lets return false
println!("Failed to copy background image: {}", e); println!("Failed to copy background image: {}", e);
return "".to_string(); "".to_string()
} }
}; };
} }

View File

@@ -4,5 +4,5 @@ use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
pub(crate) struct APIQuery { pub(crate) struct APIQuery {
pub backgroundFile: String, pub bg_file: String,
} }

View File

@@ -2,7 +2,7 @@ import React from 'react'
import Menu from './Menu' import Menu from './Menu'
import Tr from '../../../utils/language' import Tr from '../../../utils/language'
import DownloadHandler from '../../../utils/download' import DownloadHandler from '../../../utils/download'
import { unzip } from '../../../utils/zip_utils' import { unzip } from '../../../utils/zipUtils'
import BigButton from '../common/BigButton' import BigButton from '../common/BigButton'
import { dataDir } from '@tauri-apps/api/path' import { dataDir } from '@tauri-apps/api/path'

View File

@@ -4,6 +4,7 @@ import React from 'react'
import Tr from '../../../utils/language' import Tr from '../../../utils/language'
import './NewsSection.css' import './NewsSection.css'
import {base64Decode} from '../../../utils/string'
interface IProps { interface IProps {
selected?: string; selected?: string;
@@ -55,6 +56,9 @@ export default class NewsSection extends React.Component<IProps, IState> {
if (!obj.commits) { if (!obj.commits) {
const commits: string = await invoke('req_get', { url: 'https://api.github.com/repos/Grasscutters/Grasscutter/commits' }) const commits: string = await invoke('req_get', { url: 'https://api.github.com/repos/Grasscutters/Grasscutter/commits' })
obj = JSON.parse(commits) obj = JSON.parse(commits)
} else {
const commitData = JSON.parse(base64Decode(obj.commits))
obj = commitData.gc_stable
} }
// Probably rate-limited // Probably rate-limited

View File

@@ -8,4 +8,8 @@ export function byteToString(bytes: number) {
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)).toString(), 10) const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)).toString(), 10)
if (i === 0) return `${bytes} ${sizes[i]}` if (i === 0) return `${bytes} ${sizes[i]}`
return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${sizes[i]}` return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${sizes[i]}`
}
export function base64Decode(str: string) {
return Buffer.from(str, 'base64').toString('utf8')
} }