Include makepublic in rphotosadm.

Another separate binary merged into the general adm binary.
This commit is contained in:
Rasmus Kaj 2016-11-24 22:42:57 +01:00
parent e5b5c28d5e
commit 5afd58086d
6 changed files with 117 additions and 83 deletions

View File

@ -33,10 +33,6 @@ path = "src/readkpa.rs"
name = "findphotos" name = "findphotos"
path = "src/findphotos.rs" path = "src/findphotos.rs"
[[bin]]
name = "public_from_list"
path = "src/public_from_list.rs"
[dependencies] [dependencies]
nickel = "^0.9" nickel = "^0.9"
nickel-jwt-session = "^0.5" nickel-jwt-session = "^0.5"

58
src/adm/makepublic.rs Normal file
View File

@ -0,0 +1,58 @@
use adm::result::Error;
use diesel::pg::PgConnection;
use diesel::prelude::*;
use diesel::result::Error as DieselError;
use diesel::update;
use photosdir::PhotosDir;
use rphotos::models::{Modification, Photo};
use std::io::prelude::*;
pub fn one(db: &PgConnection,
photodir: &PhotosDir,
tpath: &str)
-> Result<(), Error> {
use rphotos::schema::photos::dsl::*;
match update(photos.filter(path.eq(&tpath)))
.set(is_public.eq(true))
.get_result::<Photo>(db) {
Ok(photo) => {
println!("Made {} public: {:?}", tpath, photo);
Ok(())
}
Err(DieselError::NotFound) => {
if !photodir.has_file(&tpath) {
return Err(Error::Other(format!("File {} does not exist",
tpath)));
}
let photo = try!(register_photo(db, &tpath));
println!("New photo {:?} is public.", photo);
Ok(())
}
Err(error) => Err(error.into()),
}
}
pub fn by_file_list<In: BufRead + Sized>(db: &PgConnection,
photodir: &PhotosDir,
list: In)
-> Result<(), Error> {
for line in list.lines() {
try!(one(db, photodir, &try!(line)));
}
Ok(())
}
fn register_photo(db: &PgConnection,
tpath: &str)
-> Result<Photo, DieselError> {
use rphotos::schema::photos::dsl::{photos, is_public};
let photo =
match try!(Photo::create_or_set_basics(&db, &tpath, None, 0, None)) {
Modification::Created(photo) => photo,
Modification::Updated(photo) => photo,
Modification::Unchanged(photo) => photo,
};
update(photos.find(photo.id))
.set(is_public.eq(true))
.get_result::<Photo>(db)
}

View File

@ -1,3 +1,4 @@
pub mod makepublic;
pub mod result; pub mod result;
pub mod stats; pub mod stats;
pub mod users; pub mod users;

View File

@ -1,12 +1,14 @@
use diesel::prelude::ConnectionError; use diesel::prelude::ConnectionError;
use diesel::result::Error as DieselError; use diesel::result::Error as DieselError;
use std::{io, fmt};
use std::convert::From; use std::convert::From;
use std::fmt;
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
Connection(ConnectionError), Connection(ConnectionError),
Db(DieselError), Db(DieselError),
Io(io::Error),
Other(String),
} }
impl fmt::Display for Error { impl fmt::Display for Error {
@ -14,6 +16,8 @@ impl fmt::Display for Error {
match self { match self {
&Error::Connection(ref e) => write!(f, "Connection error: {}", e), &Error::Connection(ref e) => write!(f, "Connection error: {}", e),
&Error::Db(ref e) => write!(f, "Database error: {}", e), &Error::Db(ref e) => write!(f, "Database error: {}", e),
&Error::Io(ref e) => write!(f, "I/O error: {}", e),
&Error::Other(ref s) => write!(f, "Error: {}", s),
} }
} }
} }
@ -29,3 +33,9 @@ impl From<DieselError> for Error {
Error::Db(e) Error::Db(e)
} }
} }
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}

View File

