common.js 6.3 KB
Newer Older
1 2
const express = require('express')
const router = express.Router()
3
const pageHelper = require('../helpers/page')
4
const _ = require('lodash')
5 6

/* global WIKI */
NGPixel's avatar
NGPixel committed
7

8 9 10 11 12 13
/**
 * Robots.txt
 */
router.get('/robots.txt', (req, res, next) => {
  res.type('text/plain')
  if (_.includes(WIKI.config.seo.robots, 'noindex')) {
14
    res.send('User-agent: *\nDisallow: /')
15 16 17 18 19
  } else {
    res.status(200).end()
  }
})

Nick's avatar
Nick committed
20 21 22 23 24 25 26 27 28 29 30
/**
 * Health Endpoint
 */
router.get('/healthz', (req, res, next) => {
  if (WIKI.models.knex.client.pool.numFree() < 1 && WIKI.models.knex.client.pool.numUsed() < 1) {
    res.status(503).json({ ok: false }).end()
  } else {
    res.status(200).json({ ok: true }).end()
  }
})

31 32 33 34 35 36 37 38
/**
 * Administration
 */
router.get(['/a', '/a/*'], (req, res, next) => {
  _.set(res.locals, 'pageMeta.title', 'Admin')
  res.render('admin')
})

NGPixel's avatar
NGPixel committed
39 40 41
/**
 * Create/Edit document
 */
42
router.get(['/e', '/e/*'], async (req, res, next) => {
43
  const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
44

45 46 47 48
  if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) {
    return res.redirect(`/e/${pageArgs.locale}/${pageArgs.path}`)
  }

49 50
  _.set(res, 'locals.siteConfig.lang', pageArgs.locale)

51 52 53 54
  if (pageHelper.isReservedPath(pageArgs.path)) {
    return next(new Error('Cannot create this page because it starts with a system reserved path.'))
  }

55 56 57 58 59 60 61
  let page = await WIKI.models.pages.getPageFromDb({
    path: pageArgs.path,
    locale: pageArgs.locale,
    userId: req.user.id,
    isPrivate: false
  })
  if (page) {
62
    if (!WIKI.auth.checkAccess(req.user, ['manage:pages'], pageArgs)) {
63
      _.set(res.locals, 'pageMeta.title', 'Unauthorized')
64
      return res.render('unauthorized', { action: 'edit' })
65 66
    }

67 68
    _.set(res.locals, 'pageMeta.title', `Edit ${page.title}`)
    _.set(res.locals, 'pageMeta.description', page.description)
69 70 71 72
    page.mode = 'update'
    page.isPublished = (page.isPublished === true || page.isPublished === 1) ? 'true' : 'false'
    page.content = Buffer.from(page.content).toString('base64')
  } else {
73
    if (!WIKI.auth.checkAccess(req.user, ['write:pages'], pageArgs)) {
74
      _.set(res.locals, 'pageMeta.title', 'Unauthorized')
75
      return res.render('unauthorized', { action: 'create' })
76 77
    }

78
    _.set(res.locals, 'pageMeta.title', `New Page`)
79 80 81 82 83 84 85 86 87
    page = {
      path: pageArgs.path,
      localeCode: pageArgs.locale,
      editorKey: null,
      mode: 'create',
      content: null
    }
  }
  res.render('editor', { page })
NGPixel's avatar
NGPixel committed
88 89
})

90
/**
91
 * History
92 93
 */
router.get(['/h', '/h/*'], async (req, res, next) => {
94
  const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
95

96 97 98 99
  if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) {
    return res.redirect(`/h/${pageArgs.locale}/${pageArgs.path}`)
  }

100 101
  _.set(res, 'locals.siteConfig.lang', pageArgs.locale)

102
  if (!WIKI.auth.checkAccess(req.user, ['read:pages'], pageArgs)) {
103
    _.set(res.locals, 'pageMeta.title', 'Unauthorized')
104
    return res.render('unauthorized', { action: 'history' })
105 106
  }

107
  const page = await WIKI.models.pages.getPageFromDb({
108 109 110 111 112 113
    path: pageArgs.path,
    locale: pageArgs.locale,
    userId: req.user.id,
    isPrivate: false
  })
  if (page) {
114 115
    _.set(res.locals, 'pageMeta.title', page.title)
    _.set(res.locals, 'pageMeta.description', page.description)
116 117 118 119 120 121
    res.render('history', { page })
  } else {
    res.redirect(`/${pageArgs.path}`)
  }
})

