♻️ refactor: flatten out indentation
parent
ac289e18f7
commit
e5bf5f7d1f
|
@ -119,6 +119,8 @@ async fn serve_file (
|
|||
)
|
||||
-> Result <Response, FileServerError>
|
||||
{
|
||||
use range::Parsed::*;
|
||||
|
||||
let file_md = file.metadata ().await.map_err (FileServerError::CantGetFileMetadata)?;
|
||||
|
||||
#[cfg (unix)]
|
||||
|
@ -134,41 +136,40 @@ async fn serve_file (
|
|||
|
||||
let range_header = headers.get ("range").and_then (|v| std::str::from_utf8 (v).ok ());
|
||||
|
||||
Ok (match range::check (range_header, file_len) {
|
||||
range::Parsed::NotSatisfiable (file_len) => Response::RangeNotSatisfiable (file_len),
|
||||
range::Parsed::Valid (range) => {
|
||||
if uri.query () == Some ("as_markdown") {
|
||||
const MAX_BUF_SIZE: u32 = 1_000_000;
|
||||
|
||||
if range.range_requested {
|
||||
return Ok (Response::InvalidQuery);
|
||||
}
|
||||
|
||||
if file_len > MAX_BUF_SIZE.into () {
|
||||
Response::MarkdownErr (markdown::Error::TooBig)
|
||||
}
|
||||
else {
|
||||
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);
|
||||
|
||||
match render_styled (&buffer) {
|
||||
Ok (x) => Response::MarkdownPreview (x),
|
||||
Err (x) => Response::MarkdownErr (x),
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
let file = file.into ();
|
||||
|
||||
Response::ServeFile (ServeFileParams {
|
||||
file,
|
||||
send_body,
|
||||
range,
|
||||
})
|
||||
}
|
||||
},
|
||||
})
|
||||
let range = match range::check (range_header, file_len) {
|
||||
NotSatisfiable (file_len) => return Ok (Response::RangeNotSatisfiable (file_len)),
|
||||
Valid (range) => range,
|
||||
};
|
||||
|
||||
if uri.query () == Some ("as_markdown") {
|
||||
const MAX_BUF_SIZE: u32 = 1_000_000;
|
||||
|
||||
if range.range_requested {
|
||||
return Ok (Response::InvalidQuery);
|
||||
}
|
||||
|
||||
if file_len > MAX_BUF_SIZE.into () {
|
||||
return Ok (Response::MarkdownErr (markdown::Error::TooBig));
|
||||
}
|
||||
else {
|
||||
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);
|
||||
|
||||
return Ok (match render_styled (&buffer) {
|
||||
Ok (x) => Response::MarkdownPreview (x),
|
||||
Err (x) => Response::MarkdownErr (x),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let file = file.into ();
|
||||
|
||||
Ok (Response::ServeFile (ServeFileParams {
|
||||
file,
|
||||
send_body,
|
||||
range,
|
||||
}))
|
||||
}
|
||||
|
||||
async fn serve_api (
|
||||
|
|
Loading…
Reference in New Issue