@ -1,73 +0,0 @@
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate dotenv;
extern crate diesel;
extern crate rphotos;
extern crate image;
extern crate rexif;
use diesel::pg::PgConnection;
use diesel::prelude::*;
use diesel::result::Error as DieselError;
use diesel::result::Error;
use dotenv::dotenv;
use rphotos::models::{Modification, Photo};
use std::io;
use std::io::prelude::*;
mod env;
use env::{dburl, photos_dir};
mod photosdir;
use photosdir::PhotosDir;
fn main() {
dotenv().ok();
env_logger::init().unwrap();
let photodir = PhotosDir::new(photos_dir());
let db = PgConnection::establish(&dburl())
.expect("Error connecting to database");
let stdin = io::stdin();
for line in stdin.lock().lines() {
match line {
Ok(line) => {
use rphotos::schema::photos::dsl::*;
match diesel::update(photos.filter(path.eq(&line)))
.set(is_public.eq(true))
.get_result::<Photo>(&db) {
Ok(photo) =>
info!("Made {} public: {:?}", line, photo),
Err(Error::NotFound) => {
if !photodir.has_file(&line) {
panic!("File {} does not exist", line);
}
let photo = register_photo(&db, &line)
.expect("Register photo");
info!("New photo {:?} is public.", photo);
}
Err(error) =>
panic!("Problem with {}: {:?}", line, error),
}
}
Err(err) => {
panic!("Failed to read a line: {:?}", err);
}
}
}
}
fn register_photo(db: &PgConnection, tpath: &str)
-> Result<Photo, DieselError> {
debug!("Should add {} to database", tpath);
use rphotos::schema::photos::dsl::{photos, is_public};
let photo =
match try!(Photo::create_or_set_basics(&db, &tpath, None, 0, None)) {
Modification::Created(photo) => photo,
Modification::Updated(photo) => photo,
Modification::Unchanged(photo) => photo,
};
diesel::update(photos.find(photo.id))
.set(is_public.eq(true))
.get_result::<Photo>(db)
}

View File

@ -7,18 +7,26 @@ extern crate djangohashers;
extern crate dotenv; extern crate dotenv;
extern crate env_logger; extern crate env_logger;
extern crate rand; extern crate rand;
#[macro_use]
extern crate log;
extern crate image;
extern crate rexif;
mod adm; mod adm;
mod env; mod env;
mod photosdir;
use adm::result::Error; use adm::result::Error;
use adm::stats::show_stats; use adm::stats::show_stats;
use adm::users; use adm::{makepublic, users};
use clap::{App, Arg, ArgMatches, SubCommand}; use clap::{App, Arg, ArgMatches, SubCommand};
use diesel::pg::PgConnection; use diesel::pg::PgConnection;
use diesel::prelude::*; use diesel::prelude::*;
use dotenv::dotenv; use dotenv::dotenv;
use env::dburl; use env::{dburl, photos_dir};
use photosdir::PhotosDir;
use std::fs::File;
use std::io::{self, BufReader};
use std::process::exit; use std::process::exit;
fn main() { fn main() {
@ -32,9 +40,21 @@ fn main() {
.about("List users")) .about("List users"))
.subcommand(SubCommand::with_name("userpass") .subcommand(SubCommand::with_name("userpass")
.about("Set password for a (new or existing) user") .about("Set password for a (new or existing) user")
.arg(Arg::with_name("USER") .arg(Arg::with_name("USER")
.required(true) .required(true)
.help("Username to set password for"))) .help("Username to set password for")))
.subcommand(SubCommand::with_name("makepublic")
.about("make specific image(s) public")
.arg(Arg::with_name("LIST")
.long("list")
.short("l")
.takes_value(true)
.help("File listing image paths to make public"))
.arg(Arg::with_name("IMAGE")
.required_unless("LIST")
.help("Image path to make public"))
.after_help("The image path(s) are relative to the \
image root."))
.get_matches(); .get_matches();
match run(args) { match run(args) {
@ -48,6 +68,28 @@ fn main() {
fn run(args: ArgMatches) -> Result<(), Error> { fn run(args: ArgMatches) -> Result<(), Error> {
match args.subcommand() { match args.subcommand() {
("makepublic", Some(args)) => {
let pd = PhotosDir::new(photos_dir());
if let Some(f) = args.value_of("LIST") {
if f == "-" {
let list = io::stdin();
try!(makepublic::by_file_list(&try!(get_db()),
&pd,
list.lock()));
} else {
let list = try!(File::open(f));
let list = BufReader::new(list);
try!(makepublic::by_file_list(&try!(get_db()),
&pd,
list));
}
Ok(())
} else {
makepublic::one(&try!(get_db()),
&pd,
args.value_of("IMAGE").unwrap())
}
}
("stats", Some(_args)) => show_stats(&try!(get_db())), ("stats", Some(_args)) => show_stats(&try!(get_db())),
("userlist", Some(_args)) => users::list(&try!(get_db())), ("userlist", Some(_args)) => users::list(&try!(get_db())),
("userpass", Some(args)) => users::passwd(&try!(get_db()), ("userpass", Some(args)) => users::passwd(&try!(get_db()),