♻️ refactor: move Markdown support in ptth_server out to a feature.
It may be removed in the future.main
parent
e5bf5f7d1f
commit
0821ccaac6
|
@ -12,7 +12,7 @@ anyhow = "1.0.38"
|
||||||
arc-swap = "1.2.0"
|
arc-swap = "1.2.0"
|
||||||
handlebars = "3.5.3"
|
handlebars = "3.5.3"
|
||||||
http = "0.2.1"
|
http = "0.2.1"
|
||||||
hyper = "0.14.4"
|
hyper = { version = "0.14.4", features = ["http1", "server", "stream"] }
|
||||||
serde = {version = "1.0.124", features = ["derive"]}
|
serde = {version = "1.0.124", features = ["derive"]}
|
||||||
structopt = "0.3.21"
|
structopt = "0.3.21"
|
||||||
tokio = { version = "1.2.0", features = [] }
|
tokio = { version = "1.2.0", features = [] }
|
||||||
|
@ -23,3 +23,7 @@ uom = "0.30.0"
|
||||||
|
|
||||||
ptth_core = { path = "../ptth_core" }
|
ptth_core = { path = "../ptth_core" }
|
||||||
ptth_server = { path = "../ptth_server" }
|
ptth_server = { path = "../ptth_server" }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
|
||||||
|
markdown = ["ptth_server/markdown"]
|
||||||
|
|
|
@ -24,7 +24,7 @@ heim = { version = "0.1.0-rc.1", features = ["process"] }
|
||||||
http = "0.2.1"
|
http = "0.2.1"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
percent-encoding = "2.1.0"
|
percent-encoding = "2.1.0"
|
||||||
pulldown-cmark = "0.8.0"
|
pulldown-cmark = { version = "0.8.0", optional = true }
|
||||||
rand = "0.8.3"
|
rand = "0.8.3"
|
||||||
regex = "1.4.1"
|
regex = "1.4.1"
|
||||||
reqwest = { version = "0.11.1", features = ["stream"] }
|
reqwest = { version = "0.11.1", features = ["stream"] }
|
||||||
|
@ -48,3 +48,7 @@ ptth_core = { path = "../ptth_core", version = "1.2.0" }
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
||||||
maplit = "1.0.2"
|
maplit = "1.0.2"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
|
||||||
|
markdown = ["pulldown-cmark"]
|
||||||
|
|
|
@ -37,11 +37,15 @@ use crate::{
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
errors::FileServerError,
|
errors::FileServerError,
|
||||||
markdown,
|
|
||||||
markdown::render_styled,
|
|
||||||
range,
|
range,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[cfg (feature = "markdown")]
|
||||||
|
use super::markdown::{
|
||||||
|
self,
|
||||||
|
render_styled,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive (Debug, PartialEq)]
|
#[derive (Debug, PartialEq)]
|
||||||
pub enum OutputFormat {
|
pub enum OutputFormat {
|
||||||
Json,
|
Json,
|
||||||
|
@ -76,10 +80,27 @@ pub enum Response {
|
||||||
ServeDir (ServeDirParams),
|
ServeDir (ServeDirParams),
|
||||||
ServeFile (ServeFileParams),
|
ServeFile (ServeFileParams),
|
||||||
|
|
||||||
MarkdownErr (markdown::Error),
|
MarkdownErr (MarkdownErrWrapper),
|
||||||
MarkdownPreview (String),
|
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 (
|
fn serve_dir (
|
||||||
path_s: &str,
|
path_s: &str,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
|
@ -141,25 +162,28 @@ async fn serve_file (
|
||||||
Valid (range) => range,
|
Valid (range) => range,
|
||||||
};
|
};
|
||||||
|
|
||||||
if uri.query () == Some ("as_markdown") {
|
#[cfg (feature = "markdown")]
|
||||||
const MAX_BUF_SIZE: u32 = 1_000_000;
|
{
|
||||||
|
if uri.query () == Some ("as_markdown") {
|
||||||
if range.range_requested {
|
const MAX_BUF_SIZE: u32 = 1_000_000;
|
||||||
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) {
|
if range.range_requested {
|
||||||
Ok (x) => Response::MarkdownPreview (x),
|
return Ok (Response::InvalidQuery);
|
||||||
Err (x) => Response::MarkdownErr (x),
|
}
|
||||||
});
|
|
||||||
|
if file_len > MAX_BUF_SIZE.into () {
|
||||||
|
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")];
|
||||||
|
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 (MarkdownErrWrapper::new (x)),
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -48,6 +48,7 @@ pub mod metrics;
|
||||||
|
|
||||||
mod html;
|
mod html;
|
||||||
mod internal;
|
mod internal;
|
||||||
|
#[cfg(feature = "markdown")]
|
||||||
mod markdown;
|
mod markdown;
|
||||||
mod range;
|
mod range;
|
||||||
|
|
||||||
|
@ -279,14 +280,21 @@ pub async fn serve_all (
|
||||||
range,
|
range,
|
||||||
}) => serve_file (file.into_inner (), send_body, range).await?,
|
}) => serve_file (file.into_inner (), send_body, range).await?,
|
||||||
MarkdownErr (e) => {
|
MarkdownErr (e) => {
|
||||||
use markdown::Error::*;
|
#[cfg (feature = "markdown")]
|
||||||
let code = match &e {
|
{
|
||||||
TooBig => StatusCode::InternalServerError,
|
use markdown::Error::*;
|
||||||
//NotMarkdown => serve_error (StatusCode::BadRequest, "File is not Markdown"),
|
let e = e.inner;
|
||||||
NotUtf8 => StatusCode::BadRequest,
|
let code = match &e {
|
||||||
};
|
TooBig => StatusCode::InternalServerError,
|
||||||
|
//NotMarkdown => serve_error (StatusCode::BadRequest, "File is not Markdown"),
|
||||||
|
NotUtf8 => StatusCode::BadRequest,
|
||||||
|
};
|
||||||
|
|
||||||
|
return Ok (serve_error (code, e.to_string ()));
|
||||||
|
}
|
||||||
|
|
||||||
serve_error (code, e.to_string ())
|
#[cfg (not (feature = "markdown"))]
|
||||||
|
serve_error (StatusCode::BadRequest, "Markdown feature is disabled")
|
||||||
},
|
},
|
||||||
MarkdownPreview (s) => html::serve (s),
|
MarkdownPreview (s) => html::serve (s),
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue