Adding an ops tool to look at photographs

This commit is contained in:
John Lyon-Smith
2018-04-27 13:46:15 -07:00
parent 88c20e9dbf
commit d2a22fd3a5
2 changed files with 129 additions and 12 deletions

117
server/src/bin/viewImage.js Normal file
View File

@@ -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] <id>
options:
-h, --hostname <hostname> Hostname of system. Defaults to ${defaultHostname}
-u, --user <email> User email
-p, --password <password> User password
-t, --token <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)
})