2015-05-04 07:53:29 +00:00
|
|
|
//response
|
|
|
|
//external modules
|
|
|
|
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');
|
2016-01-31 21:42:26 +00:00
|
|
|
var querystring = require('querystring');
|
|
|
|
var request = require('request');
|
2016-06-17 08:11:14 +00:00
|
|
|
var moment = require('moment');
|
2015-05-04 07:53:29 +00:00
|
|
|
|
|
|
|
//core
|
2016-04-20 10:03:55 +00:00
|
|
|
var config = require("./config.js");
|
|
|
|
var logger = require("./logger.js");
|
|
|
|
var models = require("./models");
|
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
|
|
|
},
|
2016-10-10 12:52:31 +00:00
|
|
|
errorBadRequest: function (res) {
|
|
|
|
responseError(res, "400", "Bad Request", "something not right.");
|
|
|
|
},
|
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,
|
2015-07-06 05:51:55 +00:00
|
|
|
showPublishNote: showPublishNote,
|
2015-11-23 12:38:26 +00:00
|
|
|
showPublishSlide: showPublishSlide,
|
2016-04-23 10:58:24 +00:00
|
|
|
showIndex: showIndex,
|
2015-07-01 16:10:20 +00:00
|
|
|
noteActions: noteActions,
|
2016-01-31 21:42:26 +00:00
|
|
|
publishNoteActions: publishNoteActions,
|
2016-08-15 03:25:27 +00:00
|
|
|
publishSlideActions: publishSlideActions,
|
2016-05-16 10:16:45 +00:00
|
|
|
githubActions: githubActions,
|
|
|
|
gitlabActions: gitlabActions
|
2015-05-04 07:53:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function responseError(res, code, detail, msg) {
|
2016-08-19 03:31:23 +00:00
|
|
|
res.status(code).render(config.errorpath, {
|
2016-04-20 10:03:55 +00:00
|
|
|
url: config.serverurl,
|
2015-07-01 16:10:20 +00:00
|
|
|
title: code + ' ' + detail + ' ' + msg,
|
2015-05-04 07:53:29 +00:00
|
|
|
code: code,
|
|
|
|
detail: detail,
|
2015-09-22 14:39:13 +00:00
|
|
|
msg: msg,
|
2016-08-19 03:31:23 +00:00
|
|
|
useCDN: config.usecdn
|
2015-05-04 07:53:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-22 04:06:13 +00:00
|
|
|
function showIndex(req, res, next) {
|
2016-08-19 03:31:23 +00:00
|
|
|
res.render(config.indexpath, {
|
2016-04-20 10:03:55 +00:00
|
|
|
url: config.serverurl,
|
|
|
|
useCDN: config.usecdn,
|
2016-12-21 13:36:54 +00:00
|
|
|
allowAnonymous: config.allowanonymous,
|
2016-04-20 10:03:55 +00:00
|
|
|
facebook: config.facebook,
|
|
|
|
twitter: config.twitter,
|
|
|
|
github: config.github,
|
2016-05-09 22:31:49 +00:00
|
|
|
gitlab: config.gitlab,
|
2016-05-21 14:48:00 +00:00
|
|
|
dropbox: config.dropbox,
|
2016-08-15 02:56:14 +00:00
|
|
|
google: config.google,
|
2016-12-13 21:31:35 +00:00
|
|
|
ldap: config.ldap,
|
2016-12-01 17:58:14 +00:00
|
|
|
email: config.email,
|
2017-01-12 03:25:58 +00:00
|
|
|
allowemailregister: config.allowemailregister,
|
2016-12-01 17:58:14 +00:00
|
|
|
signin: req.isAuthenticated(),
|
|
|
|
infoMessage: req.flash('info'),
|
|
|
|
errorMessage: req.flash('error')
|
2015-09-22 04:06:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:03:55 +00:00
|
|
|
function responseHackMD(res, note) {
|
2017-01-02 02:59:53 +00:00
|
|
|
var body = note.content;
|
2017-01-04 15:57:16 +00:00
|
|
|
var extracted = models.Note.extractMeta(body);
|
|
|
|
var meta = models.Note.parseMeta(extracted.meta);
|
2016-04-20 10:03:55 +00:00
|
|
|
var title = models.Note.decodeTitle(note.title);
|
2016-07-30 03:01:07 +00:00
|
|
|
title = models.Note.generateWebTitle(meta.title || title);
|
2016-08-19 03:31:23 +00:00
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private', // only cache by client
|
|
|
|
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
|
|
|
});
|
|
|
|
res.render(config.hackmdpath, {
|
2016-04-20 10:03:55 +00:00
|
|
|
url: config.serverurl,
|
2016-07-30 03:01:07 +00:00
|
|
|
title: title,
|
2016-04-20 10:03:55 +00:00
|
|
|
useCDN: config.usecdn,
|
2016-12-21 13:36:54 +00:00
|
|
|
allowAnonymous: config.allowanonymous,
|
2016-04-20 10:03:55 +00:00
|
|
|
facebook: config.facebook,
|
|
|
|
twitter: config.twitter,
|
|
|
|
github: config.github,
|
2016-05-09 22:31:49 +00:00
|
|
|
gitlab: config.gitlab,
|
2016-05-21 14:48:00 +00:00
|
|
|
dropbox: config.dropbox,
|
2016-12-01 17:58:14 +00:00
|
|
|
google: config.google,
|
2016-12-13 21:31:35 +00:00
|
|
|
ldap: config.ldap,
|
2017-01-16 04:41:34 +00:00
|
|
|
email: config.email,
|
|
|
|
allowemailregister: config.allowemailregister
|
2015-05-04 07:53:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function newNote(req, res, next) {
|
|
|
|
var owner = null;
|
|
|
|
if (req.isAuthenticated()) {
|
2016-04-20 10:03:55 +00:00
|
|
|
owner = req.user.id;
|
2016-12-21 13:36:54 +00:00
|
|
|
} else if (!config.allowanonymous) {
|
2016-12-15 06:07:12 +00:00
|
|
|
return response.errorForbidden(res);
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
2016-04-20 10:03:55 +00:00
|
|
|
models.Note.create({
|
2016-12-16 07:38:05 +00:00
|
|
|
ownerId: owner,
|
|
|
|
alias: req.alias ? req.alias : null
|
2016-04-20 10:03:55 +00:00
|
|
|
}).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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:03:55 +00:00
|
|
|
function checkViewPermission(req, note) {
|
|
|
|
if (note.permission == 'private') {
|
|
|
|
if (!req.isAuthenticated() || note.ownerId != req.user.id)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
2017-01-10 02:02:37 +00:00
|
|
|
} else if (note.permission == 'limited' || note.permission == 'protected') {
|
2017-01-16 15:47:53 +00:00
|
|
|
if(!req.isAuthenticated())
|
2017-01-10 02:02:37 +00:00
|
|
|
return false;
|
2017-01-16 15:47:53 +00:00
|
|
|
else
|
|
|
|
return true;
|
2016-04-20 10:03:55 +00:00
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:03:55 +00:00
|
|
|
function findNote(req, res, callback, include) {
|
2016-12-16 07:38:05 +00:00
|
|
|
var noteId = req.params.noteId;
|
2016-04-20 10:03:55 +00:00
|
|
|
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) {
|
2016-12-16 07:38:05 +00:00
|
|
|
if (config.allowfreeurl && noteId) {
|
|
|
|
req.alias = noteId;
|
|
|
|
return newNote(req, res);
|
|
|
|
} else {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
2016-01-17 15:51:27 +00:00
|
|
|
}
|
2016-04-20 10:03:55 +00:00
|
|
|
if (!checkViewPermission(req, note)) {
|
|
|
|
return response.errorForbidden(res);
|
|
|
|
} else {
|
|
|
|
return callback(note);
|
2016-01-17 15:51:27 +00:00
|
|
|
}
|
2016-04-20 10:03:55 +00:00
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err);
|
|
|
|
return response.errorInternalError(res);
|
2016-01-17 15:51:27 +00:00
|
|
|
});
|
|
|
|
});
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:03:55 +00:00
|
|
|
function showNote(req, res, next) {
|
|
|
|
findNote(req, res, function (note) {
|
2016-07-02 08:11:30 +00:00
|
|
|
// force to use note id
|
|
|
|
var noteId = req.params.noteId;
|
2017-01-10 02:02:37 +00:00
|
|
|
var id = LZString.compressToBase64(note.id);
|
2016-07-02 08:11:30 +00:00
|
|
|
if ((note.alias && noteId != note.alias) || (!note.alias && noteId != id))
|
|
|
|
return res.redirect(config.serverurl + "/" + (note.alias || id));
|
2016-04-20 10:03:55 +00:00
|
|
|
return responseHackMD(res, note);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-06 05:51:55 +00:00
|
|
|
function showPublishNote(req, res, next) {
|
2016-04-20 10:03:55 +00:00
|
|
|
var include = [{
|
|
|
|
model: models.User,
|
|
|
|
as: "owner"
|
|
|
|
}, {
|
|
|
|
model: models.User,
|
|
|
|
as: "lastchangeuser"
|
|
|
|
}];
|
|
|
|
findNote(req, res, function (note) {
|
2016-07-02 08:11:30 +00:00
|
|
|
// force to use short id
|
|
|
|
var shortid = req.params.shortid;
|
|
|
|
if ((note.alias && shortid != note.alias) || (!note.alias && shortid != note.shortid))
|
|
|
|
return res.redirect(config.serverurl + "/s/" + (note.alias || note.shortid));
|
2016-04-20 10:03:55 +00:00
|
|
|
note.increment('viewcount').then(function (note) {
|
|
|
|
if (!note) {
|
2016-01-17 15:51:27 +00:00
|
|
|
return response.errorNotFound(res);
|
2015-07-01 16:10:20 +00:00
|
|
|
}
|
2017-01-02 02:59:53 +00:00
|
|
|
var body = note.content;
|
2017-01-04 15:57:16 +00:00
|
|
|
var extracted = models.Note.extractMeta(body);
|
|
|
|
markdown = extracted.markdown;
|
|
|
|
meta = models.Note.parseMeta(extracted.meta);
|
2016-04-20 10:03:55 +00:00
|
|
|
var createtime = note.createdAt;
|
|
|
|
var updatetime = note.lastchangeAt;
|
|
|
|
var title = models.Note.decodeTitle(note.title);
|
2016-07-30 03:01:07 +00:00
|
|
|
title = models.Note.generateWebTitle(meta.title || title);
|
2016-04-20 10:03:55 +00:00
|
|
|
var origin = config.serverurl;
|
|
|
|
var data = {
|
2016-07-30 03:01:07 +00:00
|
|
|
title: title,
|
2017-01-02 03:13:41 +00:00
|
|
|
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
|
2016-04-20 10:03:55 +00:00
|
|
|
viewcount: note.viewcount,
|
|
|
|
createtime: createtime,
|
|
|
|
updatetime: updatetime,
|
|
|
|
url: origin,
|
2016-11-26 14:55:31 +00:00
|
|
|
body: body,
|
2016-04-20 10:03:55 +00:00
|
|
|
useCDN: config.usecdn,
|
2016-10-10 12:32:20 +00:00
|
|
|
owner: note.owner ? note.owner.id : null,
|
2016-12-01 17:58:14 +00:00
|
|
|
ownerprofile: note.owner ? models.User.getProfile(note.owner) : null,
|
2016-10-10 12:32:20 +00:00
|
|
|
lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null,
|
2016-12-01 17:58:14 +00:00
|
|
|
lastchangeuserprofile: note.lastchangeuser ? models.User.getProfile(note.lastchangeuser) : null,
|
2016-06-21 13:42:03 +00:00
|
|
|
robots: meta.robots || false, //default allow robots
|
2016-08-15 03:25:27 +00:00
|
|
|
GA: meta.GA,
|
|
|
|
disqus: meta.disqus
|
2016-04-20 10:03:55 +00:00
|
|
|
};
|
|
|
|
return renderPublish(data, res);
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err);
|
|
|
|
return response.errorInternalError(res);
|
2015-07-01 16:10:20 +00:00
|
|
|
});
|
2016-04-20 10:03:55 +00:00
|
|
|
}, include);
|
2015-07-01 16:10:20 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 14:01:42 +00:00
|
|
|
function renderPublish(data, res) {
|
2016-08-19 03:31:23 +00:00
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private' // only cache by client
|
2016-01-12 14:01:42 +00:00
|
|
|
});
|
2016-08-19 03:31:23 +00:00
|
|
|
res.render(config.prettypath, data);
|
2016-01-12 14:01:42 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:03:55 +00:00
|
|
|
function actionPublish(req, res, note) {
|
|
|
|
res.redirect(config.serverurl + "/s/" + (note.alias || note.shortid));
|
2015-07-01 16:10:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:03:55 +00:00
|
|
|
function actionSlide(req, res, note) {
|
|
|
|
res.redirect(config.serverurl + "/p/" + (note.alias || note.shortid));
|
2015-11-23 12:38:26 +00:00
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2016-04-20 10:03:55 +00:00
|
|
|
function actionDownload(req, res, note) {
|
2017-01-02 02:59:53 +00:00
|
|
|
var body = note.content;
|
2016-04-20 10:03:55 +00:00
|
|
|
var title = models.Note.decodeTitle(note.title);
|
|
|
|
var filename = title;
|
|
|
|
filename = encodeURIComponent(filename);
|
2016-08-19 03:31:23 +00:00
|
|
|
res.set({
|
2016-04-20 10:03:55 +00:00
|
|
|
'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',
|
|
|
|
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
2015-05-04 07:53:29 +00:00
|
|
|
});
|
2016-08-19 03:31:23 +00:00
|
|
|
res.send(body);
|
|
|
|
}
|
2016-08-19 03:24:36 +00:00
|
|
|
|
|
|
|
function actionInfo(req, res, note) {
|
2017-01-02 02:59:53 +00:00
|
|
|
var body = note.content;
|
2017-01-04 15:57:16 +00:00
|
|
|
var extracted = models.Note.extractMeta(body);
|
|
|
|
var markdown = extracted.markdown;
|
|
|
|
var meta = models.Note.parseMeta(extracted.meta);
|
2016-08-19 03:24:36 +00:00
|
|
|
var createtime = note.createdAt;
|
|
|
|
var updatetime = note.lastchangeAt;
|
|
|
|
var title = models.Note.decodeTitle(note.title);
|
|
|
|
var data = {
|
|
|
|
title: meta.title || title,
|
2017-01-02 03:13:41 +00:00
|
|
|
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
|
2016-08-19 03:24:36 +00:00
|
|
|
viewcount: note.viewcount,
|
|
|
|
createtime: createtime,
|
|
|
|
updatetime: updatetime
|
|
|
|
};
|
|
|
|
res.set({
|
|
|
|
'Access-Control-Allow-Origin': '*', //allow CORS as API
|
|
|
|
'Access-Control-Allow-Headers': 'Range',
|
|
|
|
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
|
2016-09-18 08:23:56 +00:00
|
|
|
'Cache-Control': 'private', // only cache by client
|
2016-08-19 03:24:36 +00:00
|
|
|
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
|
|
|
});
|
|
|
|
res.send(data);
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 10:03:55 +00:00
|
|
|
function actionPDF(req, res, note) {
|
2017-01-02 02:59:53 +00:00
|
|
|
var body = note.content;
|
2017-01-04 15:57:16 +00:00
|
|
|
var extracted = models.Note.extractMeta(body);
|
2016-04-20 10:03:55 +00:00
|
|
|
var title = models.Note.decodeTitle(note.title);
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2016-04-20 10:03:55 +00:00
|
|
|
if (!fs.existsSync(config.tmppath)) {
|
|
|
|
fs.mkdirSync(config.tmppath);
|
|
|
|
}
|
2016-06-17 08:33:58 +00:00
|
|
|
var path = config.tmppath + '/' + Date.now() + '.pdf';
|
2017-01-04 15:57:16 +00:00
|
|
|
markdownpdf().from.string(extracted.markdown).to(path, function () {
|
2016-04-20 10:03:55 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:03:55 +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
|
|
|
}
|
|
|
|
|
2016-06-17 08:11:14 +00:00
|
|
|
function actionRevision(req, res, note) {
|
|
|
|
var actionId = req.params.actionId;
|
|
|
|
if (actionId) {
|
|
|
|
var time = moment(parseInt(actionId));
|
|
|
|
if (time.isValid()) {
|
|
|
|
models.Revision.getPatchedNoteRevisionByTime(note, time, function (err, content) {
|
|
|
|
if (err) {
|
|
|
|
logger.error(err);
|
|
|
|
return response.errorInternalError(res);
|
|
|
|
}
|
|
|
|
if (!content) {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
2016-10-10 12:33:48 +00:00
|
|
|
res.set({
|
|
|
|
'Access-Control-Allow-Origin': '*', //allow CORS as API
|
|
|
|
'Access-Control-Allow-Headers': 'Range',
|
|
|
|
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
|
|
|
|
'Cache-Control': 'private', // only cache by client
|
|
|
|
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
|
|
|
});
|
2016-08-19 03:31:23 +00:00
|
|
|
res.send(content);
|
2016-06-17 08:11:14 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
models.Revision.getNoteRevisions(note, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
logger.error(err);
|
|
|
|
return response.errorInternalError(res);
|
|
|
|
}
|
|
|
|
var out = {
|
|
|
|
revision: data
|
|
|
|
};
|
2016-10-10 12:33:48 +00:00
|
|
|
res.set({
|
|
|
|
'Access-Control-Allow-Origin': '*', //allow CORS as API
|
|
|
|
'Access-Control-Allow-Headers': 'Range',
|
|
|
|
'Access-Control-Expose-Headers': 'Cache-Control, Content-Encoding, Content-Range',
|
|
|
|
'Cache-Control': 'private', // only cache by client
|
|
|
|
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
|
|
|
});
|
2016-08-19 03:31:23 +00:00
|
|
|
res.send(out);
|
2016-06-17 08:11:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 07:53:29 +00:00
|
|
|
function noteActions(req, res, next) {
|
|
|
|
var noteId = req.params.noteId;
|
2016-04-20 10:03:55 +00:00
|
|
|
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;
|
2016-08-19 03:24:36 +00:00
|
|
|
case "info":
|
|
|
|
actionInfo(req, res, note);
|
|
|
|
break;
|
2016-04-20 10:03:55 +00:00
|
|
|
case "pdf":
|
|
|
|
actionPDF(req, res, note);
|
|
|
|
break;
|
|
|
|
case "gist":
|
|
|
|
actionGist(req, res, note);
|
|
|
|
break;
|
2016-06-17 08:11:14 +00:00
|
|
|
case "revision":
|
|
|
|
actionRevision(req, res, note);
|
|
|
|
break;
|
2016-04-20 10:03:55 +00:00
|
|
|
default:
|
|
|
|
return res.redirect(config.serverurl + '/' + noteId);
|
|
|
|
break;
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
2016-01-17 15:51:27 +00:00
|
|
|
});
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 05:51:55 +00:00
|
|
|
function publishNoteActions(req, res, next) {
|
2016-04-20 10:03:55 +00:00
|
|
|
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:
|
2016-04-23 10:58:24 +00:00
|
|
|
res.redirect(config.serverurl + '/s/' + note.shortid);
|
2016-04-20 10:03:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2015-07-01 16:10:20 +00:00
|
|
|
}
|
2016-01-31 21:42:26 +00:00
|
|
|
|
2016-08-15 03:25:27 +00:00
|
|
|
function publishSlideActions(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:
|
2017-01-10 02:02:37 +00:00
|
|
|
res.redirect(config.serverurl + '/p/' + note.shortid);
|
2016-08-15 03:25:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-31 21:42:26 +00:00
|
|
|
function githubActions(req, res, next) {
|
|
|
|
var noteId = req.params.noteId;
|
2016-04-20 10:03:55 +00:00
|
|
|
findNote(req, res, function (note) {
|
|
|
|
var action = req.params.action;
|
|
|
|
switch (action) {
|
|
|
|
case "gist":
|
|
|
|
githubActionGist(req, res, note);
|
|
|
|
break;
|
|
|
|
default:
|
2016-04-23 10:58:24 +00:00
|
|
|
res.redirect(config.serverurl + '/' + noteId);
|
2016-04-20 10:03:55 +00:00
|
|
|
break;
|
2016-01-31 21:42:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-20 10:03:55 +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
|
|
|
}
|
2016-04-20 10:03:55 +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) {
|
2017-01-02 02:59:53 +00:00
|
|
|
var content = note.content;
|
2016-04-20 10:03:55 +00:00
|
|
|
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);
|
|
|
|
}
|
2016-04-20 10:03:55 +00:00
|
|
|
} else {
|
|
|
|
return response.errorForbidden(res);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-01-31 21:42:26 +00:00
|
|
|
}
|
2015-07-01 16:10:20 +00:00
|
|
|
|
2016-05-16 10:16:45 +00:00
|
|
|
function gitlabActions(req, res, next) {
|
|
|
|
var noteId = req.params.noteId;
|
|
|
|
findNote(req, res, function (note) {
|
|
|
|
var action = req.params.action;
|
|
|
|
switch (action) {
|
|
|
|
case "projects":
|
|
|
|
gitlabActionProjects(req, res, note);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res.redirect(config.serverurl + '/' + noteId);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function gitlabActionProjects(req, res, note) {
|
|
|
|
if (req.isAuthenticated()) {
|
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: req.user.id
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
|
|
|
if (!user)
|
|
|
|
return response.errorNotFound(res);
|
|
|
|
var ret = { baseURL: config.gitlab.baseURL };
|
|
|
|
ret.accesstoken = user.accessToken;
|
|
|
|
ret.profileid = user.profileid;
|
|
|
|
request(
|
|
|
|
config.gitlab.baseURL + '/api/v3/projects?access_token=' + user.accessToken,
|
|
|
|
function(error, httpResponse, body) {
|
|
|
|
if (!error && httpResponse.statusCode == 200) {
|
|
|
|
ret.projects = JSON.parse(body);
|
|
|
|
return res.send(ret);
|
|
|
|
} else {
|
|
|
|
return res.send(ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('gitlab action projects failed: ' + err);
|
|
|
|
return response.errorInternalError(res);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return response.errorForbidden(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-23 12:38:26 +00:00
|
|
|
function showPublishSlide(req, res, next) {
|
2016-08-15 03:25:27 +00:00
|
|
|
var include = [{
|
|
|
|
model: models.User,
|
|
|
|
as: "owner"
|
|
|
|
}, {
|
|
|
|
model: models.User,
|
|
|
|
as: "lastchangeuser"
|
|
|
|
}];
|
2016-04-20 10:03:55 +00:00
|
|
|
findNote(req, res, function (note) {
|
2016-07-02 08:11:30 +00:00
|
|
|
// force to use short id
|
|
|
|
var shortid = req.params.shortid;
|
|
|
|
if ((note.alias && shortid != note.alias) || (!note.alias && shortid != note.shortid))
|
|
|
|
return res.redirect(config.serverurl + "/p/" + (note.alias || note.shortid));
|
2016-04-20 10:03:55 +00:00
|
|
|
note.increment('viewcount').then(function (note) {
|
|
|
|
if (!note) {
|
2016-01-17 15:51:27 +00:00
|
|
|
return response.errorNotFound(res);
|
2015-11-23 12:38:26 +00:00
|
|
|
}
|
2017-01-02 02:59:53 +00:00
|
|
|
var body = note.content;
|
2017-01-04 15:57:16 +00:00
|
|
|
var extracted = models.Note.extractMeta(body);
|
|
|
|
markdown = extracted.markdown;
|
|
|
|
meta = models.Note.parseMeta(extracted.meta);
|
2016-08-15 03:25:27 +00:00
|
|
|
var createtime = note.createdAt;
|
|
|
|
var updatetime = note.lastchangeAt;
|
2016-04-20 10:03:55 +00:00
|
|
|
var title = models.Note.decodeTitle(note.title);
|
2016-07-30 03:01:07 +00:00
|
|
|
title = models.Note.generateWebTitle(meta.title || title);
|
2016-06-21 13:42:03 +00:00
|
|
|
var origin = config.serverurl;
|
|
|
|
var data = {
|
2016-07-30 03:01:07 +00:00
|
|
|
title: title,
|
2017-01-02 03:13:41 +00:00
|
|
|
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
|
2016-08-15 03:25:27 +00:00
|
|
|
viewcount: note.viewcount,
|
|
|
|
createtime: createtime,
|
|
|
|
updatetime: updatetime,
|
|
|
|
url: origin,
|
2016-11-26 15:04:29 +00:00
|
|
|
body: markdown,
|
2017-01-04 15:57:16 +00:00
|
|
|
meta: JSON.stringify(extracted.meta),
|
2016-08-15 03:25:27 +00:00
|
|
|
useCDN: config.usecdn,
|
2016-10-10 12:32:20 +00:00
|
|
|
owner: note.owner ? note.owner.id : null,
|
2016-12-01 17:58:14 +00:00
|
|
|
ownerprofile: note.owner ? models.User.getProfile(note.owner) : null,
|
2016-10-10 12:32:20 +00:00
|
|
|
lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null,
|
2016-12-01 17:58:14 +00:00
|
|
|
lastchangeuserprofile: note.lastchangeuser ? models.User.getProfile(note.lastchangeuser) : null,
|
2016-08-15 03:25:27 +00:00
|
|
|
robots: meta.robots || false, //default allow robots
|
2016-07-02 08:09:26 +00:00
|
|
|
GA: meta.GA,
|
2016-08-15 03:25:27 +00:00
|
|
|
disqus: meta.disqus
|
2016-06-21 13:42:03 +00:00
|
|
|
};
|
|
|
|
return renderPublishSlide(data, res);
|
2016-04-20 10:03:55 +00:00
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err);
|
|
|
|
return response.errorInternalError(res);
|
2015-11-23 12:38:26 +00:00
|
|
|
});
|
2016-08-15 03:25:27 +00:00
|
|
|
}, include);
|
2015-11-23 12:38:26 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 13:42:03 +00:00
|
|
|
function renderPublishSlide(data, res) {
|
2016-08-19 03:31:23 +00:00
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private' // only cache by client
|
2016-05-29 09:54:24 +00:00
|
|
|
});
|
2016-08-19 03:31:23 +00:00
|
|
|
res.render(config.slidepath, data);
|
2016-06-21 13:42:03 +00:00
|
|
|
}
|
2015-11-23 12:38:26 +00:00
|
|
|
|
2016-04-23 10:58:24 +00:00
|
|
|
module.exports = response;
|