diff --git a/crates/ptth_server/src/file_server/mod.rs b/crates/ptth_server/src/file_server/mod.rs index 74aec33..b27b95b 100644 --- a/crates/ptth_server/src/file_server/mod.rs +++ b/crates/ptth_server/src/file_server/mod.rs @@ -7,7 +7,7 @@ use std::{ borrow::Cow, cmp::min, collections::HashMap, - convert::{Infallible, TryInto}, + convert::{Infallible, TryFrom, TryInto}, error::Error, fmt::Debug, io::SeekFrom, @@ -319,28 +319,30 @@ async fn serve_file ( loop { let mut buffer = vec! [0_u8; 65_536]; - let bytes_read: u64 = f.read (&mut buffer).await.unwrap ().try_into ().unwrap (); - - let bytes_read = min (bytes_left, bytes_read); - - buffer.truncate (bytes_read.try_into ().unwrap ()); + let bytes_read = f.read (&mut buffer).await.expect ("Couldn't read from file"); if bytes_read == 0 { break; } + buffer.truncate (bytes_read); + + let bytes_read_64 = u64::try_from (bytes_read).expect ("Couldn't fit usize into u64"); + + let bytes_read_64 = min (bytes_left, bytes_read_64); + if tx.send (Ok::<_, Infallible> (buffer)).await.is_err () { warn! ("Cancelling file stream (Sent {} out of {} bytes)", bytes_sent, content_length); break; } - bytes_left -= bytes_read; + bytes_left -= bytes_read_64; if bytes_left == 0 { debug! ("Finished"); break; } - bytes_sent += bytes_read; + bytes_sent += bytes_read_64; while next_mark <= bytes_sent { trace! ("Sent {} bytes", next_mark); next_mark += mark_interval; @@ -558,11 +560,11 @@ async fn internal_serve_all ( ParsedRange::Ok (range) => { if uri.query () == Some ("as_markdown") { const MAX_BUF_SIZE: u32 = 1_000_000; - if file_len > MAX_BUF_SIZE.try_into ().unwrap () { + if file_len > MAX_BUF_SIZE.into () { MarkdownErr (MarkdownError::TooBig) } else { - let mut buffer = vec! [0_u8; MAX_BUF_SIZE.try_into ().unwrap ()]; + let mut buffer = vec! [0_u8; MAX_BUF_SIZE.try_into ().expect ("Couldn't fit u32 into usize")]; let bytes_read = file.read (&mut buffer).await?; buffer.truncate (bytes_read);