2019-08-03 15:53:41 +02:00
|
|
|
import express from 'express'
|
|
|
|
import cors from 'cors'
|
|
|
|
import bodyParser from 'body-parser'
|
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
|
|
|
import formidable from 'formidable'
|
|
|
|
import shellExec from 'shell-exec'
|
2018-12-20 16:54:05 +01:00
|
|
|
|
2019-08-03 15:53:41 +02:00
|
|
|
const app = express()
|
|
|
|
app.use(cors())
|
|
|
|
app.use(bodyParser.json())
|
|
|
|
const port = process.env.PORT || 4000
|
|
|
|
const formats = ['pdf', 'ps']
|
|
|
|
const sizes = ['full', 'a4', 'a3', 'a2', 'a1', 'a0', 'letter', 'tabloid']
|
2018-12-20 16:54:05 +01:00
|
|
|
|
2019-08-03 15:53:41 +02:00
|
|
|
app.get('/', async (req, res) => res.sendFile(path.resolve(__dirname + '/form.html')))
|
|
|
|
app.post('/', async (req, res) => {
|
|
|
|
let form = new formidable.IncomingForm()
|
2018-12-20 16:54:05 +01:00
|
|
|
form.parse(req, (err, fields, files) => {
|
2019-08-03 15:53:41 +02:00
|
|
|
if (
|
|
|
|
err ||
|
|
|
|
typeof files.svg === 'undefined' ||
|
2018-12-20 16:54:05 +01:00
|
|
|
formats.indexOf(fields.format) === -1 ||
|
|
|
|
sizes.indexOf(fields.size) === -1
|
2019-08-03 15:53:41 +02:00
|
|
|
)
|
|
|
|
return res.sendFile(path.resolve(__dirname + '/form.html'))
|
|
|
|
let upload = files.svg.path
|
|
|
|
let cmd
|
|
|
|
if (fields.size === 'full') {
|
|
|
|
// Do not tile
|
|
|
|
let target = `/tmp/pattern.${fields.format}`
|
|
|
|
cmd = `/usr/bin/inkscape --export-${fields.format}=${target} ${upload}`
|
2018-12-20 16:54:05 +01:00
|
|
|
shellExec(cmd).then(() => {
|
2019-08-03 15:53:41 +02:00
|
|
|
return res.sendFile(target)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// Do tile
|
|
|
|
let untiled = '/tmp/untiled.ps'
|
|
|
|
let tiled = '/tmp/tiled.ps'
|
|
|
|
cmd = `/usr/bin/inkscape --export-ps=${untiled} ${upload}`
|
2018-12-20 16:54:05 +01:00
|
|
|
shellExec(cmd).then(() => {
|
2019-08-03 15:53:41 +02:00
|
|
|
cmd = `/usr/local/bin/tile -a -m${
|
|
|
|
fields.size
|
|
|
|
} -s1 -t"On-demand tiler" ${untiled} > ${tiled}`
|
2018-12-20 16:54:05 +01:00
|
|
|
shellExec(cmd).then(() => {
|
2019-08-03 15:53:41 +02:00
|
|
|
if (fields.format === 'ps') return res.sendFile(tiled)
|
|
|
|
cmd = `/usr/bin/ps2pdf14 ${tiled} ${tiled}.pdf`
|
2018-12-20 16:54:05 +01:00
|
|
|
shellExec(cmd).then(() => {
|
2019-08-03 15:53:41 +02:00
|
|
|
return res.sendFile(tiled + '.pdf')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2018-12-20 16:54:05 +01:00
|
|
|
}
|
2019-08-03 15:53:41 +02:00
|
|
|
})
|
|
|
|
})
|
2018-12-20 16:54:05 +01:00
|
|
|
|
2019-08-03 15:53:41 +02:00
|
|
|
app.post('/api', async (req, res) => {
|
|
|
|
if (
|
|
|
|
typeof req.body.svg === 'undefined' ||
|
|
|
|
typeof req.body.format === 'undefined' ||
|
|
|
|
typeof req.body.size === 'undefined' ||
|
2019-01-05 19:00:41 +00:00
|
|
|
formats.indexOf(req.body.format) === -1 ||
|
|
|
|
sizes.indexOf(req.body.size) === -1
|
|
|
|
)
|
2019-08-03 15:53:41 +02:00
|
|
|
return res.sendStatus(400)
|
|
|
|
let storage = '/fs/storage/tmp/'
|
|
|
|
let dir = createTempDir(storage)
|
|
|
|
let svg = storage + dir + '/draft.svg'
|
|
|
|
let cmd = ''
|
2019-01-05 12:47:06 +01:00
|
|
|
// Save svg to disk
|
2019-01-06 14:53:28 +01:00
|
|
|
fs.writeFile(svg, req.body.svg, err => {
|
2019-08-03 15:53:41 +02:00
|
|
|
if (err) {
|
|
|
|
console.log(err)
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
let target = storage + dir + '/pattern-' + req.body.size
|
|
|
|
if (req.body.size === 'full') {
|
|
|
|
// Do not tile
|
|
|
|
if (req.body.format === 'ps') {
|
|
|
|
target += '.ps'
|
|
|
|
cmd = '/usr/bin/inkscape --export-ps=' + target + ' ' + svg
|
|
|
|
shellExec(cmd).then(() => {
|
|
|
|
return res.send({ link: process.env.TILER_DOWNLOAD + '/tmp/' + dir + '/pattern-full.ps' })
|
|
|
|
})
|
2019-06-23 15:58:18 +00:00
|
|
|
} else {
|
2019-08-03 15:53:41 +02:00
|
|
|
target += '.pdf'
|
|
|
|
cmd = '/usr/bin/inkscape --export-pdf=' + target + ' ' + svg
|
|
|
|
shellExec(cmd).then(() => {
|
|
|
|
return res.send({
|
|
|
|
link: process.env.TILER_DOWNLOAD + '/tmp/' + dir + '/pattern-full.pdf'
|
|
|
|
})
|
|
|
|
})
|
2019-06-23 15:58:18 +00:00
|
|
|
}
|
2019-08-03 15:53:41 +02:00
|
|
|
} else {
|
|
|
|
// Do tile
|
|
|
|
target += '.pdf'
|
|
|
|
let untiled = storage + dir + '/untiled.ps'
|
|
|
|
let tiled = storage + dir + '/tiled.ps'
|
|
|
|
cmd = `/usr/bin/inkscape --export-ps=${untiled} ${svg}`
|
|
|
|
shellExec(cmd).then(() => {
|
|
|
|
cmd = `/usr/local/bin/tile -a -m${
|
|
|
|
req.body.size
|
|
|
|
} -s1 -t"freesewing.org" ${untiled} > ${tiled}`
|
|
|
|
console.log('tile cmd', cmd)
|
|
|
|
shellExec(cmd).then(() => {
|
|
|
|
cmd = `/usr/bin/ps2pdf14 ${tiled} ${target}`
|
|
|
|
shellExec(cmd).then(() => {
|
|
|
|
return res.send({
|
|
|
|
link:
|
|
|
|
process.env.TILER_DOWNLOAD + '/tmp/' + dir + '/pattern-' + req.body.size + '.pdf'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-01-05 19:28:48 +00:00
|
|
|
|
2019-08-03 15:53:41 +02:00
|
|
|
const createTempDir = folder => {
|
|
|
|
let dir = newDir()
|
|
|
|
let path = folder + dir
|
|
|
|
fs.mkdirSync(path, { recursive: true }, err => {
|
|
|
|
if (err) console.log('mkdirFailed', err)
|
|
|
|
})
|
2019-01-05 19:28:48 +00:00
|
|
|
|
2019-08-03 15:53:41 +02:00
|
|
|
return dir
|
2019-01-05 19:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const newDir = (length = 10) => {
|
2019-08-03 15:53:41 +02:00
|
|
|
let dir = ''
|
|
|
|
let possible = 'abcdefghijklmnopqrstuvwxyz'
|
2019-01-05 19:28:48 +00:00
|
|
|
for (let i = 0; i < length; i++)
|
2019-08-03 15:53:41 +02:00
|
|
|
dir += possible.charAt(Math.floor(Math.random() * possible.length))
|
2019-01-05 19:28:48 +00:00
|
|
|
|
2019-08-03 15:53:41 +02:00
|
|
|
return dir
|
2019-01-05 19:28:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-03 15:53:41 +02:00
|
|
|
app.listen(port, err => {
|
|
|
|
console.log(`> listening on port ${port}`)
|
|
|
|
})
|