mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 06:27:19 +01:00
use promises
This commit is contained in:
@@ -10,28 +10,19 @@ app.use(bodyParser.json());
|
||||
|
||||
app.options('/api/pdf/generate/:template');
|
||||
app.post('/api/pdf/generate/:template', (req, res) => {
|
||||
|
||||
const templateName = req.params.template;
|
||||
const options = req.body;
|
||||
|
||||
templates.get(templateName, options, (err, texDocument) => {
|
||||
if (err) {
|
||||
console.error('Error:', err.code, 'for', req.url);
|
||||
res.sendStatus(500).end('Something went wrong while generating Tex source');
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
const id = pdfFilePath.replace('/tmp/', '');
|
||||
templates.get(templateName, options)
|
||||
.then(renderer)
|
||||
.then(id => {
|
||||
res.send({id: id});
|
||||
res.end();
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('Error:', err, 'for', req.url);
|
||||
res.sendStatus(500).end(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
app.options('/api/pdf/:id');
|
||||
|
||||
@@ -5,48 +5,47 @@ const uuid = require('uuid');
|
||||
const {getDirPath, getDocPath} = require('./utils');
|
||||
|
||||
|
||||
function copyToTemp(id, texDocument, callback) {
|
||||
const dirPath = getDirPath(id);
|
||||
function copyToTemp(id, texDocument) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const dirPath = getDirPath(id);
|
||||
|
||||
fs.mkdir(dirPath, (err) => {
|
||||
if (err) {
|
||||
callback(err);
|
||||
return;
|
||||
}
|
||||
|
||||
const docPath = getDocPath(id);
|
||||
fs.writeFile(docPath, texDocument, (err) => {
|
||||
fs.mkdir(dirPath, (err) => {
|
||||
if (err) {
|
||||
callback(err);
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
callback(null);
|
||||
|
||||
const docPath = getDocPath(id);
|
||||
fs.writeFile(docPath, texDocument, (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
resolve(id);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function generateDoc(id, callback) {
|
||||
const pdflatex = spawn('pdflatex', [getDocPath(id), '-interaction', 'nonstopmode'], {cwd: getDirPath(id)});
|
||||
pdflatex.stderr.on('data', (data) => {
|
||||
console.error('onData', data);
|
||||
});
|
||||
function generateDoc(id) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const pdflatex = spawn('pdflatex', ['-interaction', 'nonstopmode', getDocPath(id)], {cwd: getDirPath(id)});
|
||||
pdflatex.stderr.on('data', (data) => {
|
||||
console.error('onData', data);
|
||||
});
|
||||
|
||||
pdflatex.on('close', (code) => {
|
||||
if (code > 0) {
|
||||
callback(`pdflatex returned with code ${code}`);
|
||||
return;
|
||||
}
|
||||
console.log(`PDF ${id} generated`);
|
||||
callback(null, id);
|
||||
pdflatex.on('close', (code) => {
|
||||
if (code > 0) {
|
||||
reject(`pdflatex returned with code ${code}`);
|
||||
return;
|
||||
}
|
||||
console.log(`PDF ${id} generated`);
|
||||
resolve(id);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = (texDocument, callback) => {
|
||||
module.exports = function(texDocument, callback) {
|
||||
const id = uuid.v1();
|
||||
copyToTemp(id, texDocument, (err) => {
|
||||
if (err) {
|
||||
callback(err);
|
||||
return;
|
||||
}
|
||||
generateDoc(id, callback);
|
||||
});
|
||||
return copyToTemp(id, texDocument)
|
||||
.then(generateDoc);
|
||||
};
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
const path = require('path');
|
||||
|
||||
module.exports.get = (templateName, options, callback) => {
|
||||
const modulePath = path.join(__dirname, 'templates', templateName);
|
||||
let template;
|
||||
|
||||
try {
|
||||
template = require(modulePath);
|
||||
callback(null, template(options));
|
||||
} catch (e) {
|
||||
callback(`${templateName} not found!`);
|
||||
}
|
||||
module.exports.get = function(templateName, options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const modulePath = path.join(__dirname, 'templates', templateName);
|
||||
try {
|
||||
const template = require(modulePath);
|
||||
resolve(template(options));
|
||||
} catch (e) {
|
||||
reject(`${templateName} not found!`);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user