1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! # warp-embed
//!
//! Serve [embedded file](https://crates.io/crates/rust-embed) with [warp](https://crates.io/crates/warp)
//!
//! ```
//! use warp::Filter;
//! use rust_embed::RustEmbed;
//!
//! #[derive(RustEmbed)]
//! #[folder = "data"]
//! struct Data;
//!
//! let data_serve = warp_embed::embed(&Data);
//! ```

use std::borrow::Cow;
use std::sync::Arc;
use warp::filters::path::{FullPath, Tail};
use warp::http::Uri;
use warp::{reject::Rejection, reply::Reply, reply::Response, Filter};

/// Embed serving configuration
#[derive(Debug, Clone)]
pub struct EmbedConfig {
    /// list of directory index.
    ///
    /// Default value is `vec!["index.html".to_string(), "index.htm".to_string()]`
    pub directory_index: Vec<String>,
}

impl Default for EmbedConfig {
    fn default() -> Self {
        EmbedConfig {
            directory_index: vec!["index.html".to_string(), "index.htm".to_string()],
        }
    }
}

struct EmbedFile {
    data: Cow<'static, [u8]>,
}

impl Reply for EmbedFile {
    fn into_response(self) -> Response {
        Response::new(self.data.into())
    }
}

fn append_filename(path: &str, filename: &str) -> String {
    if path.is_empty() {
        filename.to_string()
    } else {
        format!("{}/{}", path, filename)
    }
}

/// Creates a `Filter` that always serves one embedded file
pub fn embed_one<A: rust_embed::RustEmbed>(
    _: &A,
    filename: &str,
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
    let filename = Arc::new(filename.to_string());
    warp::any()
        .map(move || filename.clone())
        .and_then(|filename: Arc<String>| async move {
            if let Some(x) = A::get(&filename) {
                Ok(create_reply(x, &filename))
            } else {
                Err(warp::reject::not_found())
            }
        })
}

/// Creates a `Filter` that serves embedded files at the base `path` joined by the request path.
pub fn embed<A: rust_embed::RustEmbed>(
    x: &A,
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
    embed_with_config(x, EmbedConfig::default())
}

#[derive(Debug)]
struct NotFound {
    config: Arc<EmbedConfig>,
    tail: Tail,
    full: FullPath,
}

impl warp::reject::Reject for NotFound {}

/// Creates a `Filter` that serves embedded files at the base `path` joined
/// by the request path with configuration.
pub fn embed_with_config<A: rust_embed::RustEmbed>(
    _: &A,
    config: EmbedConfig,
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
    let config = Arc::new(config);
    let config2 = config.clone();
    let direct_serve = warp::path::tail().and_then(|tail: Tail| async move {
        if let Some(x) = A::get(tail.as_str()) {
            Ok(create_reply(x, tail.as_str()))
        } else {
            Err(warp::reject::not_found())
        }
    });

    let directory_index = warp::any()
        .map(move || config.clone())
        .and(warp::path::tail())
        .and(warp::path::full())
        .and_then(
            |config: Arc<EmbedConfig>, tail: Tail, full: FullPath| async move {
                //eprintln!("directory index: {:?} {:?}", tail, full);
                for one in config.directory_index.iter() {
                    if full.as_str().ends_with('/') {
                        let filepath = format!("{}{}", tail.as_str(), one);
                        if let Some(x) = A::get(&filepath) {
                            return Ok(create_reply(x, one));
                        }
                    }
                }

                Err(warp::reject::not_found())
            },
        );

    let redirect = warp::any()
        .map(move || config2.clone())
        .and(warp::path::tail())
        .and(warp::path::full())
        .and_then(
            |config: Arc<EmbedConfig>, tail: Tail, full: FullPath| async move {
                for one in config.directory_index.iter() {
                    if A::get(&append_filename(tail.as_str(), one)).is_some()
                        && !full.as_str().ends_with('/')
                    {
                        let new_path = format!("{}/", full.as_str());
                        return Ok(warp::redirect(
                            Uri::builder()
                                .path_and_query(new_path.as_str())
                                .build()
                                .unwrap(),
                        ));
                    }
                }

                Err(warp::reject::not_found())
            },
        );

    warp::any()
        .and(direct_serve)
        .or(directory_index)
        .or(redirect)
}

fn create_reply(data: Cow<'static, [u8]>, actual_name: &str) -> impl Reply {
    let suggest = mime_guess::guess_mime_type(actual_name);
    warp::reply::with_header(EmbedFile { data }, "Content-Type", suggest.to_string())
}

#[cfg(test)]
mod test;