Some cleanups, mainly clippy-suggested.
This commit is contained in:
parent
20c72d5ef3
commit
e86271fc2a
@ -25,12 +25,8 @@ fn save_photo(
|
||||
file_path: &str,
|
||||
exif: &ExifData,
|
||||
) -> Result<(), Error> {
|
||||
let width = exif
|
||||
.width
|
||||
.ok_or(Error::Other(format!("Image {} missing width", file_path,)))?;
|
||||
let height = exif
|
||||
.height
|
||||
.ok_or(Error::Other(format!("Image {} missing height", file_path,)))?;
|
||||
let width = exif.width.ok_or(Error::MissingWidth)?;
|
||||
let height = exif.height.ok_or(Error::MissingHeight)?;
|
||||
let photo = match Photo::create_or_set_basics(
|
||||
db,
|
||||
file_path,
|
||||
|
@ -25,7 +25,7 @@ pub fn precache(db: &PgConnection, pd: &PhotosDir) -> Result<(), Error> {
|
||||
let no_expire = 0;
|
||||
for photo in photos {
|
||||
n += 1;
|
||||
let key = &photo.cache_key(&size);
|
||||
let key = &photo.cache_key(size);
|
||||
if cache.get(key.as_bytes()).is_ok() {
|
||||
debug!("Cache: {} found for {}", key, photo.path);
|
||||
} else {
|
||||
|
@ -17,6 +17,8 @@ pub enum Error {
|
||||
BadTimeFormat(ChronoParseError),
|
||||
BadIntFormat(ParseIntError),
|
||||
Cache(MemcachedError),
|
||||
MissingWidth,
|
||||
MissingHeight,
|
||||
Other(String),
|
||||
}
|
||||
|
||||
@ -32,6 +34,8 @@ impl fmt::Display for Error {
|
||||
Error::BadTimeFormat(ref e) => write!(f, "Bad time value: {}", e),
|
||||
Error::BadIntFormat(ref e) => write!(f, "Bad int value: {}", e),
|
||||
Error::Cache(ref e) => write!(f, "Memcached error: {}", e),
|
||||
Error::MissingHeight => write!(f, "Missing height property"),
|
||||
Error::MissingWidth => write!(f, "Missing width property"),
|
||||
Error::Other(ref s) => write!(f, "Error: {}", s),
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ impl Photo {
|
||||
self.is_public
|
||||
}
|
||||
|
||||
pub fn cache_key(&self, size: &SizeTag) -> String {
|
||||
pub fn cache_key(&self, size: SizeTag) -> String {
|
||||
format!("rp{}{:?}", self.id, size)
|
||||
}
|
||||
|
||||
|
115
src/myexif.rs
115
src/myexif.rs
@ -30,45 +30,44 @@ impl ExifData {
|
||||
let reader = Reader::new(&mut BufReader::new(&file))?;
|
||||
for f in reader.fields() {
|
||||
if !f.thumbnail {
|
||||
if let Some(d) = is_datetime(f, &Tag::DateTimeOriginal) {
|
||||
if let Some(d) = is_datetime(f, Tag::DateTimeOriginal) {
|
||||
result.dateval = Some(d);
|
||||
} else if let Some(d) = is_datetime(f, &Tag::DateTime) {
|
||||
} else if let Some(d) = is_datetime(f, Tag::DateTime) {
|
||||
result.dateval = Some(d);
|
||||
} else if let Some(d) = is_datetime(f, &Tag::DateTimeDigitized)
|
||||
} else if let Some(d) = is_datetime(f, Tag::DateTimeDigitized)
|
||||
{
|
||||
if result.dateval.is_none() {
|
||||
result.dateval = Some(d)
|
||||
}
|
||||
} else if let Some(s) = is_string(f, &Tag::Make) {
|
||||
} else if let Some(s) = is_string(f, Tag::Make) {
|
||||
result.make = Some(s.to_string());
|
||||
} else if let Some(s) = is_string(f, &Tag::Model) {
|
||||
} else if let Some(s) = is_string(f, Tag::Model) {
|
||||
result.model = Some(s.to_string());
|
||||
} else if let Some(w) = is_u32(f, &Tag::PixelXDimension) {
|
||||
} else if let Some(w) = is_u32(f, Tag::PixelXDimension) {
|
||||
result.width = Some(w);
|
||||
} else if let Some(h) = is_u32(f, &Tag::PixelYDimension) {
|
||||
} else if let Some(h) = is_u32(f, Tag::PixelYDimension) {
|
||||
result.height = Some(h);
|
||||
} else if let Some(w) = is_u32(f, &Tag::ImageWidth) {
|
||||
} else if let Some(w) = is_u32(f, Tag::ImageWidth) {
|
||||
result.width = Some(w);
|
||||
} else if let Some(h) = is_u32(f, &Tag::ImageLength) {
|
||||
} else if let Some(h) = is_u32(f, Tag::ImageLength) {
|
||||
result.height = Some(h);
|
||||
} else if let Some(o) = is_u32(f, &Tag::Orientation) {
|
||||
} else if let Some(o) = is_u32(f, Tag::Orientation) {
|
||||
result.orientation = Some(o);
|
||||
} else if let Some(lat) = is_lat_long(f, &Tag::GPSLatitude) {
|
||||
} else if let Some(lat) = is_lat_long(f, Tag::GPSLatitude) {
|
||||
result.latval = Some(lat);
|
||||
} else if let Some(long) = is_lat_long(f, &Tag::GPSLongitude) {
|
||||
} else if let Some(long) = is_lat_long(f, Tag::GPSLongitude) {
|
||||
result.longval = Some(long);
|
||||
} else if let Some(s) = is_string(f, &Tag::GPSLatitudeRef) {
|
||||
} else if let Some(s) = is_string(f, Tag::GPSLatitudeRef) {
|
||||
result.latref = Some(s.to_string());
|
||||
} else if let Some(s) = is_string(f, &Tag::GPSLongitudeRef) {
|
||||
} else if let Some(s) = is_string(f, Tag::GPSLongitudeRef) {
|
||||
result.longref = Some(s.to_string());
|
||||
} else if let Some(s) = is_string(f, &Tag::GPSImgDirectionRef)
|
||||
{
|
||||
} else if let Some(s) = is_string(f, Tag::GPSImgDirectionRef) {
|
||||
println!(" direction ref: {}", s);
|
||||
} else if let Some(s) = is_string(f, &Tag::GPSImgDirection) {
|
||||
} else if let Some(s) = is_string(f, Tag::GPSImgDirection) {
|
||||
println!(" direction: {}", s);
|
||||
} else if let Some(d) = is_date(f, &Tag::GPSDateStamp) {
|
||||
} else if let Some(d) = is_date(f, Tag::GPSDateStamp) {
|
||||
result.gpsdate = Some(d);
|
||||
} else if let Some(hms) = is_time(f, &Tag::GPSTimeStamp) {
|
||||
} else if let Some(hms) = is_time(f, Tag::GPSTimeStamp) {
|
||||
result.gpstime = Some(hms);
|
||||
}
|
||||
}
|
||||
@ -83,7 +82,7 @@ impl ExifData {
|
||||
if let (&Some(date), &Some((h, m, s))) = (&self.gpsdate, &self.gpstime)
|
||||
{
|
||||
let naive = date
|
||||
.and_hms(h as u32, m as u32, s as u32)
|
||||
.and_hms(u32::from(h), u32::from(m), u32::from(s))
|
||||
.with_timezone(&Local)
|
||||
.naive_local();
|
||||
debug!("GPS Date {}, {}:{}:{} => {}", date, h, m, s, naive);
|
||||
@ -150,13 +149,12 @@ impl ExifData {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_lat_long(f: &Field, tag: &Tag) -> Option<f64> {
|
||||
if f.tag == *tag {
|
||||
match &f.value {
|
||||
&Value::Rational(ref v) if v.len() == 3 => {
|
||||
let (v0, v1, v2) =
|
||||
(v[0].to_f64(), v[1].to_f64(), v[2].to_f64());
|
||||
return Some(v0 + (v1 + v2 / 60.0) / 60.0);
|
||||
fn is_lat_long(f: &Field, tag: Tag) -> Option<f64> {
|
||||
if f.tag == tag {
|
||||
match f.value {
|
||||
Value::Rational(ref v) if v.len() == 3 => {
|
||||
let d = 1. / 60.;
|
||||
Some(v[0].to_f64() + d * (v[1].to_f64() + d * v[2].to_f64()))
|
||||
}
|
||||
ref v => {
|
||||
println!("ERROR: Bad value for {}: {:?}", tag, v);
|
||||
@ -168,36 +166,35 @@ fn is_lat_long(f: &Field, tag: &Tag) -> Option<f64> {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_datetime(f: &Field, tag: &Tag) -> Option<NaiveDateTime> {
|
||||
if f.tag == *tag {
|
||||
match single_datetime(&f.value) {
|
||||
Ok(date) => Some(date),
|
||||
Err(err) => {
|
||||
println!("ERROR: Expected datetime for {}: {:?}", tag, err);
|
||||
None
|
||||
}
|
||||
}
|
||||
fn is_datetime(f: &Field, tag: Tag) -> Option<NaiveDateTime> {
|
||||
if f.tag == tag {
|
||||
single_ascii(&f.value)
|
||||
.and_then(|s| Ok(NaiveDateTime::parse_from_str(s, "%Y:%m:%d %T")?))
|
||||
.map_err(|e| {
|
||||
println!("ERROR: Expected datetime for {}: {:?}", tag, e);
|
||||
})
|
||||
.ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn is_date(f: &Field, tag: &Tag) -> Option<Date<Utc>> {
|
||||
if f.tag == *tag {
|
||||
match single_date(&f.value) {
|
||||
Ok(date) => Some(date),
|
||||
Err(err) => {
|
||||
println!("ERROR: Expected date for {}: {:?}", tag, err);
|
||||
None
|
||||
}
|
||||
}
|
||||
fn is_date(f: &Field, tag: Tag) -> Option<Date<Utc>> {
|
||||
if f.tag == tag {
|
||||
single_ascii(&f.value)
|
||||
.and_then(|s| Ok(NaiveDate::parse_from_str(s, "%Y:%m:%d")?))
|
||||
.map(|d| Date::from_utc(d, Utc))
|
||||
.map_err(|e| {
|
||||
println!("ERROR: Expected date for {}: {:?}", tag, e);
|
||||
})
|
||||
.ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn is_time(f: &Field, tag: &Tag) -> Option<(u8, u8, u8)> {
|
||||
if f.tag == *tag {
|
||||
fn is_time(f: &Field, tag: Tag) -> Option<(u8, u8, u8)> {
|
||||
if f.tag == tag {
|
||||
match &f.value {
|
||||
&Value::Rational(ref v)
|
||||
if v.len() == 3
|
||||
@ -217,8 +214,8 @@ fn is_time(f: &Field, tag: &Tag) -> Option<(u8, u8, u8)> {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_string<'a>(f: &'a Field, tag: &Tag) -> Option<&'a str> {
|
||||
if f.tag == *tag {
|
||||
fn is_string<'a>(f: &'a Field, tag: Tag) -> Option<&'a str> {
|
||||
if f.tag == tag {
|
||||
match single_ascii(&f.value) {
|
||||
Ok(s) => Some(s),
|
||||
Err(err) => {
|
||||
@ -231,11 +228,11 @@ fn is_string<'a>(f: &'a Field, tag: &Tag) -> Option<&'a str> {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_u32(f: &Field, tag: &Tag) -> Option<u32> {
|
||||
if f.tag == *tag {
|
||||
fn is_u32(f: &Field, tag: Tag) -> Option<u32> {
|
||||
if f.tag == tag {
|
||||
match &f.value {
|
||||
&Value::Long(ref v) if v.len() == 1 => Some(v[0]),
|
||||
&Value::Short(ref v) if v.len() == 1 => Some(v[0] as u32),
|
||||
&Value::Short(ref v) if v.len() == 1 => Some(u32::from(v[0])),
|
||||
v => {
|
||||
println!("ERROR: Unsuppored value for {}: {:?}", tag, v);
|
||||
None
|
||||
@ -246,20 +243,6 @@ fn is_u32(f: &Field, tag: &Tag) -> Option<u32> {
|
||||
}
|
||||
}
|
||||
|
||||
fn single_datetime(value: &Value) -> Result<NaiveDateTime, Error> {
|
||||
single_ascii(value)
|
||||
.and_then(|s| Ok(NaiveDateTime::parse_from_str(s, "%Y:%m:%d %T")?))
|
||||
}
|
||||
|
||||
fn single_date(value: &Value) -> Result<Date<Utc>, Error> {
|
||||
single_ascii(value).and_then(|s| {
|
||||
Ok(Date::from_utc(
|
||||
NaiveDate::parse_from_str(s, "%Y:%m:%d")?,
|
||||
Utc,
|
||||
))
|
||||
})
|
||||
}
|
||||
|
||||
fn single_ascii<'a>(value: &'a Value) -> Result<&'a str, Error> {
|
||||
match value {
|
||||
&Value::Ascii(ref v) if v.len() == 1 => Ok(from_utf8(v[0])?),
|
||||
|
@ -10,7 +10,7 @@ pub struct PhotosDirMiddleware {
|
||||
|
||||
impl PhotosDirMiddleware {
|
||||
pub fn new(dir: PathBuf) -> Self {
|
||||
PhotosDirMiddleware { dir: dir }
|
||||
PhotosDirMiddleware { dir }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,8 +18,8 @@ impl RequestLogger {
|
||||
debug!("Start handling {}", mu);
|
||||
RequestLogger {
|
||||
start: get_time(),
|
||||
mu: mu,
|
||||
status: status,
|
||||
mu,
|
||||
status,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ pub fn rotate<'mw>(
|
||||
req: &mut Request,
|
||||
res: Response<'mw>,
|
||||
) -> MiddlewareResult<'mw> {
|
||||
if !req.authorized_user().is_some() {
|
||||
if req.authorized_user().is_none() {
|
||||
return res.error(StatusCode::Unauthorized, "permission denied");
|
||||
}
|
||||
if let (Some(image), Some(angle)) = try_with!(res, rotate_params(req)) {
|
||||
@ -28,8 +28,8 @@ pub fn rotate<'mw>(
|
||||
image.rotation = newvalue;
|
||||
match image.save_changes::<Photo>(c) {
|
||||
Ok(image) => {
|
||||
req.clear_cache(&image.cache_key(&SizeTag::Small));
|
||||
req.clear_cache(&image.cache_key(&SizeTag::Medium));
|
||||
req.clear_cache(&image.cache_key(SizeTag::Small));
|
||||
req.clear_cache(&image.cache_key(SizeTag::Medium));
|
||||
return res.ok(|o| writeln!(o, "ok"));
|
||||
}
|
||||
Err(error) => {
|
||||
@ -56,7 +56,7 @@ pub fn set_tag<'mw>(
|
||||
req: &mut Request,
|
||||
res: Response<'mw>,
|
||||
) -> MiddlewareResult<'mw> {
|
||||
if !req.authorized_user().is_some() {
|
||||
if req.authorized_user().is_none() {
|
||||
return res.error(StatusCode::Unauthorized, "permission denied");
|
||||
}
|
||||
if let (Some(image), Some(tag)) = try_with!(res, tag_params(req)) {
|
||||
@ -105,7 +105,7 @@ pub fn set_person<'mw>(
|
||||
req: &mut Request,
|
||||
res: Response<'mw>,
|
||||
) -> MiddlewareResult<'mw> {
|
||||
if !req.authorized_user().is_some() {
|
||||
if req.authorized_user().is_none() {
|
||||
return res.error(StatusCode::Unauthorized, "permission denied");
|
||||
}
|
||||
if let (Some(image), Some(name)) = try_with!(res, person_params(req)) {
|
||||
@ -158,7 +158,7 @@ pub fn set_grade<'mw>(
|
||||
req: &mut Request,
|
||||
res: Response<'mw>,
|
||||
) -> MiddlewareResult<'mw> {
|
||||
if !req.authorized_user().is_some() {
|
||||
if req.authorized_user().is_none() {
|
||||
return res.error(StatusCode::Unauthorized, "permission denied");
|
||||
}
|
||||
if let (Some(image), Some(newgrade)) = try_with!(res, grade_params(req)) {
|
||||
|
@ -277,8 +277,8 @@ pub enum SizeTag {
|
||||
Large,
|
||||
}
|
||||
impl SizeTag {
|
||||
pub fn px(&self) -> u32 {
|
||||
match *self {
|
||||
pub fn px(self) -> u32 {
|
||||
match self {
|
||||
SizeTag::Small => 240,
|
||||
SizeTag::Medium => 960,
|
||||
SizeTag::Large => 1900,
|
||||
@ -329,7 +329,7 @@ fn get_image_data(
|
||||
photo: &Photo,
|
||||
size: SizeTag,
|
||||
) -> Result<Vec<u8>, image::ImageError> {
|
||||
req.cached_or(&photo.cache_key(&size), || {
|
||||
req.cached_or(&photo.cache_key(size), || {
|
||||
let size = size.px();
|
||||
req.photos().scale_image(photo, size, size)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user