122 123 124 125 126 127 128 129
/**
 * Profile
 */
router.get(['/p', '/p/*'], (req, res, next) => {
  _.set(res.locals, 'pageMeta.title', 'User Profile')
  res.render('profile')
})

130 131 132 133
/**
 * Source
 */
router.get(['/s', '/s/*'], async (req, res, next) => {
134
  const pageArgs = pageHelper.parsePath(req.path, { stripExt: true })
135

136 137 138 139
  if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) {
    return res.redirect(`/s/${pageArgs.locale}/${pageArgs.path}`)
  }

140 141
  _.set(res, 'locals.siteConfig.lang', pageArgs.locale)

142
  if (!WIKI.auth.checkAccess(req.user, ['read:pages'], pageArgs)) {
143
    return res.render('unauthorized', { action: 'source' })
144 145
  }

146 147 148 149 150 151 152
  const page = await WIKI.models.pages.getPageFromDb({
    path: pageArgs.path,
    locale: pageArgs.locale,
    userId: req.user.id,
    isPrivate: false
  })
  if (page) {
153 154
    _.set(res.locals, 'pageMeta.title', page.title)
    _.set(res.locals, 'pageMeta.description', page.description)
155 156 157 158 159 160
    res.render('source', { page })
  } else {
    res.redirect(`/${pageArgs.path}`)
  }
})

NGPixel's avatar
NGPixel committed
161
/**
162
 * View document / asset
NGPixel's avatar
NGPixel committed
163
 */
164
router.get('/*', async (req, res, next) => {
165 166 167
  const stripExt = _.some(WIKI.data.pageExtensions, ext => _.endsWith(req.path, `.${ext}`))
  const pageArgs = pageHelper.parsePath(req.path, { stripExt })
  const isPage = (stripExt || pageArgs.path.indexOf('.') === -1)
168

169
  if (isPage) {
170 171 172 173
    if (WIKI.config.lang.namespacing && !pageArgs.explicitLocale) {
      return res.redirect(`/${pageArgs.locale}/${pageArgs.path}`)
    }

174 175 176 177
    if (!WIKI.auth.checkAccess(req.user, ['read:pages'], pageArgs)) {
      _.set(res.locals, 'pageMeta.title', 'Unauthorized')
      return res.status(403).render('unauthorized', { action: 'view' })
    }
178

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    try {
      const page = await WIKI.models.pages.getPage({
        path: pageArgs.path,
        locale: pageArgs.locale,
        userId: req.user.id,
        isPrivate: false
      })

      _.set(res, 'locals.siteConfig.lang', pageArgs.locale)

      if (page) {
        _.set(res.locals, 'pageMeta.title', page.title)
        _.set(res.locals, 'pageMeta.description', page.description)
        const sidebar = await WIKI.models.navigation.getTree({ cache: true })
        const injectCode = {
          css: WIKI.config.theming.injectCSS,
          head: WIKI.config.theming.injectHead,
          body: WIKI.config.theming.injectBody
        }
        res.render('page', { page, sidebar, injectCode })
      } else if (pageArgs.path === 'home') {
        _.set(res.locals, 'pageMeta.title', 'Welcome')
        res.render('welcome')
202
      } else {
203 204 205 206 207 208
        _.set(res.locals, 'pageMeta.title', 'Page Not Found')
        if (WIKI.auth.checkAccess(req.user, ['write:pages'], pageArgs)) {
          res.status(404).render('new', { pagePath: req.path })
        } else {
          res.status(404).render('notfound', { action: 'view' })
        }
209
      }
210 211
    } catch (err) {
      next(err)
212
    }
213
  } else {
214 215
    if (!WIKI.auth.checkAccess(req.user, ['read:assets'], pageArgs)) {
      return res.sendStatus(403)
216
    }
217 218

    await WIKI.models.assets.getAsset(pageArgs.path, res)
219
  }
220 221
})

222
module.exports = router