Finished Admin UI

This commit is contained in:
Samuel Walker 2024-07-14 08:44:16 -06:00
parent c39fcfe66b
commit e8583d3a05
7 changed files with 65 additions and 13 deletions

View File

@ -11,7 +11,7 @@ edition = "2021"
tauri-build = { version = "1", features = [] }
[dependencies]
tauri = { version = "1", features = [ "dialog-open", "updater", "dialog-ask", "shell-open"] }
tauri = { version = "1", features = [ "process-all", "dialog-open", "updater", "dialog-ask", "shell-open"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
suppaftp = { version = "6.0.1", features = ["native-tls"] }
@ -26,6 +26,7 @@ reqwest = { version = "0.12.5", features = ["stream"] }
futures-util = "0.3.30"
ssh2 = "0.9.4"
chrono = "0.4.38"
zip = "2.1.3"
[features]
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!

View File

@ -1,7 +1,13 @@
use std::io::Cursor;
use std::io::Read;
use std::fs::File;
use std::io::Seek;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use crate::modpack::get_modpacks;
use crate::modpack::get_versions;
use crate::modpack::VersionEntry;
use crate::sftp;
use crate::modpack;
@ -9,6 +15,11 @@ use crate::ModpackEntry;
use ssh2::Sftp;
use ssh2::Session;
use chrono;
use zip::unstable::write::FileOptionsExt;
use zip::write::FileOptions;
use zip::write::SimpleFileOptions;
use zip::ZipArchive;
use zip::ZipWriter;
//static USERNAME: parking_lot::Mutex<String> = parking_lot::const_mutex(String::new());
//static PASSWORD: parking_lot::Mutex<String> = parking_lot::const_mutex(String::new());
@ -125,6 +136,50 @@ pub async fn remove_pack(id: String){
}
#[tauri::command]
pub async fn update_pack(id: String, path: String, version: String){
pub async fn update_pack(window: tauri::Window, id: String, path: String, version: String) -> Result<(), String>{
println!("Update modpack {}, to version {}, from file {}", id, version, path);
let file = File::open(Path::new(path.as_str())).or(Err(format!("Unable to open file")))?;
let mut archive = ZipArchive::new(file).or(Err(format!("File not a zip archive!")))?;
let mut buf = Cursor::new(vec![]);
let mut out_archive = ZipWriter::new(&mut buf);
for i in 0..archive.len() {
let mut file = archive.by_index(i).or(Err(format!("error reading archive")))?;
if(file.name() == "overrides/version.txt"){
continue;
}
out_archive.start_file(file.name(), SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated));
std::io::copy(&mut file, &mut out_archive);
}
out_archive.start_file("overrides/version.txt", SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated));
out_archive.write_all(version.as_bytes());
out_archive.finish();
buf.rewind().unwrap();
let timestamp = format!("{:?}", chrono::offset::Utc::now());
let path = format!("Versions/{}-{}.mrpack", id, timestamp);
{
let ref mut session = *SESSION.lock().await;
if let Some(session) = session{
let size = buf.clone().bytes().count();
let upload_path = format!("/ftp/{}/{}", id, path.clone());
println!("Uploading to {}", upload_path);
sftp::uplaod(Some(window), session.clone(), PathBuf::from(upload_path), &mut buf, path.clone(), size).await;
}
}
let mut versions = get_versions(id.clone()).await?;
versions.push(VersionEntry{Version: version, Date: timestamp.clone(), File: path});
update_versions(id.clone(), versions).await;
let mut modpacks = get_modpacks().await;
let mut index = 0;
for mut pack in modpacks.as_slice(){
if pack.id == id {
modpacks[index].last_updated = timestamp;
break;
}
index += 1;
}
update_modpacks(modpacks).await;
Ok(())
}

View File

@ -26,7 +26,7 @@ pub async fn uplaod(window: Option<tauri::Window>, sess: Session, path: PathBuf,
let mut file = sftp.create(path.as_path()).or(Err("Unable to open file"))?;
let mut uploaded = 0;
while true {
let mut buf = vec![0u8; 1024];
let mut buf = vec![0u8; 1024*32];
let res = reader.read(&mut buf).unwrap();
if res <= 0 {
if let Some(window) = window.clone() {

View File

@ -6,7 +6,7 @@
},
"package": {
"productName": "FCLauncher",
"version": "1.0.3"
"version": "1.0.4"
},
"tauri": {
"updater": {
@ -27,6 +27,9 @@
"all": false,
"ask": true,
"open": true
},
"process": {
"all": true
}
},
"windows": [

View File

@ -22,7 +22,6 @@
<div class="progressFinished"></div>
</div>
<div class="container-horizontal" data-bs-theme="dark">
<div id="modpacks" >
</div>

View File

@ -13,18 +13,11 @@ const download_progress = listen("download_progress", (progress) => {
//console.log("Downloaded "+progress.payload.downloaded/(1024*1024) +"MB / " + progress.payload.total/(1024*1024) + "MB");
let downProgress = (progress.payload.downloaded/(1024*1024)).toFixed(2);
let downTotal = (progress.payload.total/(1024*1024)).toFixed(2);
document.getElementById("download_name").textContent = "Downloading "+progress.payload.download_name;
document.getElementById("download_progress").textContent = downProgress + "MB / "+downTotal+"MB";
document.getElementById("launchGame").disabled = true;
downBar.style.width = `${(progress.payload.downloaded / progress.payload.total) * 100}%`;
document.querySelector(".progress").style.visibility = "visible";
});
const download_finished = listen("download_finished", (event) => {
document.getElementById("download_name").textContent = "";
document.getElementById("download_progress").textContent = "";
document.getElementById("launchGame").disabled = false;
document.getElementById("launchGame").textContent ="Launch Minecraft";
downBar.style.width = 0;
document.querySelector(".progress").style.visibility = "hidden";
});

View File

@ -1,6 +1,7 @@
const { invoke } = window.__TAURI__.tauri;
const { listen } = window.__TAURI__.event;
const { ask } = window.__TAURI__.dialog;
const { exit } = window.__TAURI__.process;
const downBar = document.querySelector(".progressFinished");
//import { listen } from '@tauri-apps/api';
@ -85,7 +86,7 @@ function gameLaunch() {
document.getElementById("launchGame").disabled = true;
document.getElementById("launchGame").textContent ="Launching...";
//TODO Launch Game
invoke("launch_modpack", { id: selectedId}).then(window.close);
invoke("launch_modpack", { id: selectedId}).then(() => { exit(1); });
}