Skip to content

Commit

Permalink
Use reader for better IO
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernthedev committed Nov 5, 2023
1 parent 60df77e commit 7c6b772
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
17 changes: 8 additions & 9 deletions src/network/agent.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::{sync, time::Duration};

use color_eyre::{
eyre::{Context},
Result,
};

use color_eyre::{eyre::Context, Result};
use itertools::Itertools;
use std::io::Read;

use crate::models::config::get_combine_config;

Expand Down Expand Up @@ -32,16 +30,17 @@ where
let request = get_agent().get(url).timeout(Duration::MAX);

// non-200 status codes are raised as errors
let response = request.call()
let response = request
.call()
.with_context(|| format!("Unable to download file {url}"))?;
// .error_for_status()?;
// .error_for_status()?;
// if response.status() == ureq::OrAnyStatus::NOT_FOUND {
// bail!("Not found!");
// }

let reader = response.into_string()?;
let reader = response.into_reader();

Ok(reader.into_bytes())
Ok(reader.bytes().try_collect()?)

// TODO: Fix
// let mut bytes = Vec::with_capacity(response.content_length().unwrap_or(0) as usize);
Expand Down
2 changes: 1 addition & 1 deletion src/repository/qpackages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl QPMRepository {
let resp = get_agent()
.post(&url)
.set("Authorization", auth)
.send_json(&package)
.send_json(package)
.or_any_status()?;

if resp.status() == 403 {
Expand Down
15 changes: 8 additions & 7 deletions src/utils/git.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
fs::File,
io::{BufReader, Write},
io::{self, BufReader},
path::Path,
process::{Command, Stdio},
};
Expand All @@ -13,6 +13,7 @@ use owo_colors::OwoColorize;
//use duct::cmd;
use serde::{Deserialize, Serialize};


use crate::{models::config::get_keyring, network::agent::get_agent};

pub fn check_git() -> Result<()> {
Expand Down Expand Up @@ -56,11 +57,10 @@ pub fn get_release(url: &str, out: &std::path::Path) -> Result<bool> {

pub fn get_release_without_token(url: &str, out: &std::path::Path) -> Result<bool> {
let mut file = File::create(out).context("create so file failed")?;
let reader = get_agent().get(url).call()?.into_string()?;

file.write_all(reader.as_bytes()).with_context(|| {
let mut reader = get_agent().get(url).call()?.into_reader();
io::copy(&mut reader, &mut file).with_context(|| {
format!(
"Failed to write to file {}",
"Failed to write out downloaded bytes to {}",
out.as_os_str().to_string_lossy()
)
})?;
Expand Down Expand Up @@ -105,8 +105,9 @@ pub fn get_release_with_token(url: &str, out: &std::path::Path, token: &str) ->

let mut file = File::create(out).context("create so file failed")?;

let res = get_agent().get(&download).call()?.into_string()?;
file.write_all(res.as_bytes()).with_context(|| {
let mut reader = get_agent().get(&download).call()?.into_reader();

io::copy(&mut reader, &mut file).with_context(|| {
format!(
"Failed to write out downloaded bytes to {}",
out.as_os_str().to_string_lossy()
Expand Down

0 comments on commit 7c6b772

Please sign in to comment.