I think that downloads are working. Need to test and set decent file locations now

This commit is contained in:
quexeky
2024-10-24 17:18:09 +11:00
parent 984472ec01
commit f388237132
3 changed files with 38 additions and 13 deletions

View File

@ -1,10 +1,14 @@
use std::fs::File;
use std::io::{Seek, SeekFrom, Write};
use std::sync::{Arc, Mutex};
use log::info; use log::info;
use uuid::Bytes;
use crate::auth::generate_authorization_header; use crate::auth::generate_authorization_header;
use crate::DB; use crate::DB;
use crate::db::DatabaseImpls; use crate::db::DatabaseImpls;
use crate::downloads::manifest::DropDownloadContext; use crate::downloads::manifest::DropDownloadContext;
const CHUNK_SIZE: u64 = 1024 * 1024 * 64;
pub fn download_game_chunk(ctx: DropDownloadContext) { 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();
@ -29,6 +33,19 @@ pub fn download_game_chunk(ctx: DropDownloadContext) {
.header("Authorization", header) .header("Authorization", header)
.send() .send()
.unwrap(); .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 // Need to implement actual download logic
}
fn write_to_file(file: Arc<Mutex<File>>, offset: 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");
}
lock.write_all(&data).unwrap();
} }

View File

@ -1,3 +1,5 @@
use std::fs::File;
use std::path::Path;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::sync::atomic::AtomicUsize; use std::sync::atomic::AtomicUsize;
use log::info; use log::info;
@ -125,14 +127,19 @@ pub fn to_contexts(manifest: &DropManifest, version: String, game_id: String) ->
let mut counter = 0; let mut counter = 0;
let mut prev_key: String = String::new(); let mut prev_key: String = String::new();
for key in manifest { for key in manifest {
contexts.push(DropDownloadContext { let path = Path::new(key.0);
file_chunk: Arc::new(key.1.clone()), if !path.exists() {
let file = File::create(path).unwrap();
contexts.push(DropDownloadContext {
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: counter,
game_id: game_id.to_string(), game_id: game_id.to_string(),
}); file: Arc::new(Mutex::new(file)),
});
}
if prev_key == *key.0 { if prev_key == *key.0 {
counter += 1; counter += 1;
} else { } else {
@ -154,7 +161,6 @@ pub async fn start_game_download(
let download = Arc::new(GameDownload::new(game_id.clone(), game_version.clone())); let download = Arc::new(GameDownload::new(game_id.clone(), game_version.clone()));
download.ensure_manifest_exists().await?; download.ensure_manifest_exists().await?;
let local_manifest = { let local_manifest = {

View File

@ -1,5 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::fs::File;
use std::sync::{Arc, Mutex};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub type DropManifest = HashMap<String, DropChunk>; pub type DropManifest = HashMap<String, DropChunk>;
@ -11,11 +12,12 @@ pub struct DropChunk {
pub lengths: Vec<usize>, pub lengths: Vec<usize>,
} }
#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, Eq, PartialEq)] #[derive(Debug, Clone)]
pub struct DropDownloadContext { pub struct DropDownloadContext {
pub file_chunk: Arc<DropChunk>, pub file_chunk: Arc<DropChunk>,
pub file_name: String, pub file_name: String,
pub version: String, pub version: String,
pub index: usize, pub index: usize,
pub game_id: String pub game_id: String,
pub file: Arc<Mutex<File>>
} }