fix downloads

pull/6/head
dvkt 5 years ago
parent 06d5bf1b9d
commit 782e156625

@ -1,5 +1,5 @@
use gopher; use gopher;
use std::io::{BufWriter, Read, Result, Write}; use std::io::{Read, Result, Write};
use std::net::TcpStream; use std::net::TcpStream;
use std::net::ToSocketAddrs; use std::net::ToSocketAddrs;
use std::os::unix::fs::OpenOptionsExt; use std::os::unix::fs::OpenOptionsExt;
@ -135,22 +135,21 @@ pub fn download_url(url: &str) -> Result<(String, usize)> {
.and_then(|mut stream| { .and_then(|mut stream| {
stream.set_read_timeout(Some(TCP_TIMEOUT_DURATION))?; stream.set_read_timeout(Some(TCP_TIMEOUT_DURATION))?;
let file = std::fs::OpenOptions::new() let mut file = std::fs::OpenOptions::new()
.write(true) .write(true)
.create(true) .create(true)
.truncate(true) .truncate(true)
.mode(0o770) .mode(0o770)
.open(path)?; .open(path)?;
let mut file_buffer = BufWriter::new(file); let mut buf = [0; 1024];
let mut buf = [0 as u8; 8]; // read 8 bytes at a time
let mut bytes = 0; let mut bytes = 0;
while let Ok(count) = stream.read(&mut buf) { while let Ok(count) = stream.read(&mut buf) {
if count == 0 { if count == 0 {
break; break;
} }
bytes += count; bytes += count;
file_buffer.write_all(&buf); file.write(&buf[..count]);
if let Some(Ok(termion::event::Key::Ctrl('c'))) = keys.next() { if let Some(Ok(termion::event::Key::Ctrl('c'))) = keys.next() {
return Err(error!("Download canceled")); return Err(error!("Download canceled"));
} }

Loading…
Cancel
Save