2017-03-14 05:02:43 +00:00
|
|
|
'use strict'
|
2017-03-08 10:45:51 +00:00
|
|
|
// response
|
|
|
|
// external modules
|
|
|
|
var fs = require('fs')
|
|
|
|
var markdownpdf = require('markdown-pdf')
|
|
|
|
var shortId = require('shortid')
|
|
|
|
var querystring = require('querystring')
|
|
|
|
var request = require('request')
|
|
|
|
var moment = require('moment')
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
// core
|
2017-04-12 16:20:28 +00:00
|
|
|
var config = require('./config')
|
|
|
|
var logger = require('./logger')
|
2017-03-08 10:45:51 +00:00
|
|
|
var models = require('./models')
|
2017-06-01 08:47:52 +00:00
|
|
|
var utils = require('./utils')
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
// public
|
2015-05-04 07:53:29 +00:00
|
|
|
var response = {
|
2017-03-08 10:45:51 +00:00
|
|
|
errorForbidden: function (res) {
|
|
|
|
responseError(res, '403', 'Forbidden', 'oh no.')
|
|
|
|
},
|
|
|
|
errorNotFound: function (res) {
|
|
|
|
responseError(res, '404', 'Not Found', 'oops.')
|
|
|
|
},
|
|
|
|
errorBadRequest: function (res) {
|
|
|
|
responseError(res, '400', 'Bad Request', 'something not right.')
|
|
|
|
},
|
|
|
|
errorInternalError: function (res) {
|
|
|
|
responseError(res, '500', 'Internal Error', 'wtf.')
|
|
|
|
},
|
|
|
|
errorServiceUnavailable: function (res) {
|
|
|
|
res.status(503).send("I'm busy right now, try again later.")
|
|
|
|
},
|
|
|
|
newNote: newNote,
|
|
|
|
showNote: showNote,
|
|
|
|
showPublishNote: showPublishNote,
|
|
|
|
showPublishSlide: showPublishSlide,
|
|
|
|
showIndex: showIndex,
|
|
|
|
noteActions: noteActions,
|
|
|
|
publishNoteActions: publishNoteActions,
|
|
|
|
publishSlideActions: publishSlideActions,
|
|
|
|
githubActions: githubActions,
|
|
|
|
gitlabActions: gitlabActions
|
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function responseError (res, code, detail, msg) {
|
2018-03-07 14:17:35 +00:00
|
|
|
res.status(code).render(config.errorPath, {
|
|
|
|
url: config.serverURL,
|
2017-03-08 10:45:51 +00:00
|
|
|
title: code + ' ' + detail + ' ' + msg,
|
|
|
|
code: code,
|
|
|
|
detail: detail,
|
|
|
|
msg: msg,
|
2018-03-07 14:17:35 +00:00
|
|
|
useCDN: config.useCDN
|
2017-03-08 10:45:51 +00:00
|
|
|
})
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function showIndex (req, res, next) {
|
2018-03-07 14:17:35 +00:00
|
|
|
res.render(config.indexPath, {
|
|
|
|
url: config.serverURL,
|
|
|
|
useCDN: config.useCDN,
|
|
|
|
allowAnonymous: config.allowAnonymous,
|
2018-04-10 12:38:39 +00:00
|
|
|
allowAnonymousEdits: config.allowAnonymousEdits,
|
2017-04-12 17:57:55 +00:00
|
|
|
facebook: config.isFacebookEnable,
|
|
|
|
twitter: config.isTwitterEnable,
|
|
|
|
github: config.isGitHubEnable,
|
|
|
|
gitlab: config.isGitLabEnable,
|
2017-10-29 10:16:40 +00:00
|
|
|
mattermost: config.isMattermostEnable,
|
2017-04-12 17:57:55 +00:00
|
|
|
dropbox: config.isDropboxEnable,
|
|
|
|
google: config.isGoogleEnable,
|
|
|
|
ldap: config.isLDAPEnable,
|
2018-01-26 09:42:06 +00:00
|
|
|
ldapProviderName: config.ldap.providerName,
|
2017-11-28 03:46:58 +00:00
|
|
|
saml: config.isSAMLEnable,
|
2017-04-12 17:57:55 +00:00
|
|
|
email: config.isEmailEnable,
|
2018-03-07 14:17:35 +00:00
|
|
|
allowEmailRegister: config.allowEmailRegister,
|
|
|
|
allowPDFExport: config.allowPDFExport,
|
2017-03-08 10:45:51 +00:00
|
|
|
signin: req.isAuthenticated(),
|
|
|
|
infoMessage: req.flash('info'),
|
|
|
|
errorMessage: req.flash('error')
|
|
|
|
})
|
2015-09-22 04:06:13 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function responseHackMD (res, note) {
|
|
|
|
var body = note.content
|
|
|
|
var extracted = models.Note.extractMeta(body)
|
|
|
|
var meta = models.Note.parseMeta(extracted.meta)
|
|
|
|
var title = models.Note.decodeTitle(note.title)
|
|
|
|
title = models.Note.generateWebTitle(meta.title || title)
|
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private', // only cache by client
|
|
|
|
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
|
|
|
})
|
2018-03-07 14:17:35 +00:00
|
|
|
res.render(config.hackmdPath, {
|
|
|
|
url: config.serverURL,
|
2017-03-08 10:45:51 +00:00
|
|
|
title: title,
|
2018-03-07 14:17:35 +00:00
|
|
|
useCDN: config.useCDN,
|
|
|
|
allowAnonymous: config.allowAnonymous,
|
2018-04-10 12:38:39 +00:00
|
|
|
allowAnonymousEdits: config.allowAnonymousEdits,
|
2017-04-12 17:57:55 +00:00
|
|
|
facebook: config.isFacebookEnable,
|
|
|
|
twitter: config.isTwitterEnable,
|
|
|
|
github: config.isGitHubEnable,
|
|
|
|
gitlab: config.isGitLabEnable,
|
2017-10-31 12:48:35 +00:00
|
|
|
mattermost: config.isMattermostEnable,
|
2017-04-12 17:57:55 +00:00
|
|
|
dropbox: config.isDropboxEnable,
|
|
|
|
google: config.isGoogleEnable,
|
|
|
|
ldap: config.isLDAPEnable,
|
2018-01-26 09:42:06 +00:00
|
|
|
ldapProviderName: config.ldap.providerName,
|
2017-11-28 03:46:58 +00:00
|
|
|
saml: config.isSAMLEnable,
|
2017-04-12 17:57:55 +00:00
|
|
|
email: config.isEmailEnable,
|
2018-03-07 14:17:35 +00:00
|
|
|
allowEmailRegister: config.allowEmailRegister,
|
|
|
|
allowPDFExport: config.allowPDFExport
|
2017-03-08 10:45:51 +00:00
|
|
|
})
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function newNote (req, res, next) {
|
|
|
|
var owner = null
|
|
|
|
if (req.isAuthenticated()) {
|
|
|
|
owner = req.user.id
|
2018-03-07 14:17:35 +00:00
|
|
|
} else if (!config.allowAnonymous) {
|
2017-03-08 10:45:51 +00:00
|
|
|
return response.errorForbidden(res)
|
|
|
|
}
|
|
|
|
models.Note.create({
|
|
|
|
ownerId: owner,
|
2018-01-10 23:51:22 +00:00
|
|
|
alias: req.alias ? req.alias : null,
|
|
|
|
content: req.body ? req.body : ''
|
2017-03-08 10:45:51 +00:00
|
|
|
}).then(function (note) {
|
2018-03-07 14:17:35 +00:00
|
|
|
return res.redirect(config.serverURL + '/' + models.Note.encodeNoteId(note.id))
|
2017-03-08 10:45:51 +00:00
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
})
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function checkViewPermission (req, note) {
|
|
|
|
if (note.permission === 'private') {
|
|
|
|
if (!req.isAuthenticated() || note.ownerId !== req.user.id) { return false } else { return true }
|
|
|
|
} else if (note.permission === 'limited' || note.permission === 'protected') {
|
|
|
|
if (!req.isAuthenticated()) { return false } else { return true }
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function findNote (req, res, callback, include) {
|
|
|
|
var noteId = req.params.noteId
|
|
|
|
var id = req.params.noteId || req.params.shortid
|
|
|
|
models.Note.parseNoteId(id, function (err, _id) {
|
|
|
|
if (err) {
|
2018-02-16 16:47:50 +00:00
|
|
|
logger.error(err)
|
|
|
|
return response.errorInternalError(res)
|
2017-03-08 10:45:51 +00:00
|
|
|
}
|
|
|
|
models.Note.findOne({
|
|
|
|
where: {
|
|
|
|
id: _id
|
|
|
|
},
|
|
|
|
include: include || null
|
|
|
|
}).then(function (note) {
|
|
|
|
if (!note) {
|
2018-03-07 14:17:35 +00:00
|
|
|
if (config.allowFreeURL && noteId) {
|
2017-03-08 10:45:51 +00:00
|
|
|
req.alias = noteId
|
|
|
|
return newNote(req, res)
|
|
|
|
} else {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function showNote (req, res, next) {
|
|
|
|
findNote(req, res, function (note) {
|
|
|
|
// force to use note id
|
|
|
|
var noteId = req.params.noteId
|
2018-02-26 08:43:29 +00:00
|
|
|
var id = models.Note.encodeNoteId(note.id)
|
2018-03-07 14:17:35 +00:00
|
|
|
if ((note.alias && noteId !== note.alias) || (!note.alias && noteId !== id)) { return res.redirect(config.serverURL + '/' + (note.alias || id)) }
|
2017-03-08 10:45:51 +00:00
|
|
|
return responseHackMD(res, note)
|
|
|
|
})
|
2016-04-20 10:03:55 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function showPublishNote (req, res, next) {
|
|
|
|
var include = [{
|
|
|
|
model: models.User,
|
|
|
|
as: 'owner'
|
|
|
|
}, {
|
|
|
|
model: models.User,
|
|
|
|
as: 'lastchangeuser'
|
|
|
|
}]
|
|
|
|
findNote(req, res, function (note) {
|
|
|
|
// force to use short id
|
|
|
|
var shortid = req.params.shortid
|
|
|
|
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
|
2018-03-07 14:17:35 +00:00
|
|
|
return res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
2017-03-08 10:45:51 +00:00
|
|
|
}
|
|
|
|
note.increment('viewcount').then(function (note) {
|
|
|
|
if (!note) {
|
|
|
|
return response.errorNotFound(res)
|
|
|
|
}
|
|
|
|
var body = note.content
|
|
|
|
var extracted = models.Note.extractMeta(body)
|
|
|
|
var markdown = extracted.markdown
|
|
|
|
var meta = models.Note.parseMeta(extracted.meta)
|
|
|
|
var createtime = note.createdAt
|
|
|
|
var updatetime = note.lastchangeAt
|
|
|
|
var title = models.Note.decodeTitle(note.title)
|
|
|
|
title = models.Note.generateWebTitle(meta.title || title)
|
2018-03-07 14:17:35 +00:00
|
|
|
var origin = config.serverURL
|
2017-03-08 10:45:51 +00:00
|
|
|
var data = {
|
|
|
|
title: title,
|
|
|
|
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
|
|
|
|
viewcount: note.viewcount,
|
|
|
|
createtime: createtime,
|
|
|
|
updatetime: updatetime,
|
|
|
|
url: origin,
|
|
|
|
body: body,
|
2018-03-07 14:17:35 +00:00
|
|
|
useCDN: config.useCDN,
|
2017-03-08 10:45:51 +00:00
|
|
|
owner: note.owner ? note.owner.id : null,
|
|
|
|
ownerprofile: note.owner ? models.User.getProfile(note.owner) : null,
|
|
|
|
lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null,
|
|
|
|
lastchangeuserprofile: note.lastchangeuser ? models.User.getProfile(note.lastchangeuser) : null,
|
|
|
|
robots: meta.robots || false, // default allow robots
|
|
|
|
GA: meta.GA,
|
2018-03-30 14:33:32 +00:00
|
|
|
disqus: meta.disqus,
|
|
|
|
cspNonce: res.locals.nonce
|
2017-03-08 10:45:51 +00:00
|
|
|
}
|
|
|
|
return renderPublish(data, res)
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
})
|
|
|
|
}, include)
|
2015-07-01 16:10:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function renderPublish (data, res) {
|
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private' // only cache by client
|
|
|
|
})
|
2018-03-07 14:17:35 +00:00
|
|
|
res.render(config.prettyPath, data)
|
2016-01-12 14:01:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function actionPublish (req, res, note) {
|
2018-03-07 14:17:35 +00:00
|
|
|
res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
2015-07-01 16:10:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function actionSlide (req, res, note) {
|
2018-03-07 14:17:35 +00:00
|
|
|
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
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function actionDownload (req, res, note) {
|
|
|
|
var body = note.content
|
|
|
|
var title = models.Note.decodeTitle(note.title)
|
|
|
|
var filename = title
|
|
|
|
filename = encodeURIComponent(filename)
|
|
|
|
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',
|
|
|
|
'Content-Type': 'text/markdown; charset=UTF-8',
|
|
|
|
'Cache-Control': 'private',
|
|
|
|
'Content-disposition': 'attachment; filename=' + filename + '.md',
|
|
|
|
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
|
|
|
})
|
|
|
|
res.send(body)
|
2016-08-19 03:31:23 +00:00
|
|
|
}
|
2016-08-19 03:24:36 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function actionInfo (req, res, note) {
|
|
|
|
var body = note.content
|
|
|
|
var extracted = models.Note.extractMeta(body)
|
|
|
|
var markdown = extracted.markdown
|
|
|
|
var meta = models.Note.parseMeta(extracted.meta)
|
|
|
|
var createtime = note.createdAt
|
|
|
|
var updatetime = note.lastchangeAt
|
|
|
|
var title = models.Note.decodeTitle(note.title)
|
|
|
|
var data = {
|
|
|
|
title: meta.title || title,
|
|
|
|
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
|
|
|
|
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',
|
|
|
|
'Cache-Control': 'private', // only cache by client
|
|
|
|
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
|
|
|
})
|
|
|
|
res.send(data)
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function actionPDF (req, res, note) {
|
|
|
|
var body = note.content
|
|
|
|
var extracted = models.Note.extractMeta(body)
|
|
|
|
var title = models.Note.decodeTitle(note.title)
|
2015-05-04 07:53:29 +00:00
|
|
|
|
2018-03-07 14:17:35 +00:00
|
|
|
if (!fs.existsSync(config.tmpPath)) {
|
|
|
|
fs.mkdirSync(config.tmpPath)
|
2017-03-08 10:45:51 +00:00
|
|
|
}
|
2018-03-07 14:17:35 +00:00
|
|
|
var path = config.tmpPath + '/' + Date.now() + '.pdf'
|
2017-03-08 10:45:51 +00:00
|
|
|
markdownpdf().from.string(extracted.markdown).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
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function actionGist (req, res, note) {
|
|
|
|
var data = {
|
|
|
|
client_id: config.github.clientID,
|
2018-03-07 14:17:35 +00:00
|
|
|
redirect_uri: config.serverURL + '/auth/github/callback/' + models.Note.encodeNoteId(note.id) + '/gist',
|
2017-03-08 10:45:51 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +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)
|
2016-06-17 08:11:14 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
if (!content) {
|
|
|
|
return response.errorNotFound(res)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
})
|
|
|
|
res.send(content)
|
|
|
|
})
|
2016-06-17 08:11:14 +00:00
|
|
|
} else {
|
2017-03-08 10:45:51 +00:00
|
|
|
return response.errorNotFound(res)
|
2016-06-17 08:11:14 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
} else {
|
|
|
|
models.Revision.getNoteRevisions(note, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
}
|
|
|
|
var out = {
|
|
|
|
revision: data
|
|
|
|
}
|
|
|
|
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
|
|
|
|
})
|
|
|
|
res.send(out)
|
|
|
|
})
|
|
|
|
}
|
2016-06-17 08:11:14 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +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 'info':
|
|
|
|
actionInfo(req, res, note)
|
|
|
|
break
|
|
|
|
case 'pdf':
|
2018-03-07 14:17:35 +00:00
|
|
|
if (config.allowPDFExport) {
|
2017-10-25 13:51:34 +00:00
|
|
|
actionPDF(req, res, note)
|
|
|
|
} else {
|
2018-03-07 14:17:35 +00:00
|
|
|
logger.error('PDF export failed: Disabled by config. Set "allowPDFExport: true" to enable. Check the documentation for details')
|
2017-10-25 13:51:34 +00:00
|
|
|
response.errorForbidden(res)
|
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
break
|
|
|
|
case 'gist':
|
|
|
|
actionGist(req, res, note)
|
|
|
|
break
|
|
|
|
case 'revision':
|
|
|
|
actionRevision(req, res, note)
|
|
|
|
break
|
|
|
|
default:
|
2018-03-07 14:17:35 +00:00
|
|
|
return res.redirect(config.serverURL + '/' + noteId)
|
2017-03-08 10:45:51 +00:00
|
|
|
}
|
|
|
|
})
|
2015-05-04 07:53:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function publishNoteActions (req, res, next) {
|
|
|
|
findNote(req, res, function (note) {
|
|
|
|
var action = req.params.action
|
|
|
|
switch (action) {
|
|
|
|
case 'edit':
|
2018-03-07 14:17:35 +00:00
|
|
|
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : models.Note.encodeNoteId(note.id)))
|
2017-03-08 10:45:51 +00:00
|
|
|
break
|
|
|
|
default:
|
2018-03-07 14:17:35 +00:00
|
|
|
res.redirect(config.serverURL + '/s/' + note.shortid)
|
2017-03-08 10:45:51 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2015-07-01 16:10:20 +00:00
|
|
|
}
|
2016-01-31 21:42:26 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function publishSlideActions (req, res, next) {
|
|
|
|
findNote(req, res, function (note) {
|
|
|
|
var action = req.params.action
|
|
|
|
switch (action) {
|
|
|
|
case 'edit':
|
2018-03-07 14:17:35 +00:00
|
|
|
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : models.Note.encodeNoteId(note.id)))
|
2017-03-08 10:45:51 +00:00
|
|
|
break
|
|
|
|
default:
|
2018-03-07 14:17:35 +00:00
|
|
|
res.redirect(config.serverURL + '/p/' + note.shortid)
|
2017-03-08 10:45:51 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2016-08-15 03:25:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +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:
|
2018-03-07 14:17:35 +00:00
|
|
|
res.redirect(config.serverURL + '/' + noteId)
|
2017-03-08 10:45:51 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2016-01-31 21:42:26 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +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
|
|
|
|
}
|
|
|
|
var authUrl = 'https://github.com/login/oauth/access_token'
|
|
|
|
request({
|
|
|
|
url: authUrl,
|
|
|
|
method: 'POST',
|
|
|
|
json: data
|
|
|
|
}, function (error, httpResponse, body) {
|
|
|
|
if (!error && httpResponse.statusCode === 200) {
|
|
|
|
var accessToken = body.access_token
|
|
|
|
if (accessToken) {
|
|
|
|
var content = note.content
|
|
|
|
var title = models.Note.decodeTitle(note.title)
|
|
|
|
var filename = title.replace('/', ' ') + '.md'
|
|
|
|
var gist = {
|
|
|
|
'files': {}
|
|
|
|
}
|
|
|
|
gist.files[filename] = {
|
|
|
|
'content': content
|
|
|
|
}
|
|
|
|
var gistUrl = 'https://api.github.com/gists'
|
|
|
|
request({
|
|
|
|
url: gistUrl,
|
|
|
|
headers: {
|
|
|
|
'User-Agent': 'HackMD',
|
|
|
|
'Authorization': 'token ' + accessToken
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
json: gist
|
|
|
|
}, function (error, httpResponse, body) {
|
|
|
|
if (!error && httpResponse.statusCode === 201) {
|
|
|
|
res.setHeader('referer', '')
|
|
|
|
res.redirect(body.html_url)
|
2016-04-20 10:03:55 +00:00
|
|
|
} else {
|
2017-03-08 10:45:51 +00:00
|
|
|
return response.errorForbidden(res)
|
2016-04-20 10:03:55 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +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
|
|
|
|
2017-03-08 10:45:51 +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:
|
2018-03-07 14:17:35 +00:00
|
|
|
res.redirect(config.serverURL + '/' + noteId)
|
2017-03-08 10:45:51 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2016-05-16 10:16:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
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(
|
2016-05-16 10:16:45 +00:00
|
|
|
config.gitlab.baseURL + '/api/v3/projects?access_token=' + user.accessToken,
|
2017-03-08 10:45:51 +00:00
|
|
|
function (error, httpResponse, body) {
|
|
|
|
if (!error && httpResponse.statusCode === 200) {
|
|
|
|
ret.projects = JSON.parse(body)
|
|
|
|
return res.send(ret)
|
|
|
|
} else {
|
|
|
|
return res.send(ret)
|
|
|
|
}
|
2016-05-16 10:16:45 +00:00
|
|
|
}
|
2017-03-08 10:45:51 +00:00
|
|
|
)
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('gitlab action projects failed: ' + err)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return response.errorForbidden(res)
|
|
|
|
}
|
2016-05-16 10:16:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function showPublishSlide (req, res, next) {
|
|
|
|
var include = [{
|
|
|
|
model: models.User,
|
|
|
|
as: 'owner'
|
|
|
|
}, {
|
|
|
|
model: models.User,
|
|
|
|
as: 'lastchangeuser'
|
|
|
|
}]
|
|
|
|
findNote(req, res, function (note) {
|
|
|
|
// force to use short id
|
|
|
|
var shortid = req.params.shortid
|
2018-03-07 14:17:35 +00:00
|
|
|
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) { return res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid)) }
|
2017-03-08 10:45:51 +00:00
|
|
|
note.increment('viewcount').then(function (note) {
|
|
|
|
if (!note) {
|
|
|
|
return response.errorNotFound(res)
|
|
|
|
}
|
|
|
|
var body = note.content
|
|
|
|
var extracted = models.Note.extractMeta(body)
|
|
|
|
var markdown = extracted.markdown
|
|
|
|
var meta = models.Note.parseMeta(extracted.meta)
|
|
|
|
var createtime = note.createdAt
|
|
|
|
var updatetime = note.lastchangeAt
|
|
|
|
var title = models.Note.decodeTitle(note.title)
|
|
|
|
title = models.Note.generateWebTitle(meta.title || title)
|
2018-03-07 14:17:35 +00:00
|
|
|
var origin = config.serverURL
|
2017-03-08 10:45:51 +00:00
|
|
|
var data = {
|
|
|
|
title: title,
|
|
|
|
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
|
|
|
|
viewcount: note.viewcount,
|
|
|
|
createtime: createtime,
|
|
|
|
updatetime: updatetime,
|
|
|
|
url: origin,
|
|
|
|
body: markdown,
|
2017-06-04 17:12:40 +00:00
|
|
|
theme: meta.slideOptions && utils.isRevealTheme(meta.slideOptions.theme),
|
2017-03-08 10:45:51 +00:00
|
|
|
meta: JSON.stringify(extracted.meta),
|
2018-03-07 14:17:35 +00:00
|
|
|
useCDN: config.useCDN,
|
2017-03-08 10:45:51 +00:00
|
|
|
owner: note.owner ? note.owner.id : null,
|
|
|
|
ownerprofile: note.owner ? models.User.getProfile(note.owner) : null,
|
|
|
|
lastchangeuser: note.lastchangeuser ? note.lastchangeuser.id : null,
|
|
|
|
lastchangeuserprofile: note.lastchangeuser ? models.User.getProfile(note.lastchangeuser) : null,
|
|
|
|
robots: meta.robots || false, // default allow robots
|
|
|
|
GA: meta.GA,
|
2017-10-18 15:48:53 +00:00
|
|
|
disqus: meta.disqus,
|
|
|
|
cspNonce: res.locals.nonce
|
2017-03-08 10:45:51 +00:00
|
|
|
}
|
|
|
|
return renderPublishSlide(data, res)
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
})
|
|
|
|
}, include)
|
2015-11-23 12:38:26 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function renderPublishSlide (data, res) {
|
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private' // only cache by client
|
|
|
|
})
|
2018-03-07 14:17:35 +00:00
|
|
|
res.render(config.slidePath, data)
|
2016-06-21 13:42:03 +00:00
|
|
|
}
|
2015-11-23 12:38:26 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
module.exports = response
|