Skip to content

Commit

Permalink
fix: data stored in ram causing overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Phill030 committed Mar 27, 2024
1 parent 88f3880 commit 5d7eb93
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/http/http_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use futures::StreamExt;
use quickxml_to_serde::Config;
use serde::Deserialize;
use std::{collections::HashMap, path::PathBuf};
use tokio::{fs::OpenOptions, io::AsyncWriteExt};

#[derive(Debug, Clone)]
pub struct HttpRequest {
Expand Down Expand Up @@ -197,12 +198,8 @@ impl HttpRequest {
if !path.exists() {
match request_file(format!("{}/{}", &url_cloned, &wad.filename)).await {
Ok(res) => {
if let Ok(bytes) = res.bytes().await {
write_to_file(&path, &bytes.to_vec()).await.unwrap();
log::info!("[✔] Fetched {}", wad.filename);
} else {
log::warn!("[❌] Could not convert response to bytes");
}
write_to_file_chunked(&path, res).await.unwrap();
log::info!("[✔] Fetched {}", wad.filename);
}
Err(why) => {
log::warn!("[❌] Could not fetch {}, {}", wad.filename, why);
Expand Down Expand Up @@ -242,14 +239,7 @@ impl HttpRequest {
if !path.exists() {
match request_file(format!("{}/{}", &url_cloned, &util.filename)).await {
Ok(res) => {
let bytes = res
.bytes()
.await
.expect("Could not convert to bytes!")
.to_vec();

write_to_file(&path, &bytes).await.unwrap();

write_to_file_chunked(&path, res).await.unwrap();
log::info!("[✔] Fetched {}", util.filename);
}
Err(why) => {
Expand Down Expand Up @@ -294,6 +284,20 @@ async fn write_to_file(path: &PathBuf, content: &Vec<u8>) -> std::io::Result<()>
Ok(())
}

async fn write_to_file_chunked(
path: &PathBuf,
mut response: reqwest::Response,
) -> std::io::Result<()> {
tokio::fs::create_dir_all(&path.parent().unwrap()).await?;
let mut file = tokio::fs::File::create(&path).await?;

while let Some(chunk) = response.chunk().await.unwrap() {
file.write_all(&chunk).await?;
}

Ok(())
}

//? I'm Crazy HAHAHAHA 🥰😎🤓🤠😩
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
Expand Down

0 comments on commit 5d7eb93

Please sign in to comment.