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
+7 -8
View File
@@ -1,5 +1,6 @@
use std::fs::File; use std::fs::File;
use std::io::{Seek, SeekFrom, Write}; use std::io::{Seek, SeekFrom, Write};
use std::os::unix::fs::MetadataExt;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use log::info; use log::info;
use uuid::Bytes; use uuid::Bytes;
@@ -13,8 +14,6 @@ pub fn download_game_chunk(ctx: DropDownloadContext) {
info!("Downloading game chunk"); info!("Downloading game chunk");
let base_url = DB.fetch_base_url(); let base_url = DB.fetch_base_url();
let index = ctx.index;
let chunk = ctx.file_chunk;
let client = reqwest::blocking::Client::new(); let client = reqwest::blocking::Client::new();
let chunk_url = base_url.join( let chunk_url = base_url.join(
@@ -23,7 +22,7 @@ pub fn download_game_chunk(ctx: DropDownloadContext) {
ctx.game_id, ctx.game_id,
ctx.version, ctx.version,
ctx.file_name, ctx.file_name,
index ctx.index
)).unwrap(); )).unwrap();
let header = generate_authorization_header(); let header = generate_authorization_header();
@@ -35,16 +34,16 @@ pub fn download_game_chunk(ctx: DropDownloadContext) {
.unwrap(); .unwrap();
let response_data = response.bytes().unwrap(); let response_data = response.bytes().unwrap();
info!("Writing data to chunk at offset {}", CHUNK_SIZE * ctx.index as u64);
write_to_file(ctx.file, CHUNK_SIZE * index as u64, response_data.to_vec()); write_to_file(ctx.file, ctx.index as u64, response_data.to_vec());
// Need to implement actual download logic // 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(); let mut lock = file.lock().unwrap();
if offset != 0 { if index != 0 {
lock.seek(SeekFrom::Start(offset)).expect("Failed to seek to file offset"); lock.seek(SeekFrom::Start(index * CHUNK_SIZE)).expect("Failed to seek to file offset");
} }
lock.write_all(&data).unwrap(); lock.write_all(&data).unwrap();
+10 -15
View File
@@ -6,7 +6,7 @@ use log::info;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{AppState, DB}; use crate::{AppState, DB};
use crate::auth::generate_authorization_header; 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::download_files;
use crate::downloads::manifest::{DropDownloadContext, DropManifest}; use crate::downloads::manifest::{DropDownloadContext, DropManifest};
use crate::downloads::progress::ProgressChecker; use crate::downloads::progress::ProgressChecker;
@@ -109,7 +109,7 @@ impl GameDownload {
} }
let manifest_download = response.json::<DropManifest>().await.unwrap(); let manifest_download = response.json::<DropManifest>().await.unwrap();
info!("{:?}", manifest_download); info!("Manifest: {:?}", manifest_download);
if let Ok(mut manifest) = self.manifest.lock() { if let Ok(mut manifest) = self.manifest.lock() {
*manifest = Some(manifest_download) *manifest = Some(manifest_download)
} else { return Err(GameDownloadError::System(SystemError::MutexLockFailed)); } } 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> { pub fn to_contexts(manifest: &DropManifest, version: String, game_id: String) -> Vec<DropDownloadContext> {
let mut contexts = Vec::new(); let mut contexts = Vec::new();
let mut counter = 0; let base_path = DATA_ROOT_DIR.clone();
let mut prev_key: String = String::new();
for key in manifest { for key in manifest {
let path = Path::new(key.0); let path = base_path.join(Path::new(key.0));
if !path.exists() { let file = Arc::new(Mutex::new(File::create(path).unwrap()));
let file = File::create(path).unwrap(); for i in 0..key.1.ids.len() {
contexts.push(DropDownloadContext { contexts.push(DropDownloadContext {
file_chunk: Arc::new(key.1.clone()), file_chunk: Arc::new(key.1.clone()),
file_name: key.0.clone(), file_name: key.0.clone(),
version: version.to_string(), version: version.to_string(),
index: counter, index: i,
game_id: game_id.to_string(), 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 contexts
} }