filesafe/routes/nojs.js
Bobby Wibowo ea30e5dee5
feat: enable persistent cache for nojs uploader
also slight improvement to NunjucksRenderer class
2022-08-19 09:50:08 +07:00

34 lines
1.1 KiB
JavaScript

const { Router } = require('hyper-express')
const routes = new Router()
const upload = require('./../controllers/uploadController')
const utils = require('./../controllers/utilsController')
const config = require('./../config')
routes.get('/nojs', async (req, res) => {
return res.render('nojs', {
config, utils, versions: utils.versionStrings
}, !utils.devmode)
})
// HyperExpress defaults to 250kb
// https://github.com/kartikk221/hyper-express/blob/6.4.4/docs/Server.md#server-constructor-options
const maxBodyLength = parseInt(config.uploads.maxSize) * 1e6
routes.post('/nojs', { max_body_length: maxBodyLength }, async (req, res) => {
// Map built-in Response.json() function into Response.render() accordingly
// Since NoJS uploader needs to reply with a complete HTML page
res._json = res.json
res.json = (...args) => {
const result = args[0]
return res.render('nojs', {
config,
utils,
versions: utils.versionStrings,
errorMessage: result.success ? '' : (result.description || 'An unexpected error occurred.'),
files: result.files || [{}]
})
}
return upload.upload(req, res)
})
module.exports = routes