feat: page history - download version

This commit is contained in:
NGPixel
2020-02-24 21:10:43 -05:00
committed by Nicolas Giard
parent 95f01cdeb8
commit 2ac9131244
4 changed files with 89 additions and 27 deletions

View File

@@ -36,6 +36,49 @@ router.get(['/a', '/a/*'], (req, res, next) => {
res.render('admin')
})
/**
* Download Page / Version
*/
router.get(['/d', '/d/*'], async (req, res, next) => {
const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
const versionId = (req.query.v) ? _.toSafeInteger(req.query.v) : 0
const page = await WIKI.models.pages.getPageFromDb({
path: pageArgs.path,
locale: pageArgs.locale,
userId: req.user.id,
isPrivate: false
})
pageArgs.tags = _.get(page, 'tags', [])
if (versionId > 0) {
if (!WIKI.auth.checkAccess(req.user, ['read:history'], pageArgs)) {
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
return res.render('unauthorized', { action: 'downloadVersion' })
}
} else {
if (!WIKI.auth.checkAccess(req.user, ['read:source'], pageArgs)) {
_.set(res.locals, 'pageMeta.title', 'Unauthorized')
return res.render('unauthorized', { action: 'download' })
}
}
if (page) {
const fileName = _.last(page.path.split('/')) + '.' + pageHelper.getFileExtension(page.contentType)
res.attachment(fileName)
if (versionId > 0) {
const pageVersion = await WIKI.models.pageHistory.getVersion({ pageId: page.id, versionId })
res.send(pageHelper.injectPageMetadata(pageVersion))
} else {
res.send(pageHelper.injectPageMetadata(page))
}
} else {
res.status(404).end()
}
})
/**
* Create/Edit document
*/

View File

@@ -73,11 +73,14 @@ module.exports = {
['date', page.updatedAt],
['tags', page.tags ? page.tags.map(t => t.tag).join(', ') : '']
]
const inject = {
'markdown': '---\n' + meta.map(mt => `${mt[0]}: ${mt[1]}`).join('\n') + '\n---\n\n' + page.content,
'html': '<!--\n' + meta.map(mt => `${mt[0]}: ${mt[1]}`).join('\n') + '\n-->\n\n' + page.content
switch (page.contentType) {
case 'markdown':
return '---\n' + meta.map(mt => `${mt[0]}: ${mt[1]}`).join('\n') + '\n---\n\n' + page.content
case 'html':
return '<!--\n' + meta.map(mt => `${mt[0]}: ${mt[1]}`).join('\n') + '\n-->\n\n' + page.content
default:
return page.content
}
return _.get(inject, page.contentType, page.content)
},
/**
* Check if path is a reserved path

View File

@@ -134,6 +134,7 @@ module.exports = class PageHistory extends Model {
}).first()
return {
...version,
updatedAt: version.createdAt,
tags: []
}
}