-
Notifications
You must be signed in to change notification settings - Fork 7
/
router.js
36 lines (30 loc) · 972 Bytes
/
router.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
import koaRouter from 'koa-router';
import koaBody from 'koa-body';
import * as config from './config.js';
import { printFile } from './printer.js';
let bodyParser = koaBody({multipart: true});
let router = koaRouter();
router.post('/receive', bodyParser, function *() {
let body = this.request.body;
if (!body.hasOwnProperty('files')) {
console.error("No attachment found.");
this.response.body = "Error, no attachment found.";
this.response.status = 400;
return;
}
for (let attachmentName in body.files) {
let attachment = body.files[attachmentName];
printFile(attachment.path, config.PRINTER_NAME)
.then(jobID => {
let msg = `Printed ${attachment.name} with job id ${jobID}`;
this.response.body = msg;
console.log(msg);
})
.catch(err => {
this.response.status = 500;
this.response.body = err.toString();
console.error(err);
});
}
});
export default router;