-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
197 lines (167 loc) · 5.35 KB
/
Copy pathindex.js
File metadata and controls
197 lines (167 loc) · 5.35 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const tessel = require('tessel');
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const av = require('tessel-av');
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const camera = new av.Camera();
const app = express();
const CAPTURES_PATH = path.join(__dirname, 'captures');
const CAPTURES_STORAGE_PATH = path.join(__dirname, 'captures.json');
ensureDir(CAPTURES_PATH);
ensureStoredCaptures(CAPTURES_STORAGE_PATH);
app.use(bodyParser.json());
app.use('/captures', express.static(CAPTURES_PATH));
app.get('/', (req, res) =>
getStoredCaptures(CAPTURES_STORAGE_PATH).then(captures => {
res.send(renderHtml(captures.items));
})
);
app.post('/jiraShipped', (req, res) => {
const payload = req.body;
console.log(payload);
if (!validPayload(payload)) {
res.status(500).send('Invalid Payload');
return;
}
const capture = camera.capture();
capture.on('data', imgData => {
const timeStamp = Date.now();
const fileName = `${timeStamp}.jpg`;
const imgPath = path.join(CAPTURES_PATH, fileName);
fs.writeFileAsync(imgPath, imgData).then(_ => {
console.log('Captured image');
return getStoredCaptures(CAPTURES_STORAGE_PATH);
}).then(data => {
data.items.push({fileName, timeStamp, overlay: payload.description, key: payload.key});
return saveStoredCaptures(CAPTURES_STORAGE_PATH, data);
}).catch((err) => {
console.log(err);
res.status(500).send('Could not store image');
});
console.log(`Writing image at ${imgPath}`);
res.send('OK');
});
capture.on('error', (error) => {
console.error(error);
res.status(500).send('Capturing picture failed');
});
});
const PORT = 8082;
app.listen(PORT, () =>
console.log(`Server running at ${PORT}`)
);
function ensureDir(path) {
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
}
function ensureStoredCaptures(path) {
if (!fs.existsSync(path)) {
return fs.writeFileSync(path, JSON.stringify({items: []}));
}
}
function getStoredCaptures(path) {
return fs.readFileAsync(path, 'utf8').then(data => {
try {
return JSON.parse(data);
}
catch (e) {
console.error('Could not parse JSON')
return {};
}
});
}
function saveStoredCaptures(path, data) {
// remove items older than 10 days
data.items = data.items.filter(i => i.timeStamp > daysAgoTimeStamp(10));
return fs.writeFileAsync(path, JSON.stringify(data)).then(_ => data);
}
function renderHtml(capturePaths) {
const capturesHTML = capturePaths.reduce((acc, cp) => {
acc += `
<div class="capture">
<div class="overlay">${cp.overlay}</div>
<div class="timestamp">${new Date(cp.timeStamp).toString()}</div>
<div class="key">${cp.key}</div>
<img src="/captures/${cp.fileName}" />
</div>
`;
return acc;
}, '');
return `
<html>
<head>
<title>Schnip Shit</title>
<style>
body {
font-family: sans-serif;
background: #000;
background-image: url(https://www.drupal.org/files/x-all-the-things-template.png);
}
.capture {
position: relative;
margin: 5px;
color: #FFF;
max-width: 50%;
margin: 100px auto;
}
.capture img {
width: 100%;
}
.overlay {
position: absolute;
bottom: 6px;
left: 6px;
font-size: 92px;
text-shadow: -4px 0 black, 0 4px black, 4px 0 black, 0 -4px black;
}
.key {
position: absolute;
top: 6px;
left: 6px;
font-size: 42px;
text-shadow: -3px 0 black, 0 3px black, 3px 0 black, 0 -3px black;
}
.timestamp {
position: absolute;
top: 6px;
right: 6px;
font-size: 12px;
text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}
</style>
</head>
<body>
<div class="container">
${capturesHTML}
</div>
<script>
const captures = document.querySelectorAll('.capture');
let i = 0;
if (captures.length) {
captures.forEach((c) => c.style.display = 'none');
setInterval(() => {
captures.forEach((c) => c.style.display = 'none');
captures[i].style.display = 'block';
i++;
if (i === captures.length) {
i = 0;
}
}, 2000);
}
setTimeout(() => {
window.location.reload();
}, 60000);
</script>
</body>
</html>
`;
}
function validPayload(payload) {
return payload && payload.key && payload.description;
}
function daysAgoTimeStamp(days) {
return Date.now() - days * 24 * 3600 * 1000;
}