album/src/main.rs

569 lines
20 KiB
Rust
Raw Normal View History

2016-05-04 14:16:20 +03:00
#[macro_use]
extern crate nickel;
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate nickel_jwt_session;
2015-11-21 18:21:44 +03:00
extern crate rustc_serialize;
2015-11-22 15:01:39 +03:00
extern crate typemap;
extern crate plugin;
extern crate image;
extern crate hyper;
extern crate time;
2015-12-22 20:22:21 +03:00
extern crate chrono;
2016-01-31 16:34:48 +03:00
extern crate rexif;
extern crate rphotos;
extern crate r2d2;
extern crate nickel_diesel;
#[macro_use]
extern crate diesel;
extern crate r2d2_diesel;
use nickel_diesel::{DieselMiddleware, DieselRequestExtensions};
use r2d2::NopErrorHandler;
2016-05-04 14:16:20 +03:00
use chrono::Duration as ChDuration;
2015-12-25 20:32:11 +03:00
use chrono::Datelike;
2015-11-22 15:01:39 +03:00
use hyper::header::{Expires, HttpDate};
use nickel::{FormBody, HttpRouter, MediaType, MiddlewareResult, Nickel,
Request, Response, StaticFilesHandler};
use nickel::extensions::response::Redirect;
2016-05-04 14:16:20 +03:00
use nickel_jwt_session::{SessionMiddleware, SessionRequestExtensions,
SessionResponseExtensions};
use plugin::Pluggable;
use rustc_serialize::Encodable;
use time::Duration;
use nickel::status::StatusCode;
use diesel::expression::sql_literal::SqlLiteral;
use diesel::prelude::*;
use diesel::pg::PgConnection;
use chrono::naive::date::NaiveDate;
2015-11-21 18:21:44 +03:00
//mod models;
use rphotos::models::{Person, Photo, Place, Tag};
2015-12-25 20:32:11 +03:00
mod env;
use env::{dburl, env_or, jwt_key, photos_dir};
2015-11-21 18:21:44 +03:00
2015-12-26 22:18:44 +03:00
mod photosdir;
//mod rustormmiddleware;
//use rustormmiddleware::{RustormMiddleware, RustormRequestExtensions};
mod requestloggermiddleware;
use requestloggermiddleware::RequestLoggerMiddleware;
mod photosdirmiddleware;
use photosdirmiddleware::{PhotosDirMiddleware, PhotosDirRequestExtensions};
macro_rules! render {
($res:expr, $template:expr, { $($param:ident : $ptype:ty = $value:expr),* })
=>
{
{
#[derive(Debug, Clone, RustcEncodable)]
struct ParamData {
csslink: String,
$(
$param: $ptype,
)*
}
$res.render($template, &ParamData {
csslink: include!(concat!(env!("OUT_DIR"), "/stylelink")).into(),
$(
$param: $value,
)*
})
}
}
}
/*
2016-05-04 14:16:20 +03:00
fn orm_get_related<T: Entity, Src: Entity>(src: &Src,
rel_table: &str)
-> Query {
2015-12-22 20:22:21 +03:00
let mut q = Query::select();
q.only_from(&T::table());
2016-05-04 14:16:20 +03:00
q.left_join_table(rel_table,
&format!("{}.id", T::table().name),
2015-12-22 20:22:21 +03:00
&format!("{}.{}", rel_table, T::table().name))
2016-05-04 14:16:20 +03:00
.filter_eq(&format!("{}.{}", rel_table, Src::table().name), src.id());
2015-12-22 20:22:21 +03:00
q
}
*/
2015-12-22 20:22:21 +03:00
#[derive(Debug, Clone, RustcEncodable)]
2016-01-31 23:15:44 +03:00
struct Group {
title: String,
url: String,
count: i64,
photo: Photo,
}
fn monthname(n: u8) -> &'static str {
match n {
1 => "january",
2 => "february",
3 => "march",
4 => "april",
5 => "may",
6 => "june",
7 => "july",
8 => "august",
9 => "september",
10 => "october",
11 => "november",
12 => "december",
2016-05-04 14:16:20 +03:00
_ => "non-month",
2016-01-31 23:15:44 +03:00
}
}
fn main() {
env_logger::init().unwrap();
2015-11-21 18:21:44 +03:00
info!("Initalized logger");
let mut server = Nickel::new();
server.utilize(RequestLoggerMiddleware);
server.utilize(SessionMiddleware::new(&jwt_key()));
// TODO This is a "build" location, not an "install" location ...
let staticdir = concat!(env!("OUT_DIR"), "/static/");
info!("Serving static files from {}", staticdir);
server.utilize(StaticFilesHandler::new(staticdir));
let dm : DieselMiddleware<PgConnection> = DieselMiddleware::new(&dburl(),
5,
Box::new(NopErrorHandler)).unwrap();
server.utilize(dm);
server.utilize(PhotosDirMiddleware::new(photos_dir()));
server.get("/login", login);
server.post("/login", do_login);
server.get("/logout", logout);
server.get("/", all_years);
server.get("/img/:id/:size", show_image);
server.get("/tag/", tag_all);
server.get("/tag/:tag", tag_one);
server.get("/place/", place_all);
server.get("/place/:slug", place_one);
server.get("/person/", person_all);
server.get("/person/:slug", person_one);
server.get("/details/:id", photo_details);
server.get("/:year/", months_in_year);
server.get("/:year/:month/", days_in_month);
server.get("/:year/:month/:day", all_for_day);
server.listen(&*env_or("RPHOTOS_LISTEN", "127.0.0.1:6767"));
}
2016-05-04 14:16:20 +03:00
fn login<'mw>(_req: &mut Request,
mut res: Response<'mw>)
-> MiddlewareResult<'mw> {
res.clear_jwt_user();
render!(res, "templates/login.tpl", {})
}
2016-05-04 14:16:20 +03:00
fn do_login<'mw>(req: &mut Request,
mut res: Response<'mw>)
-> MiddlewareResult<'mw> {
let form_data = try_with!(res, req.form_body());
if let (Some(user), Some(password)) = (form_data.get("user"),
form_data.get("password")) {
// TODO Actual password hashing and checking
if user == "kaj" && password == "kaj123" {
res.set_jwt_user(user);
return res.redirect("/");
}
}
render!(res, "templates/login.tpl", {})
}
2016-05-04 14:16:20 +03:00
fn logout<'mw>(_req: &mut Request,
mut res: Response<'mw>)
-> MiddlewareResult<'mw> {
res.clear_jwt_user();
res.redirect("/")
}
2016-05-04 14:16:20 +03:00
fn show_image<'mw>(req: &mut Request,
mut res: Response<'mw>)
-> MiddlewareResult<'mw> {
if let Ok(the_id) = req.param("id").unwrap().parse::<i32>() {
use rphotos::schema::photos::dsl::*;
let connection = req.db_conn();
let c : &PgConnection = &connection;
if let Ok(tphoto) = photos.find(the_id).first::<Photo>(c) {
if req.authorized_user().is_some() || tphoto.is_public() {
if let Some(size) = match req.param("size").unwrap() {
"s" => Some(200),
"m" => Some(800),
"l" => Some(1200),
2016-05-04 14:16:20 +03:00
_ => None,
} {
match req.photos().get_scaled_image(tphoto, size, size) {
Ok(buf) => {
res.set(MediaType::Jpeg);
2016-05-04 14:16:20 +03:00
res.set(Expires(HttpDate(time::now() +
Duration::days(14))));
return res.send(buf);
2016-05-04 14:16:20 +03:00
}
Err(err) => {
return res.error(StatusCode::InternalServerError,
format!("{}", err));
}
}
}
2015-11-28 11:53:12 +03:00
}
}
}
res.error(StatusCode::NotFound, "No such image")
}
2016-05-04 14:16:20 +03:00
fn tag_all<'mw>(req: &mut Request,
res: Response<'mw>)
-> MiddlewareResult<'mw> {
/*
use rphotos::schema::tag::dsl::*;
let connection = req.db_conn();
let c : &PgConnection = &connection;
*/
return render!(res, "templates/tags.tpl", {
user: Option<String> = req.authorized_user()
// TODO order by tag name!
// tags: Vec<Tag> = tag.load(c).unwrap()
});
}
2016-05-04 14:16:20 +03:00
fn tag_one<'mw>(req: &mut Request,
res: Response<'mw>)
-> MiddlewareResult<'mw> {
/*
use rphotos::schema::tag::dsl::*;
let tslug = req.param("tag").unwrap();
let connection = req.db_conn();
let c : &PgConnection = &connection;
if let Ok(ttag) = tag.filter(slug.eq(tslug)).first::<Tag>(c) {
return render!(res, "templates/tag.tpl", {
user: Option<String> = req.authorized_user(),
photos: Vec<i32> = vec![], / * FIXME Vec<Photo>
orm_get_related::<Photo, Tag>(&tag, "photo_tag")
.only_public(req.authorized_user().is_none())
.desc_nulls_last("grade")
.desc_nulls_last("date")
.collect(req.db_conn()).unwrap(),* /
tag: Tag = ttag
});
}
*/
res.error(StatusCode::NotFound, "Not a tag")
}
2016-05-04 14:16:20 +03:00
fn place_all<'mw>(req: &mut Request,
res: Response<'mw>)
-> MiddlewareResult<'mw> {
/*
use rphotos::schema::place::dsl::*;
let connection = req.db_conn();
let c : &PgConnection = &connection;
*/
return render!(res, "templates/places.tpl", {
user: Option<String> = req.authorized_user()
// TODO order by place name!
// places: Vec<Place> = place.load(c).unwrap()
});
}
2016-05-04 14:16:20 +03:00
fn place_one<'mw>(req: &mut Request,
res: Response<'mw>)
-> MiddlewareResult<'mw> {
/*
let tslug = req.param("slug").unwrap();
use rphotos::schema::place::dsl::*;
let connection = req.db_conn();
let c : &PgConnection = &connection;
if let Ok(tplace) = place.filter(slug.eq(tslug)).first::<Place>(c) {
return render!(res, "templates/place.tpl", {
user: Option<String> = req.authorized_user(),
photos: Vec<i32> = vec![], / * TODO Vec<Photo> =
orm_get_related::<Photo, Place>(&place, "photo_place")
.only_public(req.authorized_user().is_none())
.desc_nulls_last("grade")
.desc_nulls_last("date")
.collect(req.db_conn()).unwrap(), * /
place: Place = tplace
});
}
*/
res.error(StatusCode::NotFound, "Not a place")
}
2016-05-04 14:16:20 +03:00
fn person_all<'mw>(req: &mut Request,
res: Response<'mw>)
-> MiddlewareResult<'mw> {
/*
use rphotos::schema::person::dsl::*;
let connection = req.db_conn();
let c : &PgConnection = &connection;
*/
return render!(res, "templates/people.tpl", {
user: Option<String> = req.authorized_user()
// TODO order by name!
//people: Vec<Person> = person.load(c).expect("list persons")
});
}
2016-05-04 14:16:20 +03:00
fn person_one<'mw>(req: &mut Request,
res: Response<'mw>)
-> MiddlewareResult<'mw> {
/*
let tslug = req.param("slug").unwrap();
use rphotos::schema::person::dsl::*;
let connection = req.db_conn();
let c : &PgConnection = &connection;
if let Ok(tperson) = person.filter(slug.eq(tslug)).first::<Person>(c) {
return render!(res, "templates/person.tpl", {
user: Option<String> = req.authorized_user(),
photos: Vec<i32> = vec![], / * TODO Vec<Photo> =
orm_get_related::<Photo, Person>(&person, "photo_person")
.only_public(req.authorized_user().is_none())
.desc_nulls_last("grade")
.desc_nulls_last("date")
.collect(req.db_conn()).unwrap(), * /
person: Person = tperson
});
}
*/
res.error(StatusCode::NotFound, "Not a place")
}
2016-05-04 14:16:20 +03:00
fn photo_details<'mw>(req: &mut Request,
res: Response<'mw>)
-> MiddlewareResult<'mw> {
if let Ok(the_id) = req.param("id").unwrap().parse::<i32>() {
use rphotos::schema::photos::dsl::*;
let connection = req.db_conn();
let c : &PgConnection = &connection;
if let Ok(tphoto) = photos.find(the_id).first::<Photo>(c) {
if req.authorized_user().is_some() || tphoto.is_public() {
2016-05-04 14:16:20 +03:00
return render!(res, "templates/details.tpl", {
user: Option<String> = req.authorized_user(),
lpath: Vec<Link> =
tphoto.date
.map(|d| vec![Link::year(d.year()),
Link::month(d.year(), d.month() as u8),
Link::day(d.year(), d.month() as u8, d.day())])
.unwrap_or_else(|| vec![]),
people: Vec<Person> = vec![],
// req.orm_get_related(&photo, "photo_person").unwrap(),
places: Vec<Place> = vec![],
// req.orm_get_related(&photo, "photo_place").unwrap(),
tags: Vec<Tag> = vec![],
// req.orm_get_related(&photo, "photo_tag").unwrap(),
time: String = match tphoto.date {
Some(d) => d.format("%T").to_string(),
None => "".to_string()
},
year: Option<i32> = tphoto.date.map(|d| d.year()),
month: Option<u32> = tphoto.date.map(|d| d.month()),
day: Option<u32> = tphoto.date.map(|d| d.day()),
photo: Photo = tphoto
2016-05-04 14:16:20 +03:00
});
}
2015-11-28 11:53:12 +03:00
}
}
res.error(StatusCode::NotFound, "Photo not found")
}
2016-05-04 14:16:20 +03:00
fn all_years<'mw>(req: &mut Request,
res: Response<'mw>)
-> MiddlewareResult<'mw> {
use rphotos::schema::photos::dsl::*;
let connection = req.db_conn();
let c : &PgConnection = &connection;
return render!(res, "templates/groups.tpl", {
user: Option<String> = req.authorized_user(),
title: &'static str = "All photos",
groups: Vec<Group> =
// FIXME only public if not logged on!
SqlLiteral::new(concat!("select extract(year from date) y, count(*) c",
" from photos group by y order by y").to_string())
.load::<(Option<f64>, i64)>(c).unwrap()
.iter().map(|&(year, count)| {
let year : i32 = year.map(|y: f64| y as i32).unwrap_or(0);
let fromdate = NaiveDate::from_ymd(year, 1, 1).and_hms(0, 0, 0);
let todate = NaiveDate::from_ymd(year + 1, 1, 1).and_hms(0, 0, 0);
let photo = photos
// .only_public(req.authorized_user().is_none())
.filter(date.ge(fromdate))
.filter(date.lt(todate))
// .filter(path.like("%.JPG"))
.order(date)
.limit(1)
.first::<Photo>(c).unwrap();
Group {
title: format!("{}", year),
url: format!("/{}/", year),
count: count,
photo: photo
}
}).collect()
});
}
2016-05-04 14:16:20 +03:00
fn months_in_year<'mw>(req: &mut Request,
res: Response<'mw>)
-> MiddlewareResult<'mw> {
use rphotos::schema::photos::dsl::*;
let connection = req.db_conn();
let c : &PgConnection = &connection;
if let Ok(year) = req.param("year").unwrap().parse::<i32>() {
return render!(res, "templates/groups.tpl", {
user: Option<String> = req.authorized_user(),
title: String = format!("Photos from {}", year),
groups: Vec<Group> =
// FIXME only public if not logged on!
SqlLiteral::new(format!(concat!(
"select extract(month from date) m, count(*) c ",
"from photos where extract(year from date)={} group by m order by m"),
year))
.load::<(Option<f64>, i64)>(c).unwrap()
.iter().map(|&(month, count)| {
let month = month.map(|y| y as u32).unwrap_or(0);
let fromdate = NaiveDate::from_ymd(year, month, 1).and_hms(0, 0, 0);
let todate =
if month == 12 { NaiveDate::from_ymd(year + 1, 1, 1) }
else { NaiveDate::from_ymd(year, month + 1, 1) }
.and_hms(0, 0, 0);
let photo = photos
// .only_public(req.authorized_user().is_none())
.filter(date.ge(fromdate))
.filter(date.lt(todate))
// .filter(path.like("%.JPG"))
.order(date)
.limit(1)
.first::<Photo>(c).unwrap();
Group {
title: monthname(month as u8).to_string(),
url: format!("/{}/{}/", year, month),
count: count,
photo: photo
2015-12-06 16:42:03 +03:00
}
}).collect()
});
}
res.error(StatusCode::NotFound, "Not a year")
}
2016-05-04 14:16:20 +03:00
fn days_in_month<'mw>(req: &mut Request,
res: Response<'mw>)
-> MiddlewareResult<'mw> {
use rphotos::schema::photos::dsl::*;
let connection = req.db_conn();
let c : &PgConnection = &connection;
if let Ok(year) = req.param("year").unwrap().parse::<i32>() {
if let Ok(month) = req.param("month").unwrap().parse::<u8>() {
return render!(res, "templates/groups.tpl", {
user: Option<String> = req.authorized_user(),
lpath: Vec<Link> = vec![Link::year(year)],
title: String = format!("Photos from {} {}", monthname(month),
year),
groups: Vec<Group> =
// FIXME only public if not logged on!
SqlLiteral::new(format!(concat!(
"select extract(day from date) d, count(*) c ",
"from photos where extract(year from date)={} ",
"and extract(month from date)={} group by d order by d"),
year, month))
.load::<(Option<f64>, i64)>(c).unwrap()
.iter().map(|&(day, count)| {
let day = day.map(|y| y as u32).unwrap_or(0);
let fromdate = NaiveDate::from_ymd(year, month as u32, day).and_hms(0, 0, 0);
let photo = photos
// .only_public(req.authorized_user().is_none())
.filter(date.ge(fromdate))
.filter(date.lt(fromdate + ChDuration::days(1)))
// .filter(path.like("%.JPG"))
.order(date)
.limit(1)
.first::<Photo>(c).unwrap();
Group {
title: format!("{}", day),
url: format!("/{}/{}/{}", year, month, day),
count: count,
photo: photo
2016-01-31 23:15:44 +03:00
}
}).collect()
});
2015-12-25 20:32:11 +03:00
}
}
res.error(StatusCode::NotFound, "Not a month")
}
2016-05-04 14:16:20 +03:00
fn all_for_day<'mw>(req: &mut Request,
res: Response<'mw>)
-> MiddlewareResult<'mw> {
if let Ok(year) = req.param("year").unwrap().parse::<i32>() {
if let Ok(month) = req.param("month").unwrap().parse::<u8>() {
if let Ok(day) = req.param("day").unwrap().parse::<u32>() {
let thedate = NaiveDate::from_ymd(year, month as u32, day).and_hms(0, 0, 0);
use rphotos::schema::photos::dsl::*;
let pq = photos
.filter(date.ge(thedate))
.filter(date.lt(thedate + ChDuration::days(1)))
//.filter(path.like("%.JPG"))
.order(date)
.limit(500);
/*
let pq = if req.authorized_user().is_none() {
pq.filter(grade.ge(&rphotos::models::MIN_PUBLIC_GRADE))
} else {
pq
}*/
/*
.no_raw()
*/
let connection = req.db_conn();
let c : &PgConnection = &connection;
return render!(res, "templates/index.tpl", {
user: Option<String> = req.authorized_user(),
lpath: Vec<Link> = vec![Link::year(year),
Link::month(year, month)],
title: String = format!("Photos from {} {} {}",
day, monthname(month), year),
photos: Vec<Photo> = pq.load(c).unwrap()
2016-05-04 14:16:20 +03:00
});
2015-12-25 20:32:11 +03:00
}
}
}
res.error(StatusCode::NotFound, "Not a day")
}
2016-05-04 19:45:50 +03:00
#[derive(Debug, Clone, RustcEncodable)]
struct Link {
pub url: String,
pub name: String,
}
impl Link {
fn year(year: i32) -> Self {
Link {
url: format!("/{}/", year),
name: format!("{}", year),
}
}
fn month(year: i32, month: u8) -> Self {
Link {
url: format!("/{}/{}/", year, month),
name: format!("{}", month),
}
}
fn day(year: i32, month: u8, day: u32) -> Self {
Link {
url: format!("/{}/{}/{}", year, month, day),
name: format!("{}", day),
}
}
2016-05-04 19:45:50 +03:00
}