-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.js
42 lines (38 loc) · 997 Bytes
/
get.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
import fs from 'fs'
import { execaCommand } from 'execa'
export default {
method: 'GET',
url: '/',
schema: {
querystring: {
type: 'object',
required: ['token'],
properties: {
token: { type: 'string' }
}
},
response: {
200: {
type: 'object'
}
}
},
handler: async function getJobHandler (request, reply) {
const { token } = request.query
const jobData = await this.cloudPrnt.getJobData(token)
if (jobData === null) {
return reply.code(404).send('Job not found')
}
const renderedJob = await this.view('templates/receipt.stm', jobData)
const fileName = `/tmp/${token}.stm`
fs.writeFileSync(fileName, renderedJob, { flag: 'w+' })
const mimeType = 'application/vnd.star.starprntcore'
const subprocess = await execaCommand(
`cputil decode ${mimeType} ${fileName} -`,
{ encoding: null }
)
return reply
.type(mimeType)
.send(subprocess.stdout)
}
}