album/src/rphotosadm.rs

146 lines
4.9 KiB
Rust
Raw Normal View History

//! Just fooling around with different ways to count images per year.
extern crate rphotos;
extern crate brotli2;
extern crate clap;
2016-11-26 13:31:21 +03:00
extern crate chrono;
#[macro_use]
extern crate diesel;
extern crate djangohashers;
extern crate dotenv;
extern crate env_logger;
extern crate flate2;
extern crate rand;
2016-11-26 13:31:21 +03:00
extern crate rexif;
#[macro_use]
extern crate log;
extern crate image;
2016-11-27 02:54:08 +03:00
extern crate xml;
mod adm;
mod env;
mod photosdir;
use adm::{findphotos, makepublic, readkpa, users, storestatics};
use adm::result::Error;
use adm::stats::show_stats;
use clap::{App, Arg, ArgMatches, SubCommand};
use diesel::pg::PgConnection;
use diesel::prelude::*;
use dotenv::dotenv;
use env::{dburl, photos_dir};
use photosdir::PhotosDir;
use std::fs::File;
use std::io::{self, BufReader};
2016-11-26 13:31:21 +03:00
use std::path::Path;
use std::process::exit;
fn main() {
dotenv().ok();
env_logger::init().unwrap();
let args = App::new("rphotoadm")
.version(env!("CARGO_PKG_VERSION"))
.about("Command line interface for rphotos")
2016-11-26 13:31:21 +03:00
.subcommand(SubCommand::with_name("findphotos")
.about("Find new photos in the photo directory")
.arg(Arg::with_name("BASE")
.multiple(true)
.help("Base directory to search in (relative to the \
image root).")))
2016-11-27 02:54:08 +03:00
.subcommand(SubCommand::with_name("readkpa")
.about("Read metadata from my kphotoalbum"))
.subcommand(SubCommand::with_name("stats")
.about("Show some statistics from the database"))
.subcommand(SubCommand::with_name("userlist")
2017-02-04 21:12:13 +04:00
.about("List existing users"))
.subcommand(SubCommand::with_name("userpass")
.about("Set password for a (new or existing) user")
.arg(Arg::with_name("USER")
2016-11-26 13:31:21 +03:00
.required(true)
.help("Username to set password for")))
.subcommand(SubCommand::with_name("makepublic")
.about("make specific image(s) public")
.arg(Arg::with_name("LIST")
2016-11-26 13:31:21 +03:00
.long("list")
.short("l")
.takes_value(true)
.help("File listing image paths to make public"))
.arg(Arg::with_name("IMAGE")
2016-11-26 13:31:21 +03:00
.required_unless("LIST")
.help("Image path to make public"))
.after_help("The image path(s) are relative to the \
image root."))
.subcommand(SubCommand::with_name("storestatics")
.about("Store statics as files for a web server")
.arg(Arg::with_name("DIR")
.required(true)
.help("Directory to store the files in")))
.get_matches();
match run(args) {
Ok(()) => (),
Err(err) => {
println!("{}", err);
exit(1);
}
}
}
fn run(args: ArgMatches) -> Result<(), Error> {
match args.subcommand() {
2016-11-26 13:31:21 +03:00
("findphotos", Some(args)) => {
let pd = PhotosDir::new(photos_dir());
2017-05-09 00:01:59 +04:00
let db = get_db()?;
2016-11-26 13:31:21 +03:00
if let Some(bases) = args.values_of("BASE") {
for base in bases {
2017-05-09 00:01:59 +04:00
findphotos::crawl(&db, &pd, Path::new(&base))
2017-02-04 21:12:13 +04:00
.map_err(|e| {
Error::Other(format!("Failed to crawl {}: {}",
base,
e))
2017-05-09 00:01:59 +04:00
})?;
2016-11-26 13:31:21 +03:00
}
} else {
2017-05-09 00:01:59 +04:00
findphotos::crawl(&db, &pd, Path::new(""))
2016-11-26 13:31:21 +03:00
.map_err(|e| Error::Other(
2017-05-09 00:01:59 +04:00
format!("Failed to crawl: {}", e)))?;
2016-11-26 13:31:21 +03:00
}
Ok(())
}
("makepublic", Some(args)) => {
let pd = PhotosDir::new(photos_dir());
2017-05-09 00:01:59 +04:00
let db = get_db()?;
2016-11-26 14:32:16 +03:00
match args.value_of("LIST") {
Some("-") => {
let list = io::stdin();
2017-05-09 00:01:59 +04:00
makepublic::by_file_list(&db, &pd, list.lock())?;
2016-11-26 14:32:16 +03:00
Ok(())
}
Some(f) => {
2017-05-09 00:01:59 +04:00
let list = File::open(f)?;
let list = BufReader::new(list);
2016-11-26 14:32:16 +03:00
makepublic::by_file_list(&db, &pd, list)
}
None => {
makepublic::one(&db, &pd, args.value_of("IMAGE").unwrap())
}
}
}
2016-11-27 02:54:08 +03:00
("readkpa", Some(_args)) => {
2017-05-09 00:01:59 +04:00
readkpa::readkpa(&get_db()?, &photos_dir())
2016-11-27 02:54:08 +03:00
}
2017-05-09 00:01:59 +04:00
("stats", Some(_args)) => show_stats(&get_db()?),
("userlist", Some(_args)) => users::list(&get_db()?),
2016-11-26 13:31:21 +03:00
("userpass", Some(args)) => {
2017-05-09 00:01:59 +04:00
users::passwd(&get_db()?, args.value_of("USER").unwrap())
2016-11-26 13:31:21 +03:00
}
("storestatics", Some(args)) => {
storestatics::to_dir(args.value_of("DIR").unwrap())
}
_ => Ok(println!("No subcommand given.\n\n{}", args.usage())),
}
}
fn get_db() -> Result<PgConnection, ConnectionError> {
PgConnection::establish(&dburl())
}