From 965dcab4868a37d81a3f669a6918550c81fe0bb0 Mon Sep 17 00:00:00 2001 From: Rasmus Kaj Date: Sat, 18 May 2019 21:43:58 +0200 Subject: [PATCH] Make photo size required in db. --- .../down.sql | 2 ++ .../up.sql | 2 ++ src/models.rs | 29 +++++++++---------- src/schema.rs | 4 +-- src/server/mod.rs | 12 ++++---- src/server/views_by_date.rs | 8 ++--- templates/details.rs.html | 2 +- templates/photo_link.rs.html | 4 +-- 8 files changed, 33 insertions(+), 30 deletions(-) create mode 100644 migrations/2019-05-18-170654_require-image-size/down.sql create mode 100644 migrations/2019-05-18-170654_require-image-size/up.sql diff --git a/migrations/2019-05-18-170654_require-image-size/down.sql b/migrations/2019-05-18-170654_require-image-size/down.sql new file mode 100644 index 0000000..7c352ad --- /dev/null +++ b/migrations/2019-05-18-170654_require-image-size/down.sql @@ -0,0 +1,2 @@ +ALTER TABLE photos ALTER COLUMN width DROP NOT NULL; +ALTER TABLE photos ALTER COLUMN height DROP NOT NULL; diff --git a/migrations/2019-05-18-170654_require-image-size/up.sql b/migrations/2019-05-18-170654_require-image-size/up.sql new file mode 100644 index 0000000..4dd8e0b --- /dev/null +++ b/migrations/2019-05-18-170654_require-image-size/up.sql @@ -0,0 +1,2 @@ +ALTER TABLE photos ALTER COLUMN width SET NOT NULL; +ALTER TABLE photos ALTER COLUMN height SET NOT NULL; diff --git a/src/models.rs b/src/models.rs index fa9e222..25443b2 100644 --- a/src/models.rs +++ b/src/models.rs @@ -18,8 +18,8 @@ pub struct Photo { pub is_public: bool, pub camera_id: Option, pub attribution_id: Option, - pub width: Option, - pub height: Option, + pub width: i32, + pub height: i32, } use crate::schema::photos; @@ -73,7 +73,7 @@ impl Photo { { let mut change = false; // TODO Merge updates to one update statement! - if pic.width != Some(newwidth) || pic.height != Some(newheight) { + if pic.width != newwidth || pic.height != newheight { change = true; pic = diesel::update(photos.find(pic.id)) .set((width.eq(newwidth), height.eq(newheight))) @@ -198,17 +198,14 @@ impl Photo { use crate::schema::cameras::dsl::cameras; self.camera_id.and_then(|i| cameras.find(i).first(db).ok()) } - pub fn get_size(&self, max_size: u32) -> Option<(u32, u32)> { - if let (Some(width), Some(height)) = (self.width, self.height) { - let scale = f64::from(max_size) / f64::from(max(width, height)); - let w = (scale * f64::from(width)) as u32; - let h = (scale * f64::from(height)) as u32; - match self.rotation { - _x @ 0...44 | _x @ 315...360 | _x @ 135...224 => Some((w, h)), - _ => Some((h, w)), - } - } else { - None + pub fn get_size(&self, size: SizeTag) -> (u32, u32) { + let (width, height) = (self.width, self.height); + let scale = f64::from(size.px()) / f64::from(max(width, height)); + let w = (scale * f64::from(width)) as u32; + let h = (scale * f64::from(height)) as u32; + match self.rotation { + _x @ 0...44 | _x @ 315...360 | _x @ 135...224 => (w, h), + _ => (h, w), } } @@ -228,8 +225,8 @@ impl Photo { is_public: false, camera_id: None, attribution_id: None, - width: Some(4000), - height: Some(3000), + width: 4000, + height: 3000, } } } diff --git a/src/schema.rs b/src/schema.rs index e52b32e..74d369c 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -47,8 +47,8 @@ table! { is_public -> Bool, camera_id -> Nullable, attribution_id -> Nullable, - width -> Nullable, - height -> Nullable, + width -> Int4, + height -> Int4, } } diff --git a/src/server/mod.rs b/src/server/mod.rs index 662b117..c07e1a0 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -61,8 +61,7 @@ pub struct PhotoLink { pub title: Option, pub href: String, pub id: i32, - // Size should not be optional, but make it best-effort for now. - pub size: Option<(u32, u32)>, + pub size: (u32, u32), pub lable: Option, } @@ -151,7 +150,7 @@ impl PhotoLink { g.first().map(|p| p.id).unwrap_or(0), ), id: photo.id, - size: photo.get_size(SizeTag::Small.px()), + size: photo.get_size(SizeTag::Small), lable: Some(lable), } } @@ -161,7 +160,7 @@ impl PhotoLink { title: p.date.map(|d| d.format("%F").to_string()), href: format!("/img/{}", p.id), id: p.id, - size: p.get_size(SizeTag::Small.px()), + size: p.get_size(SizeTag::Small), lable: p.date.map(|d| d.format("%T").to_string()), } } @@ -170,10 +169,13 @@ impl PhotoLink { title: None, // p.date.map(|d| d.format("%F").to_string()), href: format!("/img/{}", p.id), id: p.id, - size: p.get_size(SizeTag::Small.px()), + size: p.get_size(SizeTag::Small), lable: p.date.map(|d| d.format("%T").to_string()), } } + pub fn is_portrait(&self) -> bool { + self.size.1 > self.size.0 + } } pub fn run(args: &Args) -> Result<(), Error> { diff --git a/src/server/views_by_date.rs b/src/server/views_by_date.rs index 8dfe94b..603befc 100644 --- a/src/server/views_by_date.rs +++ b/src/server/views_by_date.rs @@ -47,7 +47,7 @@ pub fn all_years(context: Context) -> impl Reply { href: format!("/{}/", year.unwrap_or(0)), lable: Some(format!("{} images", count)), id: photo.id, - size: photo.get_size(SizeTag::Small.px()), + size: photo.get_size(SizeTag::Small), } }) .collect::>(); @@ -91,7 +91,7 @@ pub fn months_in_year(year: i32, context: Context) -> Response> { href: format!("/{}/{}/", year, month), lable: Some(format!("{} pictures", count)), id: photo.id, - size: photo.get_size(SizeTag::Small.px()), + size: photo.get_size(SizeTag::Small), } }) .collect::>(); @@ -167,7 +167,7 @@ pub fn days_in_month( href: format!("/{}/{}/{}", year, month, day), lable: Some(format!("{} pictures", count)), id: photo.id, - size: photo.get_size(SizeTag::Small.px()), + size: photo.get_size(SizeTag::Small), } }) .collect::>(); @@ -315,7 +315,7 @@ pub fn on_this_day(context: Context) -> impl Reply { href: format!("/{}/{}/{}", year, month, day), lable: Some(format!("{} pictures", count)), id: photo.id, - size: photo.get_size(SizeTag::Small.px()), + size: photo.get_size(SizeTag::Small), } }) .collect::>(), diff --git a/templates/details.rs.html b/templates/details.rs.html index 89c7e84..4603ae2 100644 --- a/templates/details.rs.html +++ b/templates/details.rs.html @@ -10,7 +10,7 @@ }, {
-
+
@if context.is_authorized() {

@photo.path

diff --git a/templates/photo_link.rs.html b/templates/photo_link.rs.html index 51a8a4a..510bf98 100644 --- a/templates/photo_link.rs.html +++ b/templates/photo_link.rs.html @@ -1,7 +1,7 @@ @use crate::server::PhotoLink; @(photo: &PhotoLink) -
@if let Some(ref title) = photo.title {

@title

} - Photo @photo.id +
@if let Some(ref title) = photo.title {

@title

} + Photo @photo.id @if let Some(ref d) = photo.lable {@d}