♻️ Remove unwraps from file server module

main
_ 2020-11-29 19:59:47 +00:00
parent f212931842
commit eada65d94b
1 changed files with 12 additions and 10 deletions

View File

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