album/src/findphotos.rs

196 lines
6.1 KiB
Rust
Raw Normal View History

2016-05-04 14:16:20 +03:00
#[macro_use]
extern crate log;
2016-01-31 16:34:48 +03:00
extern crate chrono;
extern crate env_logger;
extern crate image;
extern crate rexif;
extern crate rustc_serialize;
extern crate dotenv;
extern crate diesel;
extern crate rphotos;
2016-01-31 16:34:48 +03:00
use chrono::format::ParseError;
use chrono::naive::datetime::NaiveDateTime;
use rexif::{ExifData, ExifEntry, ExifTag, TagValue};
use std::path::Path;
use dotenv::dotenv;
use diesel::pg::PgConnection;
use self::diesel::prelude::*;
2016-06-05 18:24:11 +03:00
use rphotos::models::{Modification, Photo};
2016-01-31 16:34:48 +03:00
mod env;
use env::{dburl, photos_dir};
mod photosdir;
use photosdir::PhotosDir;
fn main() {
dotenv().ok();
2016-01-31 16:34:48 +03:00
env_logger::init().unwrap();
let db = PgConnection::establish(&dburl())
.expect("Error connecting to database");
2016-01-31 16:34:48 +03:00
let photos = PhotosDir::new(photos_dir());
2016-06-05 22:58:01 +03:00
let args = std::env::args().skip(1);
if args.len() > 0 {
for a in args {
do_find(&db, &photos, Path::new(&a));
}
} else {
println!("No args");
}
}
fn do_find(db: &PgConnection, photos: &PhotosDir, only_in: &Path) {
2016-05-04 14:16:20 +03:00
photos.find_files(only_in,
&|path, exif| {
match save_photo(&db, path, &exif) {
2016-05-04 14:16:20 +03:00
Ok(()) => debug!("Saved photo {}", path),
Err(e) => {
warn!("Failed to save photo {}: {:?}",
path,
e)
}
}
})
.unwrap();
2016-01-31 16:34:48 +03:00
}
fn save_photo(db: &PgConnection,
file_path: &str,
2016-05-04 14:16:20 +03:00
exif: &ExifData)
-> FindPhotoResult<()> {
let photo = match try!(Photo::create_or_set_basics(db, file_path,
2016-07-09 17:04:19 +03:00
Some(try!(find_date(&exif))),
try!(find_rotation(&exif)))) {
Modification::Created(photo) => {
info!("Created {:?}", photo);
photo
}
Modification::Updated(photo) => {
info!("Modified {:?}", photo);
photo
}
Modification::Unchanged(photo) => {
debug!("No change for {:?}", photo);
photo
}
};
2016-07-09 17:04:19 +03:00
if let Some((lat, long)) = try!(find_position(&exif)) {
debug!("Position for {} is {} {}", file_path, lat, long);
use rphotos::schema::positions::dsl::*;
if let Ok((pos, clat, clong)) =
positions.filter(photo_id.eq(photo.id))
.select((id, latitude, longitude))
.first::<(i32, i32, i32)>(db) {
2016-07-09 17:04:19 +03:00
if (clat != (lat * 1e6) as i32) || (clong != (long * 1e6) as i32) {
panic!("TODO Should update position #{} from {} {} to {} {}",
pos, clat, clong, lat, long)
}
} else {
info!("Position for {} is {} {}", file_path, lat, long);
use rphotos::models::NewPosition;
diesel::insert(&NewPosition {
photo_id: photo.id,
latitude: (lat * 1e6) as i32,
longitude: (long * 1e6) as i32,
})
.into(positions)
.execute(db)
.expect("Insert image position");
2016-07-09 17:04:19 +03:00
}
}
2016-01-31 16:34:48 +03:00
Ok(())
}
type FindPhotoResult<T> = Result<T, FindPhotoError>;
2016-01-31 16:34:48 +03:00
#[derive(Debug)]
enum FindPhotoError {
DatabaseError(diesel::result::Error),
2016-01-31 16:34:48 +03:00
ExifOfUnexpectedType(TagValue),
ExifTagMissing(ExifTag),
TimeFormat(ParseError),
UnknownOrientation(u16),
BadLatLong(TagValue),
2016-01-31 16:34:48 +03:00
}
impl From<diesel::result::Error> for FindPhotoError {
fn from(err: diesel::result::Error) -> FindPhotoError {
FindPhotoError::DatabaseError(err)
}
}
2016-01-31 16:34:48 +03:00
impl From<ParseError> for FindPhotoError {
fn from(err: ParseError) -> FindPhotoError {
FindPhotoError::TimeFormat(err)
}
}
fn find_rotation(exif: &ExifData) -> FindPhotoResult<i16> {
2016-01-31 16:34:48 +03:00
if let Some(ref value) = find_entry(exif, &ExifTag::Orientation) {
if let TagValue::U16(ref v) = value.value {
let n = v[0];
debug!("Raw orientation is {}", n);
2016-01-31 16:34:48 +03:00
match n {
1 => Ok(0),
3 => Ok(180),
6 => Ok(90),
8 => Ok(270),
x => Err(FindPhotoError::UnknownOrientation(x)),
}
} else {
Err(FindPhotoError::ExifOfUnexpectedType(value.value.clone()))
}
} else {
info!("Orientation tag missing, default to 0 degrees");
Ok(0)
}
}
fn find_date(exif: &ExifData) -> FindPhotoResult<NaiveDateTime> {
find_entry(exif, &ExifTag::DateTimeOriginal)
2016-08-06 00:21:24 +03:00
.or_else(|| find_entry(exif, &ExifTag::DateTime))
.or_else(|| find_entry(exif, &ExifTag::DateTimeDigitized))
.map(|value| {
debug!("Found {:?}", value);
if let TagValue::Ascii(ref str) = value.value {
debug!("Try to parse {:?} (from {:?}) as datetime",
str, value.tag);
Ok(try!(NaiveDateTime::parse_from_str(str, "%Y:%m:%d %T")))
} else {
Err(FindPhotoError::ExifOfUnexpectedType(value.value.clone()))
}
})
.unwrap_or_else(|| {
Err(FindPhotoError::ExifTagMissing(ExifTag::DateTimeOriginal))
})
2016-01-31 16:34:48 +03:00
}
fn find_position(exif: &ExifData) -> FindPhotoResult<Option<(f64, f64)>> {
if let Some(lat) = find_entry(exif, &ExifTag::GPSLatitude) {
if let Some(long) = find_entry(exif, &ExifTag::GPSLongitude) {
return Ok(Some((try!(rat2float(&lat.value)),
try!(rat2float(&long.value)))));
}
2016-07-09 17:04:19 +03:00
}
Ok(None)
}
fn rat2float(val: &rexif::TagValue) -> FindPhotoResult<f64> {
2016-07-09 17:04:19 +03:00
if let rexif::TagValue::URational(ref v) = *val {
if v.len() == 3 {
return Ok(v[0].value() +
(v[1].value() + v[2].value() / 60.0) / 60.0);
}
2016-07-09 17:04:19 +03:00
}
Err(FindPhotoError::BadLatLong(val.clone()))
2016-07-09 17:04:19 +03:00
}
2016-01-31 16:34:48 +03:00
fn find_entry<'a>(exif: &'a ExifData, tag: &ExifTag) -> Option<&'a ExifEntry> {
for entry in &exif.entries {
if entry.tag == *tag {
return Some(entry);
}
}
None
}