mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-14 00:31:33 +10:00
I think that downloads are working. Need to test and set decent file locations now
This commit is contained in:
@ -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<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();
|
||||
}
|
||||
@ -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,6 +127,9 @@ 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 {
|
||||
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()),
|
||||
|
||||
@ -132,7 +137,9 @@ pub fn to_contexts(manifest: &DropManifest, version: String, game_id: String) ->
|
||||
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 = {
|
||||
|
||||
@ -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<String, DropChunk>;
|
||||
@ -11,11 +12,12 @@ pub struct DropChunk {
|
||||
pub lengths: Vec<usize>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DropDownloadContext {
|
||||
pub file_chunk: Arc<DropChunk>,
|
||||
pub file_name: String,
|
||||
pub version: String,
|
||||
pub index: usize,
|
||||
pub game_id: String
|
||||
pub game_id: String,
|
||||
pub file: Arc<Mutex<File>>
|
||||
}
|
||||
Reference in New Issue
Block a user