Refactor util to be just about serving files
This commit is contained in:
parent
fb4a33685b
commit
4c63370a26
|
@ -3,17 +3,13 @@
|
|||
//! ⚠️ Docs still under construction & API not yet stable ⚠️
|
||||
#![allow(missing_docs)]
|
||||
|
||||
#[cfg(feature="serve_dir")]
|
||||
use std::path::{Path, PathBuf};
|
||||
#[cfg(feature="serve_dir")]
|
||||
use tokio::{
|
||||
fs::{self, File},
|
||||
io,
|
||||
};
|
||||
#[cfg(feature="serve_dir")]
|
||||
use crate::types::Response;
|
||||
|
||||
#[cfg(feature="serve_dir")]
|
||||
pub async fn serve_file<P: AsRef<Path>>(path: P, mime: &str) -> Response {
|
||||
let path = path.as_ref();
|
||||
|
||||
|
@ -31,7 +27,6 @@ pub async fn serve_file<P: AsRef<Path>>(path: P, mime: &str) -> Response {
|
|||
Response::success(mime, file)
|
||||
}
|
||||
|
||||
#[cfg(feature="serve_dir")]
|
||||
pub async fn serve_dir<D: AsRef<Path>, P: AsRef<Path>>(dir: D, virtual_path: &[P]) -> Response {
|
||||
debug!("Dir: {}", dir.as_ref().display());
|
||||
let dir = dir.as_ref();
|
||||
|
@ -85,7 +80,6 @@ pub async fn serve_dir<D: AsRef<Path>, P: AsRef<Path>>(dir: D, virtual_path: &[P
|
|||
serve_dir_listing(path, virtual_path).await
|
||||
}
|
||||
|
||||
#[cfg(feature="serve_dir")]
|
||||
async fn serve_dir_listing<P: AsRef<Path>, B: AsRef<Path>>(path: P, virtual_path: &[B]) -> Response {
|
||||
let mut dir = match fs::read_dir(path.as_ref()).await {
|
||||
Ok(dir) => dir,
|
||||
|
@ -126,7 +120,6 @@ async fn serve_dir_listing<P: AsRef<Path>, B: AsRef<Path>>(path: P, virtual_path
|
|||
document.into()
|
||||
}
|
||||
|
||||
#[cfg(feature="serve_dir")]
|
||||
pub fn guess_mime_from_path<P: AsRef<Path>>(path: P) -> &'static str {
|
||||
let path = path.as_ref();
|
||||
let extension = path.extension().and_then(|s| s.to_str());
|
||||
|
@ -142,7 +135,6 @@ pub fn guess_mime_from_path<P: AsRef<Path>>(path: P) -> &'static str {
|
|||
mime_guess::from_ext(extension).first_raw().unwrap_or("application/octet-stream")
|
||||
}
|
||||
|
||||
#[cfg(feature="serve_dir")]
|
||||
/// Print a warning to the log asking to file an issue and respond with "Unexpected Error"
|
||||
pub (crate) fn warn_unexpected(err: impl std::fmt::Debug, path: &Path, line: u32) -> Response {
|
||||
warn!(
|
||||
|
@ -157,19 +149,3 @@ pub (crate) fn warn_unexpected(err: impl std::fmt::Debug, path: &Path, line: u32
|
|||
);
|
||||
Response::temporary_failure("Unexpected error")
|
||||
}
|
||||
|
||||
/// A convenience trait alias for `AsRef<T> + Into<T::Owned>`,
|
||||
/// most commonly used to accept `&str` or `String`:
|
||||
///
|
||||
/// `Cowy<str>` ⇔ `AsRef<str> + Into<String>`
|
||||
pub trait Cowy<T>
|
||||
where
|
||||
Self: AsRef<T> + Into<T::Owned>,
|
||||
T: ToOwned + ?Sized,
|
||||
{}
|
||||
|
||||
impl<C, T> Cowy<T> for C
|
||||
where
|
||||
C: AsRef<T> + Into<T::Owned>,
|
||||
T: ToOwned + ?Sized,
|
||||
{}
|
|
@ -235,7 +235,8 @@ use rustls::Session;
|
|||
|
||||
mod types;
|
||||
mod handling;
|
||||
pub mod util;
|
||||
#[cfg(feature = "serve_dir")]
|
||||
pub mod files;
|
||||
pub mod routing;
|
||||
#[cfg(feature = "ratelimiting")]
|
||||
mod ratelimiting;
|
||||
|
|
Loading…
Reference in a new issue