From d2a22fd3a5e1f6d493cad9abd871262568875993 Mon Sep 17 00:00:00 2001 From: John Lyon-Smith Date: Fri, 27 Apr 2018 13:46:15 -0700 Subject: [PATCH] Adding an ops tool to look at photographs --- mobile/src/config.js | 24 ++++---- server/src/bin/viewImage.js | 117 ++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 12 deletions(-) create mode 100644 server/src/bin/viewImage.js diff --git a/mobile/src/config.js b/mobile/src/config.js index ac41ef2..294113f 100644 --- a/mobile/src/config.js +++ b/mobile/src/config.js @@ -7,25 +7,25 @@ export const config = { googleGeocodeAPIKey: "AIzaSyCs4JVT6gysnY5dAJ7KjVJYeykLv_xz1GI", googleGeocodeURL: "https://maps.googleapis.com/maps/api/geocode/json", refererURL: "https://dar.kss.us.com", - defaultUser: "john@lyon-smith.org", - //defaultUser: "", + //defaultUser: "john@lyon-smith.org", + defaultUser: "", minGPSAccuracy: 100, //minGPSAccuracy: 20, minDistanceToItem: 10, geocodeDelayMilliseconds: 500, // This region is downtown Toronto - // initialRegion: { - // latitude: 43.653908, - // longitude: -79.384293, - // latitudeDelta: 0.0922, - // longitudeDelta: 0.0421, - // }, - // This region is Bainbridge Island initialRegion: { - latitude: 47.629536, - longitude: -122.524162, + latitude: 43.653908, + longitude: -79.384293, latitudeDelta: 0.0922, longitudeDelta: 0.0421, }, - alwaysShowWorkItemInAR: true, + // This region is Bainbridge Island + // initialRegion: { + // latitude: 47.629536, + // longitude: -122.524162, + // latitudeDelta: 0.0922, + // longitudeDelta: 0.0421, + // }, + // alwaysShowWorkItemInAR: true, } diff --git a/server/src/bin/viewImage.js b/server/src/bin/viewImage.js new file mode 100644 index 0000000..fa2c473 --- /dev/null +++ b/server/src/bin/viewImage.js @@ -0,0 +1,117 @@ +import parseArgs from "minimist" +import chalk from "chalk" +import fetch from "node-fetch" +import path from "path" +import { promisify } from "util" +import fs from "fs" +import { exec } from "child_process" +import autobind from "autobind-decorator" + +const readAsync = promisify(fs.read) +const closeAsync = promisify(fs.close) +const openAsync = promisify(fs.open) +const fstat = promisify(fs.fstat) + +@autobind +class ViewImageTool { + constructor(toolName, log) { + this.toolName = toolName + this.log = log + } + + async run(argv) { + const defaultHostname = "http://localhost:3001" + const options = { + string: ["user", "password", "hostname", "token"], + boolean: ["help", "version"], + alias: { + u: "user", + p: "password", + t: "token", + h: "hostname", + }, + default: { + hostname: defaultHostname, + }, + } + let args = parseArgs(argv, options) + + if (args.help) { + this.log.info(` +usage: ${this.toolName} [options] + +options: + -h, --hostname Hostname of system. Defaults to ${defaultHostname} + -u, --user User email + -p, --password User password + -t, --token Existing login token +`) + return 0 + } + + if (args._.length < 1) { + this.log.error("Please specify a image asset id") + return -1 + } + + let token = null + let assetId = args._[0] + const contentTypeJsonHeader = { + "Content-Type": "application/json", + } + + if ((args.user && args.password) || args.token) { + if (!args.token) { + const res = await fetch(args.hostname + "/auth/login", { + method: "POST", + headers: contentTypeJsonHeader, + body: JSON.stringify({ + email: args.user, + password: args.password, + }), + }) + const obj = await res.json() + + if (!res.ok) { + throw new Error(obj.message) + } + + this.log.info(`Logged in as '${obj.email}'`) + token = res.headers.get("Authorization").slice("Bearer ".length) + } else { + token = args._[0] + } + } else { + this.log.error("Specify either user email and password, or token") + return -1 + } + + const url = `${args.hostname}/assets/${assetId}?access_token=${token}` + + this.log.info(url) + + exec("open " + url) + + return 0 + } +} + +const log = { + info: console.error, + error: function() { + console.error(chalk.red("error:", [...arguments].join(" "))) + }, + warning: function() { + console.error(chalk.yellow("warning:", [...arguments].join(" "))) + }, +} + +const tool = new ViewImageTool("viewImage", log) +tool + .run(process.argv.slice(2)) + .then((exitCode) => { + process.exit(exitCode) + }) + .catch((err) => { + console.error(err) + })