summaryrefslogtreecommitdiffstats
path: root/src/utils.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs51
1 files changed, 23 insertions, 28 deletions
diff --git a/src/utils.rs b/src/utils.rs
index 68ad4d8..cf91b37 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -4,55 +4,50 @@ use std::io::{self, Read};
use reqwest::Client;
use reqwest::header::Connection;
+use failure::Fail;
use error::FrippyError;
-pub fn download(max_kib: usize, url: &str) -> Result<String, FrippyError> {
+/// Downloads the file and converts it to a String.
+/// Any invalid bytes are converted to a replacement character.
+///
+/// The error indicated either a failed download or that the DownloadLimit was reached
+pub fn download(url: &str, max_kib: Option<usize>) -> Result<String, FrippyError> {
let mut response = Client::new().get(url).header(Connection::close()).send()?;
- let mut body = String::new();
-
// 100 kibibyte buffer
let mut buf = [0; 100 * 1024];
let mut written = 0;
- let mut vec = Vec::new();
- let mut end_of_valid = None;
+ let mut bytes = Vec::new();
// Read until we reach EOF or max_kib KiB
loop {
- if let Some(eov) = end_of_valid {
- vec = vec[..eov].to_vec();
- }
-
let len = match response.read(&mut buf) {
Ok(0) => break,
Ok(len) => len,
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue,
Err(e) => Err(e)?,
};
- vec.extend_from_slice(&buf);
-
- end_of_valid = None;
- let body_slice = match str::from_utf8(&vec[..len]) {
- Ok(slice) => slice,
- Err(e) => {
- let valid = e.valid_up_to();
- if valid == 0 {
- Err(e)?;
- }
- end_of_valid = Some(valid);
-
- str::from_utf8(&buf[..valid])?
- }
- };
- body.push_str(body_slice);
+ bytes.extend_from_slice(&buf);
written += len;
// Check if the file is too large to download
- if written > max_kib * 1024 {
- Err(FrippyError::DownloadLimit { limit: max_kib })?;
+ if let Some(max_kib) = max_kib {
+ if written > max_kib * 1024 {
+ Err(FrippyError::DownloadLimit { limit: max_kib })?;
+ }
}
}
- Ok(body)
+ Ok(String::from_utf8_lossy(&bytes).into_owned())
+}
+
+
+pub fn log_error(e: FrippyError) {
+ let mut causes = e.causes();
+
+ error!("{}", causes.next().unwrap());
+ for cause in causes {
+ error!("caused by: {}", cause);
+ }
}