diff --git a/index.js b/index.js index 1c6198e..d2d0b90 100644 --- a/index.js +++ b/index.js @@ -5,10 +5,12 @@ const app = express(); const templates = require('./templates'); const renderer = require('./renderer'); +const {getPdfPath} = require('./utils'); + app.use(bodyParser.json()); -app.options('/generate/pdf/:template', cors()); -app.post('/generate/pdf/:template', cors(), (req, res) => { +app.options('/pdf/generate/:template', cors()); +app.post('/pdf/generate/:template', cors(), (req, res) => { const templateName = req.params.template; const options = req.body; @@ -20,19 +22,25 @@ app.post('/generate/pdf/:template', cors(), (req, res) => { return; } - renderer(texDocument, (err, pdf) => { + renderer(texDocument, (err, pdfFilePath) => { if (err) { console.error('Error:', err.code, 'for', req.url); res.sendStatus(500).end('Something went wrong while baking the PDF'); return; } - res.setHeader('Content-Type', 'application/pdf'); - res.send(pdf); + const id = pdfFilePath.replace('/tmp/', ''); + res.send({id: id}); res.end(); }); }); }); +app.options('/pdf/:id', cors()); +app.get('/pdf/:id', cors(), (req, res) => { + const {id} = req.params; + res.sendFile(getPdfPath(id)); +}); + app.use(express.static('dist')); app.listen(5000); diff --git a/package.json b/package.json index 0621b77..6a68356 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "preact": "^7.2.0", "preact-compat": "^3.13.1", "preact-router": "^2.4.1", - "react-pdf": "0.0.10" + "react-pdf": "0.0.10", + "uuid": "^3.0.1" }, "devDependencies": { "babel-core": "^6.23.1", diff --git a/renderer.js b/renderer.js index 22ca710..dc92149 100644 --- a/renderer.js +++ b/renderer.js @@ -1,26 +1,32 @@ const fs = require('fs'); -const path = require('path'); const spawn = require('child_process').spawn; +const uuid = require('uuid'); + +const {getDirPath, getDocPath} = require('./utils'); + function copyToTemp(texDocument, callback) { - fs.mkdtemp('/tmp/pdfer-', (err, tmpPath) => { + const id = uuid.v1(); + const dirPath = getDirPath(id); + + fs.mkdir(dirPath, (err) => { if (err) { callback(err); return; } - const docPath = path.join(tmpPath, 'doc.tex'); + const docPath = getDocPath(id); fs.writeFile(docPath, texDocument, (err) => { if (err) { callback(err); } - callback(null, docPath); + callback(null, id); }); }); } -function generateDoc(docPath, callback) { - const pdflatex = spawn('pdflatex', [docPath, '-interaction', 'nonstopmode'], {cwd: path.dirname(docPath)}); +function generateDoc(id, callback) { + const pdflatex = spawn('pdflatex', [getDocPath(id), '-interaction', 'nonstopmode'], {cwd: getDirPath(id)}); pdflatex.stderr.on('data', (data) => { console.error('onData', data); }); @@ -31,19 +37,12 @@ function generateDoc(docPath, callback) { return; } console.log('PDF generated'); - const pdfFilePath = docPath.replace('.tex', '.pdf'); - fs.readFile(pdfFilePath, (err, data) => { - if (err) { - callback(err); - return; - } - callback(null, data); - }); + callback(null, id); }); } module.exports = (texDocument, callback) => { - copyToTemp(texDocument, (err, docPath) => { - generateDoc(docPath, callback); + copyToTemp(texDocument, (err, id) => { + generateDoc(id, callback); }); }; diff --git a/utils.js b/utils.js new file mode 100644 index 0000000..e863ac0 --- /dev/null +++ b/utils.js @@ -0,0 +1,19 @@ +const path = require('path'); + +function getDirPath(id) { + return `/tmp/pdfer-${id}`; +} + +function getDocPath(id) { + return path.join(getDirPath(id), 'doc.tex'); +} + +function getPdfPath(id) { + return path.join(getDirPath(id), 'doc.pdf'); +} + +module.exports = { + getDirPath, + getDocPath, + getPdfPath +}; diff --git a/webapp/index.html b/webapp/index.html index a6884f7..efef6d9 100644 --- a/webapp/index.html +++ b/webapp/index.html @@ -10,6 +10,7 @@