2022-07-10 12:46:25 +00:00
|
|
|
const { Router } = require('hyper-express')
|
|
|
|
const routes = new Router()
|
2022-08-04 14:59:06 +00:00
|
|
|
const upload = require('./../controllers/uploadController')
|
2019-09-08 01:56:29 +00:00
|
|
|
const utils = require('./../controllers/utilsController')
|
2022-10-05 19:39:51 +00:00
|
|
|
const config = require('./../controllers/utils/ConfigManager')
|
2018-04-13 16:20:57 +00:00
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
routes.get('/nojs', async (req, res) => {
|
2019-09-19 18:27:00 +00:00
|
|
|
return res.render('nojs', {
|
2022-08-19 02:30:09 +00:00
|
|
|
config, utils, versions: utils.versionStrings
|
2022-08-19 02:50:08 +00:00
|
|
|
}, !utils.devmode)
|
2018-04-12 14:37:42 +00:00
|
|
|
})
|
|
|
|
|
2022-08-04 18:12:58 +00:00
|
|
|
// HyperExpress defaults to 250kb
|
2022-09-21 01:03:28 +00:00
|
|
|
// https://github.com/kartikk221/hyper-express/blob/6.4.8/docs/Server.md#server-constructor-options
|
|
|
|
routes.post('/nojs', {
|
|
|
|
max_body_length: parseInt(config.uploads.maxSize) * 1e6,
|
|
|
|
middlewares: [
|
|
|
|
async (req, res) => {
|
|
|
|
// Assert Request type early
|
|
|
|
utils.assertRequestType(req, 'multipart/form-data')
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}, async (req, res) => {
|
2022-08-19 02:30:09 +00:00
|
|
|
// Map built-in Response.json() function into Response.render() accordingly
|
|
|
|
// Since NoJS uploader needs to reply with a complete HTML page
|
2018-04-13 16:20:57 +00:00
|
|
|
res._json = res.json
|
|
|
|
res.json = (...args) => {
|
|
|
|
const result = args[0]
|
2019-09-17 04:13:41 +00:00
|
|
|
return res.render('nojs', {
|
|
|
|
config,
|
2022-06-29 06:58:09 +00:00
|
|
|
utils,
|
2019-09-19 12:10:37 +00:00
|
|
|
versions: utils.versionStrings,
|
2019-09-17 04:13:41 +00:00
|
|
|
errorMessage: result.success ? '' : (result.description || 'An unexpected error occurred.'),
|
|
|
|
files: result.files || [{}]
|
|
|
|
})
|
2018-04-13 16:20:57 +00:00
|
|
|
}
|
2022-09-21 01:03:28 +00:00
|
|
|
|
|
|
|
// Indicate uploadController.js to additionally process this request further
|
|
|
|
// (skip request type assertion, parse token from form input, etc.)
|
|
|
|
req.locals.nojs = true
|
|
|
|
|
2022-08-04 14:59:06 +00:00
|
|
|
return upload.upload(req, res)
|
2018-04-12 14:37:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
module.exports = routes
|