Downloads should be fixed now

This commit is contained in:
quexeky
2024-10-24 19:38:58 +11:00
parent f388237132
commit 403ca65f7a
2 changed files with 17 additions and 23 deletions

View File

@ -1,5 +1,6 @@
use std::fs::File;
use std::io::{Seek, SeekFrom, Write};
use std::os::unix::fs::MetadataExt;
use std::sync::{Arc, Mutex};
use log::info;
use uuid::Bytes;
@ -13,8 +14,6 @@ pub fn download_game_chunk(ctx: DropDownloadContext) {
info!("Downloading game chunk");
let base_url = DB.fetch_base_url();
let index = ctx.index;
let chunk = ctx.file_chunk;
let client = reqwest::blocking::Client::new();
let chunk_url = base_url.join(
@ -23,7 +22,7 @@ pub fn download_game_chunk(ctx: DropDownloadContext) {
ctx.game_id,
ctx.version,
ctx.file_name,
index
ctx.index
)).unwrap();
let header = generate_authorization_header();
@ -35,16 +34,16 @@ pub fn download_game_chunk(ctx: DropDownloadContext) {
.unwrap();
let response_data = response.bytes().unwrap();
write_to_file(ctx.file, CHUNK_SIZE * index as u64, response_data.to_vec());
info!("Writing data to chunk at offset {}", CHUNK_SIZE * ctx.index as u64);
write_to_file(ctx.file, ctx.index as u64, response_data.to_vec());
// Need to implement actual download logic
}
fn write_to_file(file: Arc<Mutex<File>>, offset: u64, data: Vec<u8>) {
fn write_to_file(file: Arc<Mutex<File>>, index: u64, data: Vec<u8>) {
let mut lock = file.lock().unwrap();
if offset != 0 {
lock.seek(SeekFrom::Start(offset)).expect("Failed to seek to file offset");
if index != 0 {
lock.seek(SeekFrom::Start(index * CHUNK_SIZE)).expect("Failed to seek to file offset");
}
lock.write_all(&data).unwrap();

View File

@ -6,7 +6,7 @@ use log::info;
use serde::{Deserialize, Serialize};
use crate::{AppState, DB};
use crate::auth::generate_authorization_header;
use crate::db::DatabaseImpls;
use crate::db::{DatabaseImpls, DATA_ROOT_DIR};
use crate::downloads::download_files;
use crate::downloads::manifest::{DropDownloadContext, DropManifest};
use crate::downloads::progress::ProgressChecker;
@ -109,7 +109,7 @@ impl GameDownload {
}
let manifest_download = response.json::<DropManifest>().await.unwrap();
info!("{:?}", manifest_download);
info!("Manifest: {:?}", manifest_download);
if let Ok(mut manifest) = self.manifest.lock() {
*manifest = Some(manifest_download)
} else { return Err(GameDownloadError::System(SystemError::MutexLockFailed)); }
@ -124,29 +124,24 @@ impl GameDownload {
}
pub fn to_contexts(manifest: &DropManifest, version: String, game_id: String) -> Vec<DropDownloadContext> {
let mut contexts = Vec::new();
let mut counter = 0;
let mut prev_key: String = String::new();
let base_path = DATA_ROOT_DIR.clone();
for key in manifest {
let path = Path::new(key.0);
if !path.exists() {
let file = File::create(path).unwrap();
let path = base_path.join(Path::new(key.0));
let file = Arc::new(Mutex::new(File::create(path).unwrap()));
for i in 0..key.1.ids.len() {
contexts.push(DropDownloadContext {
file_chunk: Arc::new(key.1.clone()),
file_name: key.0.clone(),
version: version.to_string(),
index: counter,
index: i,
game_id: game_id.to_string(),
file: Arc::new(Mutex::new(file)),
file: file.clone(),
});
}
if prev_key == *key.0 {
counter += 1;
} else {
prev_key = key.0.clone();
counter = 0;
}
}
info!("Contexts: {:?}", contexts);
contexts
}