HackMD/lib/response.js

477 lines
14 KiB
JavaScript
Raw Normal View History

2015-05-04 07:53:29 +00:00
//response
//external modules
var ejs = require('ejs');
var fs = require('fs');
var path = require('path');
var markdownpdf = require("markdown-pdf");
var LZString = require('lz-string');
2015-07-01 16:10:20 +00:00
var S = require('string');
var shortId = require('shortid');
var metaMarked = require('meta-marked');
2016-01-31 21:42:26 +00:00
var querystring = require('querystring');
var request = require('request');
2015-05-04 07:53:29 +00:00
//core
var config = require("./config.js");
var logger = require("./logger.js");
var models = require("./models");
2015-05-04 07:53:29 +00:00
//slides
var md = require('reveal.js/plugin/markdown/markdown');
var Mustache = require('mustache');
//reveal.js
var opts = {
template: fs.readFileSync(config.slidepath).toString(),
theme: 'css/theme/black.css',
highlightTheme: 'zenburn',
separator: '^(\r\n?|\n)---(\r\n?|\n)$',
verticalSeparator: '^(\r\n?|\n)----(\r\n?|\n)$',
revealOptions: {}
};
2015-05-04 07:53:29 +00:00
//public
var response = {
errorForbidden: function (res) {
2015-12-30 05:33:36 +00:00
responseError(res, "403", "Forbidden", "oh no.");
2015-05-04 07:53:29 +00:00
},
errorNotFound: function (res) {
2015-05-15 04:58:13 +00:00
responseError(res, "404", "Not Found", "oops.");
2015-05-04 07:53:29 +00:00
},
errorInternalError: function (res) {
2015-05-15 04:58:13 +00:00
responseError(res, "500", "Internal Error", "wtf.");
2015-05-04 07:53:29 +00:00
},
errorServiceUnavailable: function (res) {
2015-05-15 04:58:13 +00:00
res.status(503).send("I'm busy right now, try again later.");
2015-05-04 07:53:29 +00:00
},
newNote: newNote,
showNote: showNote,
showPublishNote: showPublishNote,
showPublishSlide: showPublishSlide,
showIndex: showIndex,
2015-07-01 16:10:20 +00:00
noteActions: noteActions,
2016-01-31 21:42:26 +00:00
publishNoteActions: publishNoteActions,
githubActions: githubActions
2015-05-04 07:53:29 +00:00
};
function responseError(res, code, detail, msg) {
res.writeHead(code, {
'Content-Type': 'text/html'
});
2015-07-01 16:10:20 +00:00
var template = config.errorpath;
var options = {
cache: !config.debug,
filename: template
};
var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options);
var content = compiled({
url: config.serverurl,
2015-07-01 16:10:20 +00:00
title: code + ' ' + detail + ' ' + msg,
2015-05-04 07:53:29 +00:00
cache: !config.debug,
2015-07-01 16:10:20 +00:00
filename: template,
2015-05-04 07:53:29 +00:00
code: code,
detail: detail,
2015-09-22 14:39:13 +00:00
msg: msg,
useCDN: config.usecdn,
googleDrive: config.googleDrive,
2015-05-04 07:53:29 +00:00
});
res.write(content);
res.end();
}
function showIndex(req, res, next) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
var template = config.indexpath;
var options = {
cache: !config.debug,
filename: template
};
var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options);
var content = compiled({
url: config.serverurl,
useCDN: config.usecdn,
facebook: config.facebook,
twitter: config.twitter,
github: config.github,
dropbox: config.dropbox,
googleDrive: config.googleDrive,
});
res.write(content);
res.end();
}
function responseHackMD(res, note) {
var body = LZString.decompressFromBase64(note.content);
var meta = null;
try {
meta = metaMarked(body).meta;
} catch(err) {
//na
}
var title = models.Note.decodeTitle(note.title);
title = models.Note.generateWebTitle(title);
var template = config.hackmdpath;
var options = {
cache: !config.debug,
filename: template
};
var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options);
var html = compiled({
url: config.serverurl,
title: title,
useCDN: config.usecdn,
robots: (meta && meta.robots) || false, //default allow robots
facebook: config.facebook,
twitter: config.twitter,
github: config.github,
dropbox: config.dropbox,
googleDrive: config.googleDrive,
2015-05-04 07:53:29 +00:00
});
var buf = html;
res.writeHead(200, {
'Content-Type': 'text/html; charset=UTF-8',
'Cache-Control': 'private',
'Content-Length': buf.length
});
res.end(buf);
2015-05-04 07:53:29 +00:00
}
function newNote(req, res, next) {
var owner = null;
if (req.isAuthenticated()) {
owner = req.user.id;
2015-05-04 07:53:29 +00:00
}
models.Note.create({
ownerId: owner
}).then(function (note) {
return res.redirect(config.serverurl + "/" + LZString.compressToBase64(note.id));
}).catch(function (err) {
logger.error(err);
return response.errorInternalError(res);
2015-05-04 07:53:29 +00:00
});
}
function checkViewPermission(req, note) {
if (note.permission == 'private') {
if (!req.isAuthenticated() || note.ownerId != req.user.id)
return false;
else
return true;
} else {
return true;
}
2015-05-04 07:53:29 +00:00
}
function findNote(req, res, callback, include) {
var id = req.params.noteId || req.params.shortid;
models.Note.parseNoteId(id, function (err, _id) {
models.Note.findOne({
where: {
id: _id
},
include: include || null
}).then(function (note) {
if (!note) {
return response.errorNotFound(res);
}
if (!checkViewPermission(req, note)) {
return response.errorForbidden(res);
} else {
return callback(note);
}
}).catch(function (err) {
logger.error(err);
return response.errorInternalError(res);
});
});
2015-05-04 07:53:29 +00:00
}
function showNote(req, res, next) {
findNote(req, res, function (note) {
return responseHackMD(res, note);
});
}
function showPublishNote(req, res, next) {
var include = [{
model: models.User,
as: "owner"
}, {
model: models.User,
as: "lastchangeuser"
}];
findNote(req, res, function (note) {
note.increment('viewcount').then(function (note) {
if (!note) {
return response.errorNotFound(res);
2015-07-01 16:10:20 +00:00
}
var body = LZString.decompressFromBase64(note.content);
var meta = null;
try {
meta = metaMarked(body).meta;
} catch(err) {
//na
}
var createtime = note.createdAt;
var updatetime = note.lastchangeAt;
var text = S(body).escapeHTML().s;
var title = models.Note.decodeTitle(note.title);
title = models.Note.generateWebTitle(title);
var origin = config.serverurl;
var data = {
title: title,
viewcount: note.viewcount,
createtime: createtime,
updatetime: updatetime,
url: origin,
body: text,
useCDN: config.usecdn,
lastchangeuserprofile: note.lastchangeuser ? models.User.parseProfile(note.lastchangeuser.profile) : null,
robots: (meta && meta.robots) || false //default allow robots
};
return renderPublish(data, res);
}).catch(function (err) {
logger.error(err);
return response.errorInternalError(res);
2015-07-01 16:10:20 +00:00
});
}, include);
2015-07-01 16:10:20 +00:00
}
function renderPublish(data, res) {
var template = config.prettypath;
var options = {
url: config.serverurl,
cache: !config.debug,
filename: template
};
var compiled = ejs.compile(fs.readFileSync(template, 'utf8'), options);
var html = compiled(data);
var buf = html;
res.writeHead(200, {
'Content-Type': 'text/html; charset=UTF-8',
'Cache-Control': 'private',
'Content-Length': buf.length
});
res.end(buf);
}
function actionPublish(req, res, note) {
res.redirect(config.serverurl + "/s/" + (note.alias || note.shortid));
2015-07-01 16:10:20 +00:00
}
function actionSlide(req, res, note) {
res.redirect(config.serverurl + "/p/" + (note.alias || note.shortid));
}
2015-05-04 07:53:29 +00:00
function actionDownload(req, res, note) {
var body = LZString.decompressFromBase64(note.content);
var title = models.Note.decodeTitle(note.title);
var filename = title;
filename = encodeURIComponent(filename);
res.writeHead(200, {
'Access-Control-Allow-Origin': '*', //allow CORS as API
'Access-Control-Allow-Headers': 'Range',
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
'Content-Type': 'text/markdown; charset=UTF-8',
'Cache-Control': 'private',
'Content-disposition': 'attachment; filename=' + filename + '.md',
'Content-Length': body.length,
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
2015-05-04 07:53:29 +00:00
});
res.end(body);
2015-05-04 07:53:29 +00:00
}
function actionPDF(req, res, note) {
var body = LZString.decompressFromBase64(note.content);
try {
body = metaMarked(body).markdown;
} catch(err) {
//na
}
var title = models.Note.decodeTitle(note.title);
2015-05-04 07:53:29 +00:00
if (!fs.existsSync(config.tmppath)) {
fs.mkdirSync(config.tmppath);
}
var path = config.tmppath + Date.now() + '.pdf';
markdownpdf().from.string(body).to(path, function () {
var stream = fs.createReadStream(path);
var filename = title;
// Be careful of special characters
filename = encodeURIComponent(filename);
// Ideally this should strip them
res.setHeader('Content-disposition', 'attachment; filename="' + filename + '.pdf"');
res.setHeader('Cache-Control', 'private');
res.setHeader('Content-Type', 'application/pdf; charset=UTF-8');
res.setHeader('X-Robots-Tag', 'noindex, nofollow'); // prevent crawling
stream.pipe(res);
fs.unlink(path);
2015-05-04 07:53:29 +00:00
});
}
function actionGist(req, res, note) {
var data = {
client_id: config.github.clientID,
redirect_uri: config.serverurl + '/auth/github/callback/' + LZString.compressToBase64(note.id) + '/gist',
scope: "gist",
state: shortId.generate()
};
var query = querystring.stringify(data);
res.redirect("https://github.com/login/oauth/authorize?" + query);
2016-01-31 21:42:26 +00:00
}
2015-05-04 07:53:29 +00:00
function noteActions(req, res, next) {
var noteId = req.params.noteId;
findNote(req, res, function (note) {
var action = req.params.action;
switch (action) {
case "publish":
case "pretty": //pretty deprecated
actionPublish(req, res, note);
break;
case "slide":
actionSlide(req, res, note);
break;
case "download":
actionDownload(req, res, note);
break;
case "pdf":
actionPDF(req, res, note);
break;
case "gist":
actionGist(req, res, note);
break;
default:
return res.redirect(config.serverurl + '/' + noteId);
break;
2015-05-04 07:53:29 +00:00
}
});
2015-05-04 07:53:29 +00:00
}
function publishNoteActions(req, res, next) {
findNote(req, res, function (note) {
var action = req.params.action;
switch (action) {
case "edit":
res.redirect(config.serverurl + '/' + (note.alias ? note.alias : LZString.compressToBase64(note.id)));
break;
default:
res.redirect(config.serverurl + '/s/' + note.shortid);
break;
}
});
2015-07-01 16:10:20 +00:00
}
2016-01-31 21:42:26 +00:00
function githubActions(req, res, next) {
var noteId = req.params.noteId;
findNote(req, res, function (note) {
var action = req.params.action;
switch (action) {
case "gist":
githubActionGist(req, res, note);
break;
default:
res.redirect(config.serverurl + '/' + noteId);
break;
2016-01-31 21:42:26 +00:00
}
});
}
function githubActionGist(req, res, note) {
var code = req.query.code;
var state = req.query.state;
if (!code || !state) {
return response.errorForbidden(res);
} else {
var data = {
client_id: config.github.clientID,
client_secret: config.github.clientSecret,
code: code,
state: state
2016-01-31 21:42:26 +00:00
}
var auth_url = 'https://github.com/login/oauth/access_token';
request({
url: auth_url,
method: "POST",
json: data
}, function (error, httpResponse, body) {
if (!error && httpResponse.statusCode == 200) {
var access_token = body.access_token;
if (access_token) {
var content = LZString.decompressFromBase64(note.content);
var title = models.Note.decodeTitle(note.title);
var filename = title.replace('/', ' ') + '.md';
var gist = {
"files": {}
};
gist.files[filename] = {
"content": content
};
var gist_url = "https://api.github.com/gists";
request({
url: gist_url,
headers: {
'User-Agent': 'HackMD',
'Authorization': 'token ' + access_token
},
method: "POST",
json: gist
}, function (error, httpResponse, body) {
if (!error && httpResponse.statusCode == 201) {
res.setHeader('referer', '');
res.redirect(body.html_url);
} else {
return response.errorForbidden(res);
}
});
2016-01-31 21:42:26 +00:00
} else {
return response.errorForbidden(res);
}
} else {
return response.errorForbidden(res);
}
})
}
2016-01-31 21:42:26 +00:00
}
2015-07-01 16:10:20 +00:00
function showPublishSlide(req, res, next) {
findNote(req, res, function (note) {
note.increment('viewcount').then(function (note) {
if (!note) {
return response.errorNotFound(res);
}
var body = LZString.decompressFromBase64(note.content);
try {
body = metaMarked(body).markdown;
} catch(err) {
//na
}
var title = models.Note.decodeTitle(note.title);
title = models.Note.generateWebTitle(title);
var text = S(body).escapeHTML().s;
render(res, title, text);
}).catch(function (err) {
logger.error(err);
return response.errorInternalError(res);
});
});
}
//reveal.js render
2015-12-18 15:40:52 +00:00
var render = function (res, title, markdown) {
var slides = md.slidify(markdown, opts);
res.end(Mustache.to_html(opts.template, {
url: config.serverurl,
2015-12-18 15:40:52 +00:00
title: title,
theme: opts.theme,
highlightTheme: opts.highlightTheme,
slides: slides,
options: JSON.stringify(opts.revealOptions, null, 2)
}));
};
module.exports = response;