Make photo size required in db.

This commit is contained in:
Rasmus Kaj 2019-05-18 21:43:58 +02:00
parent 19e714970b
commit 965dcab486
8 changed files with 33 additions and 30 deletions

View File

@ -0,0 +1,2 @@
ALTER TABLE photos ALTER COLUMN width DROP NOT NULL;
ALTER TABLE photos ALTER COLUMN height DROP NOT NULL;

View File

@ -0,0 +1,2 @@
ALTER TABLE photos ALTER COLUMN width SET NOT NULL;
ALTER TABLE photos ALTER COLUMN height SET NOT NULL;

View File

@ -18,8 +18,8 @@ pub struct Photo {
pub is_public: bool,
pub camera_id: Option<i32>,
pub attribution_id: Option<i32>,
pub width: Option<i32>,
pub height: Option<i32>,
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,
}
}
}

View File

@ -47,8 +47,8 @@ table! {
is_public -> Bool,
camera_id -> Nullable<Int4>,
attribution_id -> Nullable<Int4>,
width -> Nullable<Int4>,
height -> Nullable<Int4>,
width -> Int4,
height -> Int4,
}
}

View File

@ -61,8 +61,7 @@ pub struct PhotoLink {
pub title: Option<String>,
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<String>,
}
@ -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> {

View File

@ -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::<Vec<_>>();
@ -91,7 +91,7 @@ pub fn months_in_year(year: i32, context: Context) -> Response<Vec<u8>> {
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::<Vec<_>>();
@ -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::<Vec<_>>();
@ -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::<Vec<_>>(),

View File

@ -10,7 +10,7 @@
<meta property='og:description' content='@for p in people {@p.person_name, }@for t in tags {#@t.tag_name, }@if let Some(p) = places.first() {@p.place_name}'>
}, {
<div class="details" data-imgid="@photo.id"@if let Some(g) = photo.grade { data-grade="@g"}@if let Some(ref p) = *position { data-position="[@p.x, @p.y]"}>
<div class="item"><img src="/img/@photo.id-m.jpg"@if let Some((w,h)) = photo.get_size(SizeTag::Medium.px()) { width="@w" height="@h"}></div>
<div class="item"><img src="/img/@photo.id-m.jpg" width="@photo.get_size(SizeTag::Medium).0" height="@photo.get_size(SizeTag::Medium).1"></div>
<div class="meta">
@if context.is_authorized() {
<p><a href="/img/@photo.id-l.jpg">@photo.path</a></p>

View File

@ -1,7 +1,7 @@
@use crate::server::PhotoLink;
@(photo: &PhotoLink)
<div class="item@if let Some((w, h)) = photo.size {@if h > w { portrait}}">@if let Some(ref title) = photo.title {<h2>@title</h2>}
<a href="@photo.href"><img src="/img/@photo.id-s.jpg" @if let Some((w, h)) = photo.size {width="@w" height="@h"} alt="Photo @photo.id"></a>
<div class="item@if photo.is_portrait() { portrait}">@if let Some(ref title) = photo.title {<h2>@title</h2>}
<a href="@photo.href"><img src="/img/@photo.id-s.jpg" width="@photo.size.0" height="@photo.size.1" alt="Photo @photo.id"></a>
@if let Some(ref d) = photo.lable {<span class="lable">@d</span>}
</div>