2016-04-19 19:10:26 +03:00
|
|
|
use nickel::{Continue, Middleware, MiddlewareResult, Request, Response};
|
|
|
|
use photosdir::PhotosDir;
|
2017-02-04 21:12:13 +04:00
|
|
|
use plugin::Extensible;
|
2016-04-19 19:10:26 +03:00
|
|
|
use std::path::PathBuf;
|
2017-02-04 21:12:13 +04:00
|
|
|
use typemap::Key;
|
2016-04-19 19:10:26 +03:00
|
|
|
|
|
|
|
pub struct PhotosDirMiddleware {
|
|
|
|
dir: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PhotosDirMiddleware {
|
2016-05-04 14:16:20 +03:00
|
|
|
pub fn new(dir: PathBuf) -> Self {
|
2018-07-15 16:34:55 +04:00
|
|
|
PhotosDirMiddleware { dir }
|
2016-04-19 19:10:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 14:16:20 +03:00
|
|
|
impl Key for PhotosDirMiddleware {
|
|
|
|
type Value = PhotosDir;
|
|
|
|
}
|
2016-04-19 19:10:26 +03:00
|
|
|
|
|
|
|
impl<D> Middleware<D> for PhotosDirMiddleware {
|
2017-11-09 00:17:42 +04:00
|
|
|
fn invoke<'mw, 'conn>(
|
|
|
|
&self,
|
|
|
|
req: &mut Request<'mw, 'conn, D>,
|
|
|
|
res: Response<'mw, D>,
|
|
|
|
) -> MiddlewareResult<'mw, D> {
|
2016-05-04 14:16:20 +03:00
|
|
|
req.extensions_mut()
|
2017-02-04 21:12:13 +04:00
|
|
|
.insert::<PhotosDirMiddleware>(PhotosDir::new(self.dir.clone()));
|
2016-04-19 19:10:26 +03:00
|
|
|
Ok(Continue(res))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait PhotosDirRequestExtensions {
|
|
|
|
fn photos(&self) -> &PhotosDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, D> PhotosDirRequestExtensions for Request<'a, 'b, D> {
|
|
|
|
fn photos(&self) -> &PhotosDir {
|
2018-07-06 01:28:57 +04:00
|
|
|
self.extensions().get::<PhotosDirMiddleware>().unwrap()
|
2016-04-19 19:10:26 +03:00
|
|
|
}
|
|
|
|
}
|