Check monetizejs token

This commit is contained in:
benweet 2014-08-19 00:30:45 +01:00
parent 2339543fd4
commit 7e0444524a
3 changed files with 93 additions and 73 deletions

View File

@ -8,12 +8,10 @@ app.engine('html', require('ejs').renderFile);
// Force HTTPS on stackedit.io // Force HTTPS on stackedit.io
app.all('*', function(req, res, next) { app.all('*', function(req, res, next) {
if (req.headers.host == 'stackedit.io' && req.headers['x-forwarded-proto'] != 'https') { if (req.headers.host == 'stackedit.io' && req.headers['x-forwarded-proto'] != 'https') {
res.redirect('https://stackedit.io' + req.url); return res.redirect('https://stackedit.io' + req.url);
}
else {
/\.(eot|ttf|woff)$/.test(req.url) && res.header('Access-Control-Allow-Origin', '*');
next();
} }
/\.(eot|ttf|woff)$/.test(req.url) && res.header('Access-Control-Allow-Origin', '*');
next();
}); });
// Use gzip compression // Use gzip compression

View File

@ -1,5 +1,8 @@
var spawn = require('child_process').spawn; var spawn = require('child_process').spawn;
var fs = require('fs'); var fs = require('fs');
var path = require('path');
var os = require('os');
var request = require('request');
function waitForJavaScript() { function waitForJavaScript() {
if(window.MathJax) { if(window.MathJax) {
@ -37,53 +40,6 @@ var authorizedPageSizes = [
]; ];
module.exports = function(req, res, next) { module.exports = function(req, res, next) {
var options, params = [];
try {
options = JSON.parse(req.query.options);
}
catch(e) {
options = {};
}
// Margins
var marginTop = parseInt(options.marginTop);
params.push('-T', isNaN(marginTop) ? 25 : marginTop);
var marginRight = parseInt(options.marginRight);
params.push('-R', isNaN(marginRight) ? 25 : marginRight);
var marginBottom = parseInt(options.marginBottom);
params.push('-B', isNaN(marginBottom) ? 25 : marginBottom);
var marginLeft = parseInt(options.marginLeft);
params.push('-L', isNaN(marginLeft) ? 25 : marginLeft);
// Header
options.headerCenter && params.push('--header-center', options.headerCenter);
options.headerLeft && params.push('--header-left', options.headerLeft);
options.headerRight && params.push('--header-left', options.headerRight);
options.headerFontName && params.push('--header-font-name ', options.headerFontName);
options.headerFontSize && params.push('--header-font-size ', options.headerFontSize);
// Footer
options.footerCenter && params.push('--footer-center', options.footerCenter);
options.footerLeft && params.push('--footer-left', options.footerLeft);
options.footerRight && params.push('--footer-left', options.footerRight);
options.footerFontName && params.push('--footer-font-name ', options.footerFontName);
options.footerFontSize && params.push('--footer-font-size ', options.footerFontSize);
// Page size
params.push('--page-size', authorizedPageSizes.indexOf(options.pageSize) === -1 ? 'A4' : options.pageSize);
// wkhtmltopdf can't access /dev/stdout on Amazon EC2 for some reason
var filePath = '/tmp/' + Date.now() + '.pdf';
var binPath = process.env.WKHTMLTOPDF_PATH || 'wkhtmltopdf';
params.push('--run-script', waitForJavaScript.toString() + 'waitForJavaScript()');
params.push('--window-status', 'done');
var wkhtmltopdf = spawn(binPath, params.concat('-', filePath), {
stdio: [
'pipe',
'ignore',
'ignore'
]
});
function onError(err) { function onError(err) {
next(err); next(err);
} }
@ -91,33 +47,98 @@ module.exports = function(req, res, next) {
res.statusCode = 400; res.statusCode = 400;
res.end('Unknown error'); res.end('Unknown error');
} }
function onUnauthorizedError() {
res.statusCode = 401;
res.end('Unauthorized');
}
function onTimeout() { function onTimeout() {
res.statusCode = 408; res.statusCode = 408;
res.end('Request timeout'); res.end('Request timeout');
} }
var timeoutId = setTimeout(function() { request({
timeoutId = undefined; uri: 'https://monetizejs.com/api/payments',
wkhtmltopdf.kill(); qs: {
}, 30000); access_token: req.query.token
wkhtmltopdf.on('error', onError); },
wkhtmltopdf.stdin.on('error', onError); json: true
wkhtmltopdf.on('close', function(code) { }, function (err, paymentsRes, payments) {
if(!timeoutId) { var authorized = payments && payments.app == 'ESTHdCYOi18iLhhO' && (
return onTimeout(); (payments.chargeOption && payments.chargeOption.alias == 'once') ||
(payments.subscriptionOption && payments.subscriptionOption.alias == 'yearly'));
if(err || paymentsRes.statusCode != 200 || !authorized) {
return onUnauthorizedError();
} }
clearTimeout(timeoutId); var options, params = [];
if(code) { try {
return onUnknownError(); options = JSON.parse(req.query.options);
} }
var readStream = fs.createReadStream(filePath); catch(e) {
readStream.on('open', function() { options = {};
readStream.pipe(res); }
// Margins
var marginTop = parseInt(options.marginTop);
params.push('-T', isNaN(marginTop) ? 25 : marginTop);
var marginRight = parseInt(options.marginRight);
params.push('-R', isNaN(marginRight) ? 25 : marginRight);
var marginBottom = parseInt(options.marginBottom);
params.push('-B', isNaN(marginBottom) ? 25 : marginBottom);
var marginLeft = parseInt(options.marginLeft);
params.push('-L', isNaN(marginLeft) ? 25 : marginLeft);
// Header
options.headerCenter && params.push('--header-center', options.headerCenter);
options.headerLeft && params.push('--header-left', options.headerLeft);
options.headerRight && params.push('--header-left', options.headerRight);
options.headerFontName && params.push('--header-font-name ', options.headerFontName);
options.headerFontSize && params.push('--header-font-size ', options.headerFontSize);
// Footer
options.footerCenter && params.push('--footer-center', options.footerCenter);
options.footerLeft && params.push('--footer-left', options.footerLeft);
options.footerRight && params.push('--footer-left', options.footerRight);
options.footerFontName && params.push('--footer-font-name ', options.footerFontName);
options.footerFontSize && params.push('--footer-font-size ', options.footerFontSize);
// Page size
params.push('--page-size', authorizedPageSizes.indexOf(options.pageSize) === -1 ? 'A4' : options.pageSize);
// Use a temp file as wkhtmltopdf can't access /dev/stdout on Amazon EC2 for some reason
var filePath = path.join(os.tmpDir(), Date.now() + '.pdf');
var binPath = process.env.WKHTMLTOPDF_PATH || 'wkhtmltopdf';
params.push('--run-script', waitForJavaScript.toString() + 'waitForJavaScript()');
params.push('--window-status', 'done');
var wkhtmltopdf = spawn(binPath, params.concat('-', filePath), {
stdio: [
'pipe',
'ignore',
'ignore'
]
}); });
readStream.on('close', function() { var timeoutId = setTimeout(function() {
fs.unlink(filePath, function() { timeoutId = undefined;
wkhtmltopdf.kill();
}, 30000);
wkhtmltopdf.on('error', onError);
wkhtmltopdf.stdin.on('error', onError);
wkhtmltopdf.on('close', function(code) {
if(!timeoutId) {
return onTimeout();
}
clearTimeout(timeoutId);
if(code) {
return onUnknownError();
}
var readStream = fs.createReadStream(filePath);
readStream.on('open', function() {
readStream.pipe(res);
}); });
readStream.on('close', function() {
fs.unlink(filePath, function() {
});
});
readStream.on('error', onUnknownError);
}); });
readStream.on('error', onUnknownError); req.pipe(wkhtmltopdf.stdin);
}); });
req.pipe(wkhtmltopdf.stdin);
}; };

View File

@ -11,8 +11,9 @@
"doc": "doc" "doc": "doc"
}, },
"dependencies": { "dependencies": {
"express": "3.x", "express": "~3.16.6",
"ejs": "~0.8.4" "ejs": "~0.8.4",
"request": "~2.40.0"
}, },
"devDependencies": { "devDependencies": {
"grunt-contrib-requirejs": "~0.4.3", "grunt-contrib-requirejs": "~0.4.3",