fixed multi-chunk downloads

This commit is contained in:
DecDuck
2024-10-25 10:28:58 +11:00
parent 4779383fc9
commit 2ec351f20e
5 changed files with 21 additions and 11 deletions

View File

@ -3,8 +3,8 @@ use crate::db::DatabaseImpls;
use crate::downloads::manifest::DropDownloadContext;
use crate::DB;
use log::info;
use std::{fs::OpenOptions, io::{BufWriter, Seek, SeekFrom, Write}};
use urlencoding::encode;
use std::io::{BufWriter, Seek, SeekFrom, Write};
pub fn download_game_chunk(ctx: DropDownloadContext) {
let base_url = DB.fetch_base_url();
@ -14,7 +14,10 @@ pub fn download_game_chunk(ctx: DropDownloadContext) {
.join(&format!(
"/api/v1/client/chunk?id={}&version={}&name={}&chunk={}",
// Encode the parts we don't trust
ctx.game_id, encode(&ctx.version), encode(&ctx.file_name), ctx.index
ctx.game_id,
encode(&ctx.version),
encode(&ctx.file_name),
ctx.index
))
.unwrap();
@ -26,16 +29,15 @@ pub fn download_game_chunk(ctx: DropDownloadContext) {
.send()
.unwrap();
let mut file_lock = ctx.file.lock().unwrap();
let mut file = OpenOptions::new().write(true).open(ctx.path).unwrap();
if ctx.offset != 0 {
file_lock
file
.seek(SeekFrom::Start(ctx.offset))
.expect("Failed to seek to file offset");
}
let mut stream = BufWriter::with_capacity(1024 * 1024, file_lock.try_clone().unwrap());
drop(file_lock);
let mut stream = BufWriter::with_capacity(1024 * 1024, file);
response.copy_to(&mut stream).unwrap();
}