2017-03-14 01:02:43 -04:00
|
|
|
'use strict'
|
2017-03-08 05:45:51 -05:00
|
|
|
// response
|
|
|
|
// external modules
|
|
|
|
var fs = require('fs')
|
2018-05-22 19:14:52 -04:00
|
|
|
var path = require('path')
|
2017-03-08 05:45:51 -05:00
|
|
|
var markdownpdf = require('markdown-pdf')
|
|
|
|
var shortId = require('shortid')
|
|
|
|
var querystring = require('querystring')
|
|
|
|
var request = require('request')
|
|
|
|
var moment = require('moment')
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// core
|
2017-04-12 12:20:28 -04:00
|
|
|
var config = require('./config')
|
|
|
|
var logger = require('./logger')
|
2017-03-08 05:45:51 -05:00
|
|
|
var models = require('./models')
|
2017-06-01 04:47:52 -04:00
|
|
|
var utils = require('./utils')
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
// public
|
2015-05-04 03:53:29 -04:00
|
|
|
var response = {
|
2017-03-08 05:45:51 -05:00
|
|
|
errorForbidden: function (res) {
|
2018-03-26 08:58:25 -04:00
|
|
|
const {req} = res
|
|
|
|
if (req.user) {
|
|
|
|
responseError(res, '403', 'Forbidden', 'oh no.')
|
|
|
|
} else {
|
|
|
|
req.flash('error', 'You are not allowed to access this page. Maybe try logging in?')
|
2019-02-27 03:23:58 -05:00
|
|
|
res.redirect(config.serverURL + '/')
|
2018-03-26 08:58:25 -04:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
},
|
|
|
|
errorNotFound: function (res) {
|
|
|
|
responseError(res, '404', 'Not Found', 'oops.')
|
|
|
|
},
|
|
|
|
errorBadRequest: function (res) {
|
|
|
|
responseError(res, '400', 'Bad Request', 'something not right.')
|
|
|
|
},
|
2018-09-26 10:00:01 -04:00
|
|
|
errorTooLong: function (res) {
|
|
|
|
responseError(res, '413', 'Payload Too Large', 'Shorten your note!')
|
|
|
|
},
|
2017-03-08 05:45:51 -05:00
|
|
|
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 03:53:29 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function responseError (res, code, detail, msg) {
|
2018-09-10 16:35:38 -04:00
|
|
|
res.status(code).render('error.ejs', {
|
2017-03-08 05:45:51 -05:00
|
|
|
title: code + ' ' + detail + ' ' + msg,
|
|
|
|
code: code,
|
|
|
|
detail: detail,
|
2018-09-13 15:26:39 -04:00
|
|
|
msg: msg
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function showIndex (req, res, next) {
|
2018-05-25 12:19:31 -04:00
|
|
|
var authStatus = req.isAuthenticated()
|
|
|
|
var deleteToken = ''
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
signin: authStatus,
|
2017-03-08 05:45:51 -05:00
|
|
|
infoMessage: req.flash('info'),
|
2018-05-22 19:14:52 -04:00
|
|
|
errorMessage: req.flash('error'),
|
|
|
|
privacyStatement: fs.existsSync(path.join(config.docsPath, 'privacy.md')),
|
2018-05-25 12:19:31 -04:00
|
|
|
termsOfUse: fs.existsSync(path.join(config.docsPath, 'terms-of-use.md')),
|
|
|
|
deleteToken: deleteToken
|
|
|
|
}
|
|
|
|
|
|
|
|
if (authStatus) {
|
|
|
|
models.User.findOne({
|
|
|
|
where: {
|
|
|
|
id: req.user.id
|
|
|
|
}
|
|
|
|
}).then(function (user) {
|
|
|
|
if (user) {
|
|
|
|
data.deleteToken = user.deleteToken
|
2018-09-10 16:35:38 -04:00
|
|
|
res.render('index.ejs', data)
|
2018-05-25 12:19:31 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
2018-09-10 16:35:38 -04:00
|
|
|
res.render('index.ejs', data)
|
2018-05-25 12:19:31 -04:00
|
|
|
}
|
2015-09-22 00:06:13 -04:00
|
|
|
}
|
|
|
|
|
2018-06-24 07:59:18 -04:00
|
|
|
function responseCodiMD (res, note) {
|
2017-03-08 05:45:51 -05:00
|
|
|
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-09-10 16:35:38 -04:00
|
|
|
res.render('codimd.ejs', {
|
2018-09-13 15:26:39 -04:00
|
|
|
title: title
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function newNote (req, res, next) {
|
|
|
|
var owner = null
|
2018-09-26 10:00:01 -04:00
|
|
|
var body = ''
|
|
|
|
if (req.body && req.body.length > config.documentMaxLength) {
|
|
|
|
return response.errorTooLong(res)
|
2018-09-27 18:17:34 -04:00
|
|
|
} else if (req.body) {
|
2018-09-26 10:00:01 -04:00
|
|
|
body = req.body
|
|
|
|
}
|
2018-06-23 18:32:41 -04:00
|
|
|
body = body.replace(/[\r]/g, '')
|
2017-03-08 05:45:51 -05:00
|
|
|
if (req.isAuthenticated()) {
|
|
|
|
owner = req.user.id
|
2018-03-07 09:17:35 -05:00
|
|
|
} else if (!config.allowAnonymous) {
|
2017-03-08 05:45:51 -05:00
|
|
|
return response.errorForbidden(res)
|
|
|
|
}
|
|
|
|
models.Note.create({
|
|
|
|
ownerId: owner,
|
2018-01-10 18:51:22 -05:00
|
|
|
alias: req.alias ? req.alias : null,
|
2018-06-23 18:32:41 -04:00
|
|
|
content: body
|
2017-03-08 05:45:51 -05:00
|
|
|
}).then(function (note) {
|
2018-03-07 09:17:35 -05:00
|
|
|
return res.redirect(config.serverURL + '/' + models.Note.encodeNoteId(note.id))
|
2017-03-08 05:45:51 -05:00
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
})
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05: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 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05: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 11:47:50 -05:00
|
|
|
logger.error(err)
|
|
|
|
return response.errorInternalError(res)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
|
|
|
models.Note.findOne({
|
|
|
|
where: {
|
|
|
|
id: _id
|
|
|
|
},
|
|
|
|
include: include || null
|
|
|
|
}).then(function (note) {
|
|
|
|
if (!note) {
|
2018-11-12 18:14:25 -05:00
|
|
|
if (config.allowFreeURL && noteId && !config.forbiddenNoteIDs.includes(noteId)) {
|
2017-03-08 05:45:51 -05: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 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function showNote (req, res, next) {
|
|
|
|
findNote(req, res, function (note) {
|
|
|
|
// force to use note id
|
|
|
|
var noteId = req.params.noteId
|
2018-02-26 03:43:29 -05:00
|
|
|
var id = models.Note.encodeNoteId(note.id)
|
2018-03-07 09:17:35 -05:00
|
|
|
if ((note.alias && noteId !== note.alias) || (!note.alias && noteId !== id)) { return res.redirect(config.serverURL + '/' + (note.alias || id)) }
|
2018-06-24 07:59:18 -04:00
|
|
|
return responseCodiMD(res, note)
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
2016-04-20 06:03:55 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05: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 09:17:35 -05:00
|
|
|
return res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
2017-03-08 05:45:51 -05: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)
|
|
|
|
var data = {
|
|
|
|
title: title,
|
|
|
|
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
|
|
|
|
viewcount: note.viewcount,
|
|
|
|
createtime: createtime,
|
|
|
|
updatetime: updatetime,
|
|
|
|
body: body,
|
|
|
|
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 10:33:32 -04:00
|
|
|
disqus: meta.disqus,
|
|
|
|
cspNonce: res.locals.nonce
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
|
|
|
return renderPublish(data, res)
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
})
|
|
|
|
}, include)
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function renderPublish (data, res) {
|
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private' // only cache by client
|
|
|
|
})
|
2018-09-10 16:35:38 -04:00
|
|
|
res.render('pretty.ejs', data)
|
2016-01-12 09:01:42 -05:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function actionPublish (req, res, note) {
|
2018-03-07 09:17:35 -05:00
|
|
|
res.redirect(config.serverURL + '/s/' + (note.alias || note.shortid))
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function actionSlide (req, res, note) {
|
2018-03-07 09:17:35 -05:00
|
|
|
res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid))
|
2015-11-23 07:38:26 -05:00
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 05:45:51 -05: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-18 23:31:23 -04:00
|
|
|
}
|
2016-08-18 23:24:36 -04:00
|
|
|
|
2017-03-08 05:45:51 -05: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 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function actionPDF (req, res, note) {
|
2018-06-23 19:00:20 -04:00
|
|
|
var url = config.serverURL || 'http://' + req.get('host')
|
2017-03-08 05:45:51 -05:00
|
|
|
var body = note.content
|
|
|
|
var extracted = models.Note.extractMeta(body)
|
2018-06-23 19:00:20 -04:00
|
|
|
var content = extracted.markdown
|
2017-03-08 05:45:51 -05:00
|
|
|
var title = models.Note.decodeTitle(note.title)
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2018-03-07 09:17:35 -05:00
|
|
|
if (!fs.existsSync(config.tmpPath)) {
|
|
|
|
fs.mkdirSync(config.tmpPath)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2018-03-07 09:17:35 -05:00
|
|
|
var path = config.tmpPath + '/' + Date.now() + '.pdf'
|
2018-06-23 19:00:20 -04:00
|
|
|
content = content.replace(/\]\(\//g, '](' + url + '/')
|
|
|
|
markdownpdf().from.string(content).to(path, function () {
|
2018-09-24 11:55:03 -04:00
|
|
|
if (!fs.existsSync(path)) {
|
|
|
|
logger.error('PDF seems to not be generated as expected. File doesn\'t exist: ' + path)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
}
|
2017-03-08 05:45:51 -05: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)
|
2019-01-18 16:51:01 -05:00
|
|
|
fs.unlinkSync(path)
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function actionGist (req, res, note) {
|
|
|
|
var data = {
|
|
|
|
client_id: config.github.clientID,
|
2018-03-07 09:17:35 -05:00
|
|
|
redirect_uri: config.serverURL + '/auth/github/callback/' + models.Note.encodeNoteId(note.id) + '/gist',
|
2017-03-08 05:45:51 -05:00
|
|
|
scope: 'gist',
|
|
|
|
state: shortId.generate()
|
|
|
|
}
|
|
|
|
var query = querystring.stringify(data)
|
|
|
|
res.redirect('https://github.com/login/oauth/authorize?' + query)
|
2016-01-31 16:42:26 -05:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05: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 04:11:14 -04:00
|
|
|
}
|
2017-03-08 05:45:51 -05: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 04:11:14 -04:00
|
|
|
} else {
|
2017-03-08 05:45:51 -05:00
|
|
|
return response.errorNotFound(res)
|
2016-06-17 04:11:14 -04:00
|
|
|
}
|
2017-03-08 05:45:51 -05: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 04:11:14 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05: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 09:17:35 -05:00
|
|
|
if (config.allowPDFExport) {
|
2017-10-25 09:51:34 -04:00
|
|
|
actionPDF(req, res, note)
|
|
|
|
} else {
|
2018-03-07 09:17:35 -05:00
|
|
|
logger.error('PDF export failed: Disabled by config. Set "allowPDFExport: true" to enable. Check the documentation for details')
|
2017-10-25 09:51:34 -04:00
|
|
|
response.errorForbidden(res)
|
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
break
|
|
|
|
case 'gist':
|
|
|
|
actionGist(req, res, note)
|
|
|
|
break
|
|
|
|
case 'revision':
|
|
|
|
actionRevision(req, res, note)
|
|
|
|
break
|
|
|
|
default:
|
2018-03-07 09:17:35 -05:00
|
|
|
return res.redirect(config.serverURL + '/' + noteId)
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
|
|
|
})
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function publishNoteActions (req, res, next) {
|
|
|
|
findNote(req, res, function (note) {
|
|
|
|
var action = req.params.action
|
|
|
|
switch (action) {
|
2018-10-27 19:55:14 -04:00
|
|
|
case 'download':
|
|
|
|
actionDownload(req, res, note)
|
2018-10-27 20:54:01 -04:00
|
|
|
break
|
2017-03-08 05:45:51 -05:00
|
|
|
case 'edit':
|
2018-03-07 09:17:35 -05:00
|
|
|
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : models.Note.encodeNoteId(note.id)))
|
2017-03-08 05:45:51 -05:00
|
|
|
break
|
|
|
|
default:
|
2018-03-07 09:17:35 -05:00
|
|
|
res.redirect(config.serverURL + '/s/' + note.shortid)
|
2017-03-08 05:45:51 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2015-07-01 12:10:20 -04:00
|
|
|
}
|
2016-01-31 16:42:26 -05:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function publishSlideActions (req, res, next) {
|
|
|
|
findNote(req, res, function (note) {
|
|
|
|
var action = req.params.action
|
|
|
|
switch (action) {
|
|
|
|
case 'edit':
|
2018-03-07 09:17:35 -05:00
|
|
|
res.redirect(config.serverURL + '/' + (note.alias ? note.alias : models.Note.encodeNoteId(note.id)))
|
2017-03-08 05:45:51 -05:00
|
|
|
break
|
|
|
|
default:
|
2018-03-07 09:17:35 -05:00
|
|
|
res.redirect(config.serverURL + '/p/' + note.shortid)
|
2017-03-08 05:45:51 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2016-08-14 23:25:27 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05: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 09:17:35 -05:00
|
|
|
res.redirect(config.serverURL + '/' + noteId)
|
2017-03-08 05:45:51 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2016-01-31 16:42:26 -05:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05: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: {
|
2018-06-24 08:13:38 -04:00
|
|
|
'User-Agent': 'CodiMD',
|
2017-03-08 05:45:51 -05:00
|
|
|
'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 06:03:55 -04:00
|
|
|
} else {
|
2017-03-08 05:45:51 -05:00
|
|
|
return response.errorForbidden(res)
|
2016-04-20 06:03:55 -04:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return response.errorForbidden(res)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return response.errorForbidden(res)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-01-31 16:42:26 -05:00
|
|
|
}
|
2015-07-01 12:10:20 -04:00
|
|
|
|
2017-03-08 05:45:51 -05: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 09:17:35 -05:00
|
|
|
res.redirect(config.serverURL + '/' + noteId)
|
2017-03-08 05:45:51 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2016-05-16 06:16:45 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05: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) }
|
2018-07-30 09:47:09 -04:00
|
|
|
var ret = { baseURL: config.gitlab.baseURL, version: config.gitlab.version }
|
2017-03-08 05:45:51 -05:00
|
|
|
ret.accesstoken = user.accessToken
|
|
|
|
ret.profileid = user.profileid
|
|
|
|
request(
|
2018-10-09 03:04:04 -04:00
|
|
|
config.gitlab.baseURL + '/api/' + config.gitlab.version + '/projects?membership=yes&per_page=100&access_token=' + user.accessToken,
|
2017-03-08 05:45:51 -05: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 06:16:45 -04:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
)
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error('gitlab action projects failed: ' + err)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return response.errorForbidden(res)
|
|
|
|
}
|
2016-05-16 06:16:45 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05: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 09:17:35 -05: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 05:45:51 -05: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)
|
|
|
|
var data = {
|
|
|
|
title: title,
|
|
|
|
description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null),
|
|
|
|
viewcount: note.viewcount,
|
|
|
|
createtime: createtime,
|
|
|
|
updatetime: updatetime,
|
|
|
|
body: markdown,
|
2017-06-04 13:12:40 -04:00
|
|
|
theme: meta.slideOptions && utils.isRevealTheme(meta.slideOptions.theme),
|
2017-03-08 05:45:51 -05:00
|
|
|
meta: JSON.stringify(extracted.meta),
|
|
|
|
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 11:48:53 -04:00
|
|
|
disqus: meta.disqus,
|
|
|
|
cspNonce: res.locals.nonce
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
|
|
|
return renderPublishSlide(data, res)
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
})
|
|
|
|
}, include)
|
2015-11-23 07:38:26 -05:00
|
|
|
}
|
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function renderPublishSlide (data, res) {
|
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private' // only cache by client
|
|
|
|
})
|
2018-09-10 16:35:38 -04:00
|
|
|
res.render('slide.ejs', data)
|
2016-06-21 09:42:03 -04:00
|
|
|
}
|
2015-11-23 07:38:26 -05:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
module.exports = response
|