This repository has been archived by the owner on Dec 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (54 loc) · 1.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"use strict"
const exec = require("child_process").execSync
const platform = require('os').platform()
const path = require('path')
const tmpdir = require('os').tmpdir()
const request = require('request')
const fs = require('fs')
const HOST = 'https://zapsnap.io'
function getPlatform() {
let screenshotCommand = undefined
let openCommand = undefined
if (/darwin/.test(platform)){
screenshotCommand = "screencapture -i"
openCommand = "open"
} else if (/freebsd/.test(platform) || /linux/.test(platform)) {
screenshotCommand = "scrot -s"
openCommand = "xdg-open"
}
return {
screenshotCommand,
openCommand
}
}
function uploadPhoto(host, screenshotPath, openCommand) {
request.post({
url: host,
formData: { file: fs.createReadStream(screenshotPath)}
}, (err, httpResponse, body) => {
if (err) {
return console.error('upload failed:', err)
}
const uuid = JSON.parse(body).uuid
const imagePath = path.join(host, uuid)
// open the browser
exec(`${openCommand} ${imagePath}`)
// remove the screenshot
fs.unlink(screenshotPath)
})
}
function seedshot() {
const host = process.env.SEEDSHOT_HOST || HOST
const screenshotPath = path.join(tmpdir, 'screenshot.jpg')
const platform = getPlatform()
// take the screenshot
try {
exec(`${platform.screenshotCommand} ${screenshotPath}`)
} catch (e){
// pressing escape or command+c should not throw errors
return
}
// upload screenshot to the server
uploadPhoto(host, screenshotPath, platform.openCommand)
}
module.exports = seedshot