From f388237132ee5316c900b68e876de204b26525db Mon Sep 17 00:00:00 2001 From: quexeky Date: Thu, 24 Oct 2024 17:18:09 +1100 Subject: [PATCH] I think that downloads are working. Need to test and set decent file locations now --- src-tauri/src/downloads/download_files.rs | 21 +++++++++++++++++++-- src-tauri/src/downloads/game_download.rs | 22 ++++++++++++++-------- src-tauri/src/downloads/manifest.rs | 8 +++++--- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src-tauri/src/downloads/download_files.rs b/src-tauri/src/downloads/download_files.rs index 44df3d4..cd50d9b 100644 --- a/src-tauri/src/downloads/download_files.rs +++ b/src-tauri/src/downloads/download_files.rs @@ -1,10 +1,14 @@ +use std::fs::File; +use std::io::{Seek, SeekFrom, Write}; +use std::sync::{Arc, Mutex}; use log::info; +use uuid::Bytes; use crate::auth::generate_authorization_header; use crate::DB; use crate::db::DatabaseImpls; use crate::downloads::manifest::DropDownloadContext; - +const CHUNK_SIZE: u64 = 1024 * 1024 * 64; pub fn download_game_chunk(ctx: DropDownloadContext) { info!("Downloading game chunk"); let base_url = DB.fetch_base_url(); @@ -29,6 +33,19 @@ pub fn download_game_chunk(ctx: DropDownloadContext) { .header("Authorization", header) .send() .unwrap(); - println!("Response text: {}", response.text().unwrap()); + let response_data = response.bytes().unwrap(); + + + write_to_file(ctx.file, CHUNK_SIZE * index as u64, response_data.to_vec()); // Need to implement actual download logic +} + +fn write_to_file(file: Arc>, offset: u64, data: Vec) { + let mut lock = file.lock().unwrap(); + + if offset != 0 { + lock.seek(SeekFrom::Start(offset)).expect("Failed to seek to file offset"); + } + + lock.write_all(&data).unwrap(); } \ No newline at end of file diff --git a/src-tauri/src/downloads/game_download.rs b/src-tauri/src/downloads/game_download.rs index c928f09..f1c7289 100644 --- a/src-tauri/src/downloads/game_download.rs +++ b/src-tauri/src/downloads/game_download.rs @@ -1,3 +1,5 @@ +use std::fs::File; +use std::path::Path; use std::sync::{Arc, Mutex}; use std::sync::atomic::AtomicUsize; use log::info; @@ -125,14 +127,19 @@ pub fn to_contexts(manifest: &DropManifest, version: String, game_id: String) -> let mut counter = 0; let mut prev_key: String = String::new(); for key in manifest { - contexts.push(DropDownloadContext { - file_chunk: Arc::new(key.1.clone()), + let path = Path::new(key.0); + if !path.exists() { + let file = File::create(path).unwrap(); + contexts.push(DropDownloadContext { + file_chunk: Arc::new(key.1.clone()), - file_name: key.0.clone(), - version: version.to_string(), - index: counter, - game_id: game_id.to_string(), - }); + file_name: key.0.clone(), + version: version.to_string(), + index: counter, + game_id: game_id.to_string(), + file: Arc::new(Mutex::new(file)), + }); + } if prev_key == *key.0 { counter += 1; } else { @@ -154,7 +161,6 @@ pub async fn start_game_download( let download = Arc::new(GameDownload::new(game_id.clone(), game_version.clone())); - download.ensure_manifest_exists().await?; let local_manifest = { diff --git a/src-tauri/src/downloads/manifest.rs b/src-tauri/src/downloads/manifest.rs index 74dd3db..a646179 100644 --- a/src-tauri/src/downloads/manifest.rs +++ b/src-tauri/src/downloads/manifest.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; -use std::sync::Arc; +use std::fs::File; +use std::sync::{Arc, Mutex}; use serde::{Deserialize, Serialize}; pub type DropManifest = HashMap; @@ -11,11 +12,12 @@ pub struct DropChunk { pub lengths: Vec, } -#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, Eq, PartialEq)] +#[derive(Debug, Clone)] pub struct DropDownloadContext { pub file_chunk: Arc, pub file_name: String, pub version: String, pub index: usize, - pub game_id: String + pub game_id: String, + pub file: Arc> } \ No newline at end of file