chore: Progress on caching

This commit is contained in:
quexeky
2025-01-29 16:55:14 +11:00
parent e204ff30b4
commit 810fbdfe49
7 changed files with 953 additions and 68 deletions

926
src-tauri/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -50,6 +50,11 @@ slice-deque = "0.3.0"
throttle_my_fn = "0.2.6"
parking_lot = "0.12.3"
atomic-instant-full = "0.1.0"
cacache = "13.1.0"
bincode = "1.3.3"
http-serde = "2.1.1"
reqwest-middleware = "0.4.0"
reqwest-middleware-cache = "0.1.1"
[dependencies.tauri]
version = "2.1.1"

View File

@ -98,9 +98,10 @@ pub struct Database {
pub base_url: String,
pub applications: DatabaseApplications,
pub prev_database: Option<PathBuf>,
pub cache_dir: PathBuf
}
impl Database {
fn new<T: Into<PathBuf>>(games_base_dir: T, prev_database: Option<PathBuf>) -> Self {
fn new<T: Into<PathBuf>>(games_base_dir: T, prev_database: Option<PathBuf>, cache_dir: PathBuf) -> Self {
Self {
applications: DatabaseApplications {
install_dirs: vec![games_base_dir.into()],
@ -116,6 +117,7 @@ impl Database {
autostart: false,
max_download_threads: 4,
},
cache_dir,
}
}
}
@ -150,21 +152,23 @@ impl DatabaseImpls for DatabaseInterface {
let db_path = data_root_dir.join("drop.db");
let games_base_dir = data_root_dir.join("games");
let logs_root_dir = data_root_dir.join("logs");
let cache_dir = data_root_dir.join("cache/");
debug!("creating data directory at {:?}", data_root_dir);
create_dir_all(data_root_dir.clone()).unwrap();
create_dir_all(games_base_dir.clone()).unwrap();
create_dir_all(logs_root_dir.clone()).unwrap();
create_dir_all(&games_base_dir).unwrap();
create_dir_all(&logs_root_dir).unwrap();
create_dir_all(&cache_dir).unwrap();
let exists = fs::exists(db_path.clone()).unwrap();
match exists {
true => match PathDatabase::load_from_path(db_path.clone()) {
Ok(db) => db,
Err(e) => handle_invalid_database(e, db_path, games_base_dir),
Err(e) => handle_invalid_database(e, db_path, games_base_dir, cache_dir),
},
false => {
let default = Database::new(games_base_dir, None);
let default = Database::new(games_base_dir, None, cache_dir);
debug!(
"Creating database at path {}",
db_path.as_os_str().to_str().unwrap()
@ -204,6 +208,7 @@ fn handle_invalid_database(
_e: RustbreakError,
db_path: PathBuf,
games_base_dir: PathBuf,
cache_dir: PathBuf
) -> rustbreak::Database<Database, rustbreak::backend::PathBackend, DropDatabaseSerializer> {
let new_path = {
let time = Utc::now().timestamp();
@ -220,6 +225,7 @@ fn handle_invalid_database(
let db = Database::new(
games_base_dir.into_os_string().into_string().unwrap(),
Some(new_path),
cache_dir
);
PathDatabase::create_at_path(db_path, db).expect("Database could not be created")

View File

@ -69,7 +69,7 @@ pub struct StatsUpdateEvent {
pub fn fetch_library_logic(app: AppHandle) -> Result<Vec<Game>, RemoteAccessError> {
let header = generate_authorization_header();
let client = reqwest::blocking::Client::new();
let client = reqwest::blocking::Client::builder().;
let response = make_request(&client, &["/api/v1/client/user/library"], &[], |f| {
f.header("Authorization", header)
})?

View File

@ -1,3 +1,4 @@
mod cache;
mod database;
mod games;

View File

@ -0,0 +1,70 @@
use std::collections::HashMap;
use http::Version;
use reqwest::blocking::{Request, RequestBuilder, Response};
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Debug, Deserialize, Serialize)]
struct StoredResponse {
body: Vec<u8>,
headers: HashMap<String, String>,
status: u16,
url: Url,
}
// HTTP version enum in the http crate does not support serde, hence the modified copy.
#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
enum HttpVersion {
#[serde(rename = "HTTP/0.9")]
Http09,
#[serde(rename = "HTTP/1.0")]
Http10,
#[serde(rename = "HTTP/1.1")]
Http11,
#[serde(rename = "HTTP/2.0")]
H2,
#[serde(rename = "HTTP/3.0")]
H3,
}
impl From<HttpVersion> for Version {
fn from(value: HttpVersion) -> Self {
match value {
HttpVersion::Http09 => Version::HTTP_09,
HttpVersion::Http10 => Version::HTTP_10,
HttpVersion::Http11 => Version::HTTP_11,
HttpVersion::H2 => Version::HTTP_2,
HttpVersion::H3 => Version::HTTP_3,
}
}
}
impl From<Version> for HttpVersion {
fn from(value: Version) -> Self {
match value {
Version::HTTP_09 => HttpVersion::Http09,
Version::HTTP_10 => HttpVersion::Http10,
Version::HTTP_11 => HttpVersion::Http11,
Version::HTTP_2 => HttpVersion::H2,
Version::HTTP_3 => HttpVersion::H3,
_ => unreachable!()
}
}
}
pub trait Cache {
fn send_cache(req: RequestBuilder) -> Result<Response, reqwest::Error>;
}
impl Cache for Request {
fn send_cache(req: RequestBuilder) -> Result<Response, reqwest::Error> {
let res = req.send()?;
let mut headers = HashMap::new();
for header in res.headers() {
headers.insert(header.0.as_str().to_owned(), header.1.to_str().unwrap().to_owned());
}
let status = res.status().as_u16();
let url = res.url().clone();
let version: HttpVersion = res.version().into();
let body: Vec<u8> = res.bytes()?.to_vec();
}
}

View File

@ -1,4 +1,5 @@
pub mod auth;
pub mod cache;
pub mod commands;
pub mod remote;
pub mod requests;