♻️ refactor: move Markdown support in ptth_server out to a feature.

It may be removed in the future.
main
_ 2021-04-03 15:17:28 +00:00
parent e5bf5f7d1f
commit 0821ccaac6
4 changed files with 70 additions and 30 deletions

View File

@ -12,7 +12,7 @@ anyhow = "1.0.38"
arc-swap = "1.2.0"
handlebars = "3.5.3"
http = "0.2.1"
hyper = "0.14.4"
hyper = { version = "0.14.4", features = ["http1", "server", "stream"] }
serde = {version = "1.0.124", features = ["derive"]}
structopt = "0.3.21"
tokio = { version = "1.2.0", features = [] }
@ -23,3 +23,7 @@ uom = "0.30.0"
ptth_core = { path = "../ptth_core" }
ptth_server = { path = "../ptth_server" }
[features]
markdown = ["ptth_server/markdown"]

View File

@ -24,7 +24,7 @@ heim = { version = "0.1.0-rc.1", features = ["process"] }
http = "0.2.1"
lazy_static = "1.4.0"
percent-encoding = "2.1.0"
pulldown-cmark = "0.8.0"
pulldown-cmark = { version = "0.8.0", optional = true }
rand = "0.8.3"
regex = "1.4.1"
reqwest = { version = "0.11.1", features = ["stream"] }
@ -48,3 +48,7 @@ ptth_core = { path = "../ptth_core", version = "1.2.0" }
[dev-dependencies]
maplit = "1.0.2"
[features]
markdown = ["pulldown-cmark"]

View File

@ -37,11 +37,15 @@ use crate::{
use super::{
errors::FileServerError,
markdown,
markdown::render_styled,
range,
};
#[cfg (feature = "markdown")]
use super::markdown::{
self,
render_styled,
};
#[derive (Debug, PartialEq)]
pub enum OutputFormat {
Json,
@ -76,10 +80,27 @@ pub enum Response {
ServeDir (ServeDirParams),
ServeFile (ServeFileParams),
MarkdownErr (markdown::Error),
MarkdownErr (MarkdownErrWrapper),
MarkdownPreview (String),
}
#[cfg (feature = "markdown")]
#[derive (Debug, PartialEq)]
pub struct MarkdownErrWrapper {
pub inner: markdown::Error,
}
#[cfg (feature = "markdown")]
impl MarkdownErrWrapper {
fn new (inner: markdown::Error) -> Self {
Self { inner }
}
}
#[cfg (not (feature = "markdown"))]
#[derive (Debug, PartialEq)]
pub struct MarkdownErrWrapper {}
fn serve_dir (
path_s: &str,
path: &Path,
@ -141,6 +162,8 @@ async fn serve_file (
Valid (range) => range,
};
#[cfg (feature = "markdown")]
{
if uri.query () == Some ("as_markdown") {
const MAX_BUF_SIZE: u32 = 1_000_000;
@ -149,7 +172,7 @@ async fn serve_file (
}
if file_len > MAX_BUF_SIZE.into () {
return Ok (Response::MarkdownErr (markdown::Error::TooBig));
return Ok (Response::MarkdownErr (MarkdownErrWrapper::new (markdown::Error::TooBig)));
}
else {
let mut buffer = vec! [0_u8; MAX_BUF_SIZE.try_into ().expect ("Couldn't fit u32 into usize")];
@ -158,10 +181,11 @@ async fn serve_file (
return Ok (match render_styled (&buffer) {
Ok (x) => Response::MarkdownPreview (x),
Err (x) => Response::MarkdownErr (x),
Err (x) => Response::MarkdownErr (MarkdownErrWrapper::new (x)),
});
}
}
}
let file = file.into ();

View File

@ -48,6 +48,7 @@ pub mod metrics;
mod html;
mod internal;
#[cfg(feature = "markdown")]
mod markdown;
mod range;
@ -279,14 +280,21 @@ pub async fn serve_all (
range,
}) => serve_file (file.into_inner (), send_body, range).await?,
MarkdownErr (e) => {
#[cfg (feature = "markdown")]
{
use markdown::Error::*;
let e = e.inner;
let code = match &e {
TooBig => StatusCode::InternalServerError,
//NotMarkdown => serve_error (StatusCode::BadRequest, "File is not Markdown"),
NotUtf8 => StatusCode::BadRequest,
};
serve_error (code, e.to_string ())
return Ok (serve_error (code, e.to_string ()));
}
#[cfg (not (feature = "markdown"))]
serve_error (StatusCode::BadRequest, "Markdown feature is disabled")
},
MarkdownPreview (s) => html::serve (s),
})