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.options('/api/pdf/generate/:template');
|
||||||
app.post('/api/pdf/generate/:template', (req, res) => {
|
app.post('/api/pdf/generate/:template', (req, res) => {
|
||||||
|
|
||||||
const templateName = req.params.template;
|
const templateName = req.params.template;
|
||||||
const options = req.body;
|
const options = req.body;
|
||||||
|
|
||||||
templates.get(templateName, options, (err, texDocument) => {
|
templates.get(templateName, options)
|
||||||
if (err) {
|
.then(renderer)
|
||||||
console.error('Error:', err.code, 'for', req.url);
|
.then(id => {
|
||||||
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/', '');
|
|
||||||
res.send({id: id});
|
res.send({id: id});
|
||||||
res.end();
|
res.end();
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error('Error:', err, 'for', req.url);
|
||||||
|
res.sendStatus(500).end(err);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.options('/api/pdf/:id');
|
app.options('/api/pdf/:id');
|
||||||
|
|||||||
@@ -5,48 +5,47 @@ const uuid = require('uuid');
|
|||||||
const {getDirPath, getDocPath} = require('./utils');
|
const {getDirPath, getDocPath} = require('./utils');
|
||||||
|
|
||||||
|
|
||||||
function copyToTemp(id, texDocument, callback) {
|
function copyToTemp(id, texDocument) {
|
||||||
const dirPath = getDirPath(id);
|
return new Promise((resolve, reject) => {
|
||||||
|
const dirPath = getDirPath(id);
|
||||||
|
|
||||||
fs.mkdir(dirPath, (err) => {
|
fs.mkdir(dirPath, (err) => {
|
||||||
if (err) {
|
|
||||||
callback(err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const docPath = getDocPath(id);
|
|
||||||
fs.writeFile(docPath, texDocument, (err) => {
|
|
||||||
if (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) {
|
function generateDoc(id) {
|
||||||
const pdflatex = spawn('pdflatex', [getDocPath(id), '-interaction', 'nonstopmode'], {cwd: getDirPath(id)});
|
return new Promise((resolve, reject) => {
|
||||||
pdflatex.stderr.on('data', (data) => {
|
const pdflatex = spawn('pdflatex', ['-interaction', 'nonstopmode', getDocPath(id)], {cwd: getDirPath(id)});
|
||||||
console.error('onData', data);
|
pdflatex.stderr.on('data', (data) => {
|
||||||
});
|
console.error('onData', data);
|
||||||
|
});
|
||||||
|
|
||||||
pdflatex.on('close', (code) => {
|
pdflatex.on('close', (code) => {
|
||||||
if (code > 0) {
|
if (code > 0) {
|
||||||
callback(`pdflatex returned with code ${code}`);
|
reject(`pdflatex returned with code ${code}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log(`PDF ${id} generated`);
|
console.log(`PDF ${id} generated`);
|
||||||
callback(null, id);
|
resolve(id);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = (texDocument, callback) => {
|
module.exports = function(texDocument, callback) {
|
||||||
const id = uuid.v1();
|
const id = uuid.v1();
|
||||||
copyToTemp(id, texDocument, (err) => {
|
return copyToTemp(id, texDocument)
|
||||||
if (err) {
|
.then(generateDoc);
|
||||||
callback(err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
generateDoc(id, callback);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
module.exports.get = (templateName, options, callback) => {
|
module.exports.get = function(templateName, options) {
|
||||||
const modulePath = path.join(__dirname, 'templates', templateName);
|
return new Promise((resolve, reject) => {
|
||||||
let template;
|
const modulePath = path.join(__dirname, 'templates', templateName);
|
||||||
|
try {
|
||||||
try {
|
const template = require(modulePath);
|
||||||
template = require(modulePath);
|
resolve(template(options));
|
||||||
callback(null, template(options));
|
} catch (e) {
|
||||||
} catch (e) {
|
reject(`${templateName} not found!`);
|
||||||
callback(`${templateName} not found!`);
|
}
|
||||||
}
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user