Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 115 additions & 57 deletions .github/copilot-instructions.md

Large diffs are not rendered by default.

108 changes: 94 additions & 14 deletions src-tauri/src/core/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ use serde::{Deserialize, Serialize};
use sha1::Digest as Sha1Digest;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
use tauri::{AppHandle, Emitter, Manager, Window};
use tokio::io::{AsyncSeekExt, AsyncWriteExt};
use tokio::sync::Semaphore;
use ts_rs::TS;

static DOWNLOAD_QUEUE_FILE_LOCK: <()> = Mutex::new(());

#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "downloader.ts")]
Expand Down Expand Up @@ -84,15 +87,17 @@ pub struct DownloadQueue {
}

impl DownloadQueue {
/// Load download queue from file
pub fn load(app_handle: &AppHandle) -> Self {
let queue_path = app_handle
fn queue_path(app_handle: &AppHandle) -> Result<PathBuf, String> {
app_handle
.path()
.app_data_dir()
.unwrap()
.join("download_queue.json");
.map(|dir| dir.join("download_queue.json"))
.map_err(|e| e.to_string())
}

fn load_from_path(queue_path: &PathBuf) -> Self {
if queue_path.exists() {
if let Ok(content) = std::fs::read_to_string(&queue_path) {
if let Ok(content) = std::fs::read_to_string(queue_path) {
if let Ok(queue) = serde_json::from_str(&content) {
return queue;
}
Expand All @@ -101,18 +106,93 @@ impl DownloadQueue {
Self::default()
}

/// Save download queue to file
pub fn save(&self, app_handle: &AppHandle) -> Result<(), String> {
let queue_path = app_handle
.path()
.app_data_dir()
.unwrap()
.join("download_queue.json");
fn save_atomic_to_path(&self, queue_path: &PathBuf) -> Result<(), String> {
let parent = queue_path
.parent()
.ok_or_else(|| "Download queue path has no parent directory".to_string())?;
std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;

let content = serde_json::to_string_pretty(self).map_err(|e| e.to_string())?;
std::fs::write(&queue_path, content).map_err(|e| e.to_string())?;
let tmp_path = queue_path.with_extension("json.tmp");
std::fs::write(&tmp_path, content).map_err(|e| e.to_string())?;

if queue_path.exists() {
let _ = std::fs::remove_file(queue_path);
}

std::fs::rename(&tmp_path, queue_path).map_err(|e| e.to_string())?;
Ok(())
}

pub fn update<T, F>(app_handle: &AppHandle, mutator: F) -> Result<T, String>
where
F: FnOnce(&mut Self) -> T,
{
let _guard = DOWNLOAD_QUEUE_FILE_LOCK
.lock()
.map_err(|_| "Download queue lock poisoned".to_string())?;
let queue_path = Self::queue_path(app_handle)?;
let mut queue = Self::load_from_path(&queue_path);
let result = mutator(&mut queue);
queue.save_atomic_to_path(&queue_path)?;
Ok(result)
}

pub fn list_pending(app_handle: &AppHandle) -> Vec<PendingJavaDownload> {
let _guard = match DOWNLOAD_QUEUE_FILE_LOCK.lock() {
Ok(guard) => guard,
Err(_) => return Vec::new(),
};

let queue_path = match Self::queue_path(app_handle) {
Ok(path) => path,
Err(_) => return Vec::new(),
};

Self::load_from_path(&queue_path).pending_downloads
}

pub fn add_pending(
app_handle: &AppHandle,
download: PendingJavaDownload,
) -> Result<(), String> {
Self::update(app_handle, |queue| queue.add(download)).map(|_| ())
}

pub fn remove_pending(
app_handle: &AppHandle,
major_version: u32,
image_type: &str,
) -> Result<(), String> {
Self::update(app_handle, |queue| queue.remove(major_version, image_type)).map(|_| ())
}

/// Load download queue from file
#[allow(dead_code)]
pub fn load(app_handle: &AppHandle) -> Self {
let _guard = match DOWNLOAD_QUEUE_FILE_LOCK.lock() {
Ok(guard) => guard,
Err(_) => return Self::default(),
};

let queue_path = match Self::queue_path(app_handle) {
Ok(path) => path,
Err(_) => return Self::default(),
};

Self::load_from_path(&queue_path)
}

/// Save download queue to file
#[allow(dead_code)]
pub fn save(&self, app_handle: &AppHandle) -> Result<(), String> {
let _guard = DOWNLOAD_QUEUE_FILE_LOCK
.lock()
.map_err(|_| "Download queue lock poisoned".to_string())?;
let queue_path = Self::queue_path(app_handle)?;
self.save_atomic_to_path(&queue_path)
}

/// Add a pending download
pub fn add(&mut self, download: PendingJavaDownload) {
// Remove existing download for same version/type
Expand Down
77 changes: 77 additions & 0 deletions src-tauri/src/core/java/cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::core::java::error::JavaError;
use crate::core::java::JavaCatalog;
use std::path::PathBuf;
use tauri::{AppHandle, Manager};

const CACHE_DURATION_SECS: u64 = 24 * 60 * 60;

fn get_catalog_cache_path(app_handle: &AppHandle) -> PathBuf {
app_handle
.path()
.app_data_dir()
.unwrap()
.join("java_catalog_cache.json")
}

fn write_file_atomic(path: &PathBuf, content: &str) -> Result<(), JavaError> {
let parent = path.parent().ok_or_else(|| {
JavaError::InvalidConfig("Java cache path has no parent directory".to_string())
})?;
std::fs::create_dir_all(parent)?;

let tmp_path = path.with_extension("tmp");
std::fs::write(&tmp_path, content)?;

if path.exists() {
let _ = std::fs::remove_file(path);
}

std::fs::rename(&tmp_path, path)?;
Ok(())
}

pub fn load_cached_catalog_result(
app_handle: &AppHandle,
) -> Result<Option<JavaCatalog>, JavaError> {
let cache_path = get_catalog_cache_path(app_handle);
if !cache_path.exists() {
return Ok(None);
}

let content = std::fs::read_to_string(&cache_path)?;
let catalog: JavaCatalog = serde_json::from_str(&content)?;

let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_err(|e| JavaError::Other(format!("System time error: {}", e)))?
.as_secs();

if now - catalog.cached_at < CACHE_DURATION_SECS {
Ok(Some(catalog))
} else {
Ok(None)
}
}

#[allow(dead_code)]
pub fn load_cached_catalog(app_handle: &AppHandle) -> Option<JavaCatalog> {
match load_cached_catalog_result(app_handle) {
Ok(value) => value,
Err(_) => None,
}
}

pub fn save_catalog_cache(app_handle: &AppHandle, catalog: &JavaCatalog) -> Result<(), JavaError> {
let cache_path = get_catalog_cache_path(app_handle);
let content = serde_json::to_string_pretty(catalog)?;
write_file_atomic(&cache_path, &content)
}

#[allow(dead_code)]
pub fn clear_catalog_cache(app_handle: &AppHandle) -> Result<(), JavaError> {
let cache_path = get_catalog_cache_path(app_handle);
if cache_path.exists() {
std::fs::remove_file(&cache_path)?;
}
Ok(())
}
Loading