2015-05-04 03:53:29 -04:00
|
|
|
//response
|
|
|
|
//external modules
|
|
|
|
var ejs = require('ejs');
|
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var uuid = require('node-uuid');
|
|
|
|
var markdownpdf = require("markdown-pdf");
|
|
|
|
var LZString = require('lz-string');
|
2015-07-01 12:10:20 -04:00
|
|
|
var S = require('string');
|
|
|
|
var shortId = require('shortid');
|
2016-01-12 09:01:42 -05:00
|
|
|
var metaMarked = require('meta-marked');
|
2016-01-31 16:42:26 -05:00
|
|
|
var querystring = require('querystring');
|
|
|
|
var request = require('request');
|
2015-05-04 03:53:29 -04:00
|
|
|
|
|
|
|
//core
|
|
|
|
var config = require("../config.js");
|
|
|
|
|
|
|
|
//others
|
|
|
|
var db = require("./db.js");
|
|
|
|
var Note = require("./note.js");
|
2016-01-12 09:01:42 -05:00
|
|
|
var User = require("./user.js");
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2015-11-23 07:38:26 -05:00
|
|
|
//slides
|
|
|
|
var md = require('reveal.js/plugin/markdown/markdown');
|
|
|
|
var Mustache = require('mustache');
|
|
|
|
|
2015-11-29 02:04:20 -05:00
|
|
|
//reveal.js
|
2015-11-23 07:38:26 -05:00
|
|
|
var opts = {
|
|
|
|
userBasePath: process.cwd(),
|
|
|
|
revealBasePath: path.resolve(require.resolve('reveal.js'), '..', '..'),
|
2015-12-18 10:40:52 -05:00
|
|
|
template: fs.readFileSync(path.join('.', '/public/views/slide', 'reveal.hbs')).toString(),
|
|
|
|
templateListing: fs.readFileSync(path.join('.', '/public/views/slide', 'listing.hbs')).toString(),
|
2015-11-29 02:04:20 -05:00
|
|
|
theme: 'css/theme/black.css',
|
2015-11-23 07:38:26 -05:00
|
|
|
highlightTheme: 'zenburn',
|
|
|
|
separator: '^(\r\n?|\n)---(\r\n?|\n)$',
|
|
|
|
verticalSeparator: '^(\r\n?|\n)----(\r\n?|\n)$',
|
|
|
|
revealOptions: {}
|
|
|
|
};
|
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
//public
|
|
|
|
var response = {
|
|
|
|
errorForbidden: function (res) {
|
2015-12-30 00:33:36 -05:00
|
|
|
responseError(res, "403", "Forbidden", "oh no.");
|
2015-05-04 03:53:29 -04:00
|
|
|
},
|
|
|
|
errorNotFound: function (res) {
|
2015-05-15 00:58:13 -04:00
|
|
|
responseError(res, "404", "Not Found", "oops.");
|
2015-05-04 03:53:29 -04:00
|
|
|
},
|
|
|
|
errorInternalError: function (res) {
|
2015-05-15 00:58:13 -04:00
|
|
|
responseError(res, "500", "Internal Error", "wtf.");
|
2015-05-04 03:53:29 -04:00
|
|
|
},
|
|
|
|
errorServiceUnavailable: function (res) {
|
2015-05-15 00:58:13 -04:00
|
|
|
res.status(503).send("I'm busy right now, try again later.");
|
2015-05-04 03:53:29 -04:00
|
|
|
},
|
|
|
|
newNote: newNote,
|
|
|
|
showFeatures: showFeatures,
|
|
|
|
showNote: showNote,
|
2015-07-06 01:51:55 -04:00
|
|
|
showPublishNote: showPublishNote,
|
2015-11-23 07:38:26 -05:00
|
|
|
showPublishSlide: showPublishSlide,
|
2016-03-04 10:12:03 -05:00
|
|
|
showIndex: showIndex,
|
2015-07-01 12:10:20 -04:00
|
|
|
noteActions: noteActions,
|
2016-01-31 16:42:26 -05:00
|
|
|
publishNoteActions: publishNoteActions,
|
|
|
|
githubActions: githubActions
|
2015-05-04 03:53:29 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
function responseError(res, code, detail, msg) {
|
|
|
|
res.writeHead(code, {
|
|
|
|
'Content-Type': 'text/html'
|
|
|
|
});
|
2015-07-01 12:10:20 -04:00
|
|
|
var template = config.errorpath;
|
|
|
|
var content = ejs.render(fs.readFileSync(template, 'utf8'), {
|
2016-02-16 23:08:44 -05:00
|
|
|
url: config.getserverurl(),
|
2015-07-01 12:10:20 -04:00
|
|
|
title: code + ' ' + detail + ' ' + msg,
|
2015-05-04 03:53:29 -04:00
|
|
|
cache: !config.debug,
|
2015-07-01 12:10:20 -04:00
|
|
|
filename: template,
|
2015-05-04 03:53:29 -04:00
|
|
|
code: code,
|
|
|
|
detail: detail,
|
2015-09-22 10:39:13 -04:00
|
|
|
msg: msg,
|
2016-03-04 10:12:03 -05:00
|
|
|
useCDN: config.usecdn
|
2015-05-04 03:53:29 -04:00
|
|
|
});
|
|
|
|
res.write(content);
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
|
2015-09-22 00:06:13 -04:00
|
|
|
function showIndex(req, res, next) {
|
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Type': 'text/html'
|
|
|
|
});
|
|
|
|
var template = config.indexpath;
|
|
|
|
var content = ejs.render(fs.readFileSync(template, 'utf8'), {
|
2016-02-16 23:08:44 -05:00
|
|
|
url: config.getserverurl(),
|
2015-09-22 00:06:13 -04:00
|
|
|
useCDN: config.usecdn
|
|
|
|
});
|
|
|
|
res.write(content);
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
|
2015-07-01 12:10:20 -04:00
|
|
|
function responseHackMD(res, noteId) {
|
|
|
|
db.readFromDB(noteId, function (err, data) {
|
|
|
|
if (err) {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorNotFound(res);
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
2016-03-04 10:12:03 -05:00
|
|
|
var notedata = data.rows[0];
|
|
|
|
var body = LZString.decompressFromBase64(notedata.content);
|
2016-01-12 09:01:42 -05:00
|
|
|
var meta = null;
|
|
|
|
try {
|
|
|
|
meta = metaMarked(body).meta;
|
|
|
|
} catch(err) {
|
|
|
|
//na
|
|
|
|
}
|
2016-03-04 10:12:03 -05:00
|
|
|
var title = Note.decodeTitle(notedata.title);
|
2015-07-01 12:10:20 -04:00
|
|
|
title = 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({
|
2016-02-16 23:08:44 -05:00
|
|
|
url: config.getserverurl(),
|
2015-09-22 10:39:13 -04:00
|
|
|
title: title,
|
2016-01-12 09:01:42 -05:00
|
|
|
useCDN: config.usecdn,
|
|
|
|
robots: (meta && meta.robots) || false //default allow robots
|
2015-07-01 12:10:20 -04: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 03:53:29 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function newNote(req, res, next) {
|
|
|
|
var newId = uuid.v4();
|
|
|
|
var body = fs.readFileSync(config.defaultnotepath, 'utf8');
|
|
|
|
body = LZString.compressToBase64(body);
|
|
|
|
var owner = null;
|
|
|
|
if (req.isAuthenticated()) {
|
2016-01-17 10:51:27 -05:00
|
|
|
owner = req.user._id;
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
db.newToDB(newId, owner, body, function (err, result) {
|
|
|
|
if (err) {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorInternalError(res);
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
2016-01-17 10:51:27 -05:00
|
|
|
Note.newNote(newId, owner, function(err, result) {
|
|
|
|
if (err) {
|
|
|
|
return response.errorInternalError(res);
|
|
|
|
}
|
2016-02-16 23:08:44 -05:00
|
|
|
res.redirect(config.getserverurl() + "/" + LZString.compressToBase64(newId));
|
2016-01-17 10:51:27 -05:00
|
|
|
});
|
2015-05-04 03:53:29 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function showFeatures(req, res, next) {
|
|
|
|
db.readFromDB(config.featuresnotename, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
var body = fs.readFileSync(config.defaultfeaturespath, 'utf8');
|
|
|
|
body = LZString.compressToBase64(body);
|
|
|
|
db.newToDB(config.featuresnotename, null, body, function (err, result) {
|
|
|
|
if (err) {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorInternalError(res);
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
2015-07-01 12:10:20 -04:00
|
|
|
responseHackMD(res, config.featuresnotename);
|
2015-05-04 03:53:29 -04:00
|
|
|
});
|
|
|
|
} else {
|
2015-07-01 12:10:20 -04:00
|
|
|
responseHackMD(res, config.featuresnotename);
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function showNote(req, res, next) {
|
|
|
|
var noteId = req.params.noteId;
|
2016-01-17 10:51:27 -05:00
|
|
|
if (noteId != config.featuresnotename) {
|
|
|
|
if (!Note.checkNoteIdValid(noteId)) {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
|
|
|
noteId = LZString.decompressFromBase64(noteId);
|
|
|
|
if (!noteId) {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
2016-01-21 00:20:50 -05:00
|
|
|
db.readFromDB(noteId, function (err, data) {
|
|
|
|
if (err) {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
2016-01-21 00:20:50 -05:00
|
|
|
var notedata = data.rows[0];
|
|
|
|
Note.findOrNewNote(noteId, notedata.owner, function (err, note) {
|
|
|
|
if (err || !note) {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
|
|
|
//check view permission
|
|
|
|
if (note.permission == 'private') {
|
|
|
|
if (!req.isAuthenticated() || notedata.owner != req.user._id)
|
|
|
|
return response.errorForbidden(res);
|
|
|
|
}
|
|
|
|
responseHackMD(res, noteId);
|
|
|
|
});
|
|
|
|
});
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2015-07-06 01:51:55 -04:00
|
|
|
function showPublishNote(req, res, next) {
|
2015-07-01 12:10:20 -04:00
|
|
|
var shortid = req.params.shortid;
|
|
|
|
if (shortId.isValid(shortid)) {
|
|
|
|
Note.findNote(shortid, function (err, note) {
|
|
|
|
if (err || !note) {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorNotFound(res);
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
2016-01-17 10:51:27 -05:00
|
|
|
db.readFromDB(note.id, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
|
|
|
var notedata = data.rows[0];
|
|
|
|
//check view permission
|
|
|
|
if (note.permission == 'private') {
|
|
|
|
if (!req.isAuthenticated() || notedata.owner != req.user._id)
|
|
|
|
return response.errorForbidden(res);
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
2016-01-17 10:51:27 -05:00
|
|
|
//increase note viewcount
|
|
|
|
Note.increaseViewCount(note, function (err, note) {
|
|
|
|
if (err || !note) {
|
|
|
|
return response.errorNotFound(res);
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
2016-01-17 10:51:27 -05:00
|
|
|
var body = LZString.decompressFromBase64(notedata.content);
|
2016-01-12 09:01:42 -05:00
|
|
|
var meta = null;
|
|
|
|
try {
|
|
|
|
meta = metaMarked(body).meta;
|
|
|
|
} catch(err) {
|
|
|
|
//na
|
|
|
|
}
|
2016-01-17 10:51:27 -05:00
|
|
|
var updatetime = notedata.update_time;
|
2015-07-01 12:10:20 -04:00
|
|
|
var text = S(body).escapeHTML().s;
|
2016-03-04 10:12:03 -05:00
|
|
|
var title = Note.decodeTitle(notedata.title);
|
2015-07-01 12:10:20 -04:00
|
|
|
title = Note.generateWebTitle(title);
|
2015-11-16 12:19:01 -05:00
|
|
|
var origin = config.getserverurl();
|
2016-01-12 09:01:42 -05:00
|
|
|
var data = {
|
2015-07-01 12:10:20 -04:00
|
|
|
title: title,
|
|
|
|
viewcount: note.viewcount,
|
|
|
|
updatetime: updatetime,
|
|
|
|
url: origin,
|
2015-09-22 10:39:13 -04:00
|
|
|
body: text,
|
2016-01-12 09:01:42 -05:00
|
|
|
useCDN: config.usecdn,
|
|
|
|
lastchangeuserprofile: null,
|
|
|
|
robots: (meta && meta.robots) || false //default allow robots
|
|
|
|
};
|
|
|
|
if (note.lastchangeuser) {
|
|
|
|
//find last change user profile if lastchangeuser exists
|
|
|
|
User.findUser(note.lastchangeuser, function (err, user) {
|
|
|
|
if (!err && user && user.profile) {
|
|
|
|
var profile = JSON.parse(user.profile);
|
|
|
|
if (profile) {
|
|
|
|
data.lastchangeuserprofile = {
|
|
|
|
name: profile.displayName || profile.username,
|
|
|
|
photo: User.parsePhotoByProfile(profile)
|
|
|
|
}
|
|
|
|
renderPublish(data, res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
renderPublish(data, res);
|
|
|
|
}
|
|
|
|
|
2015-07-01 12:10:20 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorNotFound(res);
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 09:01:42 -05:00
|
|
|
function renderPublish(data, res) {
|
|
|
|
var template = config.prettypath;
|
|
|
|
var options = {
|
2016-02-16 23:08:44 -05:00
|
|
|
url: config.getserverurl(),
|
2016-01-12 09:01:42 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-07-06 01:51:55 -04:00
|
|
|
function actionPublish(req, res, noteId) {
|
2015-07-01 12:10:20 -04:00
|
|
|
db.readFromDB(noteId, function (err, data) {
|
|
|
|
if (err) {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorNotFound(res);
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
|
|
|
var owner = data.rows[0].owner;
|
2016-01-17 10:51:27 -05:00
|
|
|
Note.findOrNewNote(noteId, owner, function (err, note) {
|
2015-07-01 12:10:20 -04:00
|
|
|
if (err) {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorNotFound(res);
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
2016-02-16 23:08:44 -05:00
|
|
|
res.redirect(config.getserverurl() + "/s/" + note.shortid);
|
2015-07-01 12:10:20 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-23 07:38:26 -05:00
|
|
|
function actionSlide(req, res, noteId) {
|
|
|
|
db.readFromDB(noteId, function (err, data) {
|
|
|
|
if (err) {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorNotFound(res);
|
2015-11-23 07:38:26 -05:00
|
|
|
}
|
|
|
|
var owner = data.rows[0].owner;
|
2016-01-17 10:51:27 -05:00
|
|
|
Note.findOrNewNote(noteId, owner, function (err, note) {
|
2015-11-23 07:38:26 -05:00
|
|
|
if (err) {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorNotFound(res);
|
2015-11-23 07:38:26 -05:00
|
|
|
}
|
2016-02-16 23:08:44 -05:00
|
|
|
res.redirect(config.getserverurl() + "/p/" + note.shortid);
|
2015-11-23 07:38:26 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
|
|
|
|
function actionDownload(req, res, noteId) {
|
|
|
|
db.readFromDB(noteId, function (err, data) {
|
|
|
|
if (err) {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorNotFound(res);
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
2016-03-04 10:12:03 -05:00
|
|
|
var notedata = data.rows[0];
|
|
|
|
var body = LZString.decompressFromBase64(notedata.content);
|
|
|
|
var title = Note.decodeTitle(notedata.title);
|
|
|
|
var filename = title;
|
|
|
|
filename = encodeURIComponent(filename);
|
2015-05-04 03:53:29 -04:00
|
|
|
res.writeHead(200, {
|
2016-02-01 01:41:28 -05:00
|
|
|
'Access-Control-Allow-Origin': '*', //allow CORS as API
|
2016-03-04 10:17:35 -05:00
|
|
|
'Access-Control-Allow-Headers': 'Range',
|
|
|
|
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
|
2015-05-04 03:53:29 -04:00
|
|
|
'Content-Type': 'text/markdown; charset=UTF-8',
|
|
|
|
'Cache-Control': 'private',
|
2016-03-04 10:12:03 -05:00
|
|
|
'Content-disposition': 'attachment; filename=' + filename + '.md',
|
2016-03-14 22:54:21 -04:00
|
|
|
'Content-Length': body.length,
|
|
|
|
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
2015-05-04 03:53:29 -04:00
|
|
|
});
|
|
|
|
res.end(body);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function actionPDF(req, res, noteId) {
|
|
|
|
db.readFromDB(noteId, function (err, data) {
|
|
|
|
if (err) {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorNotFound(res);
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
2016-03-04 10:12:03 -05:00
|
|
|
var notedata = data.rows[0];
|
|
|
|
var body = LZString.decompressFromBase64(notedata.content);
|
2016-01-12 09:01:42 -05:00
|
|
|
try {
|
|
|
|
body = metaMarked(body).markdown;
|
|
|
|
} catch(err) {
|
|
|
|
//na
|
|
|
|
}
|
2016-03-04 10:12:03 -05:00
|
|
|
var title = Note.decodeTitle(notedata.title);
|
2015-05-04 03:53:29 -04: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');
|
2016-03-14 22:54:21 -04:00
|
|
|
res.setHeader('X-Robots-Tag', 'noindex, nofollow'); // prevent crawling
|
2015-05-04 03:53:29 -04:00
|
|
|
stream.pipe(res);
|
|
|
|
fs.unlink(path);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-31 16:42:26 -05:00
|
|
|
function actionGist(req, res, noteId) {
|
|
|
|
db.readFromDB(noteId, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
|
|
|
var owner = data.rows[0].owner;
|
|
|
|
Note.findOrNewNote(noteId, owner, function (err, note) {
|
|
|
|
if (err) {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
|
|
|
var data = {
|
|
|
|
client_id: config.github.clientID,
|
|
|
|
redirect_uri: config.getserverurl() + '/auth/github/callback/' + LZString.compressToBase64(noteId) + '/gist',
|
|
|
|
scope: "gist",
|
|
|
|
state: shortId.generate()
|
|
|
|
};
|
|
|
|
var query = querystring.stringify(data);
|
|
|
|
res.redirect("https://github.com/login/oauth/authorize?" + query);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
function noteActions(req, res, next) {
|
|
|
|
var noteId = req.params.noteId;
|
|
|
|
if (noteId != config.featuresnotename) {
|
|
|
|
if (!Note.checkNoteIdValid(noteId)) {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorNotFound(res);
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
noteId = LZString.decompressFromBase64(noteId);
|
|
|
|
if (!noteId) {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorNotFound(res);
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
}
|
2016-01-17 10:51:27 -05:00
|
|
|
Note.findNote(noteId, function (err, note) {
|
|
|
|
if (err || !note) {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
|
|
|
db.readFromDB(note.id, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
|
|
|
var notedata = data.rows[0];
|
|
|
|
//check view permission
|
|
|
|
if (note.permission == 'private') {
|
|
|
|
if (!req.isAuthenticated() || notedata.owner != req.user._id)
|
|
|
|
return response.errorForbidden(res);
|
|
|
|
}
|
|
|
|
var action = req.params.action;
|
|
|
|
switch (action) {
|
|
|
|
case "publish":
|
|
|
|
case "pretty": //pretty deprecated
|
|
|
|
actionPublish(req, res, noteId);
|
|
|
|
break;
|
|
|
|
case "slide":
|
|
|
|
actionSlide(req, res, noteId);
|
|
|
|
break;
|
|
|
|
case "download":
|
|
|
|
actionDownload(req, res, noteId);
|
|
|
|
break;
|
|
|
|
case "pdf":
|
|
|
|
actionPDF(req, res, noteId);
|
|
|
|
break;
|
2016-01-31 16:42:26 -05:00
|
|
|
case "gist":
|
|
|
|
actionGist(req, res, noteId);
|
|
|
|
break;
|
2016-01-17 10:51:27 -05:00
|
|
|
default:
|
|
|
|
if (noteId != config.featuresnotename)
|
2016-02-16 23:08:44 -05:00
|
|
|
res.redirect(config.getserverurl() + '/' + LZString.compressToBase64(noteId));
|
2016-01-17 10:51:27 -05:00
|
|
|
else
|
2016-02-16 23:08:44 -05:00
|
|
|
res.redirect(config.getserverurl() + '/' + noteId);
|
2016-01-17 10:51:27 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2015-07-06 01:51:55 -04:00
|
|
|
function publishNoteActions(req, res, next) {
|
2016-01-17 10:51:27 -05:00
|
|
|
var shortid = req.params.shortid;
|
|
|
|
if (shortId.isValid(shortid)) {
|
|
|
|
Note.findNote(shortid, function (err, note) {
|
|
|
|
if (err || !note) {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
|
|
|
db.readFromDB(note.id, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
|
|
|
var notedata = data.rows[0];
|
|
|
|
//check view permission
|
|
|
|
if (note.permission == 'private') {
|
|
|
|
if (!req.isAuthenticated() || notedata.owner != req.user._id)
|
|
|
|
return response.errorForbidden(res);
|
|
|
|
}
|
|
|
|
var action = req.params.action;
|
|
|
|
switch (action) {
|
|
|
|
case "edit":
|
|
|
|
if (note.id != config.featuresnotename)
|
2016-02-16 23:08:44 -05:00
|
|
|
res.redirect(config.getserverurl() + '/' + LZString.compressToBase64(note.id));
|
2016-01-17 10:51:27 -05:00
|
|
|
else
|
2016-02-16 23:08:44 -05:00
|
|
|
res.redirect(config.getserverurl() + '/' + note.id);
|
2016-01-17 10:51:27 -05:00
|
|
|
break;
|
2016-01-12 09:01:42 -05:00
|
|
|
}
|
|
|
|
});
|
2016-01-17 10:51:27 -05:00
|
|
|
});
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
|
|
|
}
|
2016-01-31 16:42:26 -05:00
|
|
|
|
|
|
|
function githubActions(req, res, next) {
|
|
|
|
var noteId = req.params.noteId;
|
|
|
|
if (noteId != config.featuresnotename) {
|
|
|
|
if (!Note.checkNoteIdValid(noteId)) {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
|
|
|
noteId = LZString.decompressFromBase64(noteId);
|
|
|
|
if (!noteId) {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Note.findNote(noteId, function (err, note) {
|
|
|
|
if (err || !note) {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
|
|
|
db.readFromDB(note.id, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
|
|
|
var notedata = data.rows[0];
|
|
|
|
//check view permission
|
|
|
|
if (note.permission == 'private') {
|
|
|
|
if (!req.isAuthenticated() || notedata.owner != req.user._id)
|
|
|
|
return response.errorForbidden(res);
|
|
|
|
}
|
|
|
|
var action = req.params.action;
|
|
|
|
switch (action) {
|
|
|
|
case "gist":
|
|
|
|
githubActionGist(req, res, noteId);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (noteId != config.featuresnotename)
|
2016-02-16 23:08:44 -05:00
|
|
|
res.redirect(config.getserverurl() + '/' + LZString.compressToBase64(noteId));
|
2016-01-31 16:42:26 -05:00
|
|
|
else
|
2016-02-16 23:08:44 -05:00
|
|
|
res.redirect(config.getserverurl() + '/' + noteId);
|
2016-01-31 16:42:26 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function githubActionGist(req, res, noteId) {
|
|
|
|
db.readFromDB(noteId, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
|
|
|
var notedata = data.rows[0];
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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(notedata.content);
|
2016-03-04 10:12:03 -05:00
|
|
|
var title = Note.decodeTitle(notedata.title);
|
2016-01-31 16:42:26 -05:00
|
|
|
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) {
|
2016-02-16 22:54:29 -05:00
|
|
|
res.setHeader('referer', '');
|
2016-01-31 16:42:26 -05:00
|
|
|
res.redirect(body.html_url);
|
|
|
|
} else {
|
|
|
|
return response.errorForbidden(res);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return response.errorForbidden(res);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return response.errorForbidden(res);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-07-01 12:10:20 -04:00
|
|
|
|
2015-11-23 07:38:26 -05:00
|
|
|
function showPublishSlide(req, res, next) {
|
|
|
|
var shortid = req.params.shortid;
|
|
|
|
if (shortId.isValid(shortid)) {
|
|
|
|
Note.findNote(shortid, function (err, note) {
|
|
|
|
if (err || !note) {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorNotFound(res);
|
2015-11-23 07:38:26 -05:00
|
|
|
}
|
2016-01-17 10:51:27 -05:00
|
|
|
db.readFromDB(note.id, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
|
|
|
var notedata = data.rows[0];
|
|
|
|
//check view permission
|
|
|
|
if (note.permission == 'private') {
|
|
|
|
if (!req.isAuthenticated() || notedata.owner != req.user._id)
|
|
|
|
return response.errorForbidden(res);
|
2015-11-23 07:38:26 -05:00
|
|
|
}
|
2016-01-17 10:51:27 -05:00
|
|
|
//increase note viewcount
|
|
|
|
Note.increaseViewCount(note, function (err, note) {
|
|
|
|
if (err || !note) {
|
|
|
|
return response.errorNotFound(res);
|
2015-11-23 07:38:26 -05:00
|
|
|
}
|
2016-01-17 10:51:27 -05:00
|
|
|
var body = LZString.decompressFromBase64(notedata.content);
|
2016-01-12 09:01:42 -05:00
|
|
|
try {
|
|
|
|
body = metaMarked(body).markdown;
|
|
|
|
} catch(err) {
|
|
|
|
//na
|
|
|
|
}
|
2016-03-04 10:12:03 -05:00
|
|
|
var title = Note.decodeTitle(notedata.title);
|
2015-12-18 10:40:52 -05:00
|
|
|
title = Note.generateWebTitle(title);
|
2015-11-23 07:38:26 -05:00
|
|
|
var text = S(body).escapeHTML().s;
|
2015-12-18 10:40:52 -05:00
|
|
|
render(res, title, text);
|
2015-11-23 07:38:26 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
2016-01-17 10:51:27 -05:00
|
|
|
return response.errorNotFound(res);
|
2015-11-23 07:38:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-29 02:04:20 -05:00
|
|
|
//reveal.js render
|
2015-12-18 10:40:52 -05:00
|
|
|
var render = function (res, title, markdown) {
|
2015-11-29 02:04:20 -05:00
|
|
|
var slides = md.slidify(markdown, opts);
|
|
|
|
|
|
|
|
res.end(Mustache.to_html(opts.template, {
|
2016-02-16 23:08:44 -05:00
|
|
|
url: config.getserverurl(),
|
2015-12-18 10:40:52 -05:00
|
|
|
title: title,
|
2015-11-29 02:04:20 -05:00
|
|
|
theme: opts.theme,
|
|
|
|
highlightTheme: opts.highlightTheme,
|
|
|
|
slides: slides,
|
|
|
|
options: JSON.stringify(opts.revealOptions, null, 2)
|
|
|
|
}));
|
2015-11-23 07:38:26 -05:00
|
|
|
};
|
|
|
|
|
2016-03-04 10:12:03 -05:00
|
|
|
module.exports = response;
|