2020-04-12 07:11:06 -04:00
|
|
|
import { config } from '../config'
|
2020-04-12 14:45:34 -04:00
|
|
|
import { Router } from 'express'
|
|
|
|
import { errors } from '../errors'
|
|
|
|
import { realtime } from '../realtime'
|
|
|
|
import { Temp } from '../models'
|
|
|
|
import { logger } from '../logger'
|
|
|
|
import { urlencodedParser } from './utils'
|
2020-04-12 07:11:06 -04:00
|
|
|
|
2020-04-12 14:45:34 -04:00
|
|
|
const StatusRouter = Router()
|
2017-04-11 17:39:41 -04:00
|
|
|
|
|
|
|
// get status
|
2020-04-12 14:45:34 -04:00
|
|
|
StatusRouter.get('/status', function (req, res, _) {
|
2017-04-11 17:39:41 -04:00
|
|
|
realtime.getStatus(function (data) {
|
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private', // only cache by client
|
2018-07-03 14:36:40 -04:00
|
|
|
'X-Robots-Tag': 'noindex, nofollow', // prevent crawling
|
|
|
|
'Content-Type': 'application/json'
|
2017-04-11 17:39:41 -04:00
|
|
|
})
|
|
|
|
res.send(data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
// get status
|
2020-04-12 14:45:34 -04:00
|
|
|
StatusRouter.get('/temp', function (req, res) {
|
|
|
|
const host = req.get('host')
|
2018-03-07 09:17:35 -05:00
|
|
|
if (config.allowOrigin.indexOf(host) === -1) {
|
2019-10-27 08:51:53 -04:00
|
|
|
errors.errorForbidden(res)
|
2017-04-11 17:39:41 -04:00
|
|
|
} else {
|
2020-04-12 14:45:34 -04:00
|
|
|
const tempid = req.query.tempid
|
2017-04-11 17:39:41 -04:00
|
|
|
if (!tempid) {
|
2019-10-27 08:51:53 -04:00
|
|
|
errors.errorForbidden(res)
|
2017-04-11 17:39:41 -04:00
|
|
|
} else {
|
2020-04-12 14:45:34 -04:00
|
|
|
Temp.findOne({
|
2017-04-11 17:39:41 -04:00
|
|
|
where: {
|
|
|
|
id: tempid
|
|
|
|
}
|
|
|
|
}).then(function (temp) {
|
|
|
|
if (!temp) {
|
2019-10-27 08:51:53 -04:00
|
|
|
errors.errorNotFound(res)
|
2017-04-11 17:39:41 -04:00
|
|
|
} else {
|
|
|
|
res.header('Access-Control-Allow-Origin', '*')
|
|
|
|
res.send({
|
|
|
|
temp: temp.data
|
|
|
|
})
|
|
|
|
temp.destroy().catch(function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('remove temp failed: ' + err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorInternalError(res)
|
2017-04-11 17:39:41 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// post status
|
2020-04-12 14:45:34 -04:00
|
|
|
StatusRouter.post('/temp', urlencodedParser, function (req, res) {
|
|
|
|
const host = req.get('host')
|
2018-03-07 09:17:35 -05:00
|
|
|
if (config.allowOrigin.indexOf(host) === -1) {
|
2019-10-27 08:51:53 -04:00
|
|
|
errors.errorForbidden(res)
|
2017-04-11 17:39:41 -04:00
|
|
|
} else {
|
2020-04-12 14:45:34 -04:00
|
|
|
const data = req.body.data
|
2017-04-11 17:39:41 -04:00
|
|
|
if (!data) {
|
2019-10-27 08:51:53 -04:00
|
|
|
errors.errorForbidden(res)
|
2017-04-11 17:39:41 -04:00
|
|
|
} else {
|
2019-06-08 14:51:24 -04:00
|
|
|
logger.debug(`SERVER received temp from [${host}]: ${req.body.data}`)
|
2020-04-12 14:45:34 -04:00
|
|
|
Temp.create({
|
2017-04-11 17:39:41 -04:00
|
|
|
data: data
|
|
|
|
}).then(function (temp) {
|
|
|
|
if (temp) {
|
|
|
|
res.header('Access-Control-Allow-Origin', '*')
|
|
|
|
res.send({
|
|
|
|
status: 'ok',
|
|
|
|
id: temp.id
|
|
|
|
})
|
|
|
|
} else {
|
2019-10-27 08:51:53 -04:00
|
|
|
errors.errorInternalError(res)
|
2017-04-11 17:39:41 -04:00
|
|
|
}
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
2019-10-27 08:51:53 -04:00
|
|
|
return errors.errorInternalError(res)
|
2017-04-11 17:39:41 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2018-06-23 18:06:03 -04:00
|
|
|
|
2020-04-12 14:45:34 -04:00
|
|
|
StatusRouter.get('/config', function (req, res) {
|
|
|
|
const data = {
|
2018-06-23 18:06:03 -04:00
|
|
|
domain: config.domain,
|
|
|
|
urlpath: config.urlPath,
|
|
|
|
debug: config.debug,
|
2018-10-05 13:33:40 -04:00
|
|
|
version: config.fullversion,
|
2018-06-23 18:06:03 -04:00
|
|
|
DROPBOX_APP_KEY: config.dropbox.appKey,
|
2019-10-21 16:10:06 -04:00
|
|
|
allowedUploadMimeTypes: config.allowedUploadMimeTypes,
|
|
|
|
linkifyHeaderStyle: config.linkifyHeaderStyle
|
2018-06-23 18:06:03 -04:00
|
|
|
}
|
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private', // only cache by client
|
2018-07-03 14:36:40 -04:00
|
|
|
'X-Robots-Tag': 'noindex, nofollow', // prevent crawling
|
|
|
|
'Content-Type': 'application/javascript'
|
2018-06-23 18:06:03 -04:00
|
|
|
})
|
2018-09-10 16:35:38 -04:00
|
|
|
res.render('../js/lib/common/constant.ejs', data)
|
2018-06-23 18:06:03 -04:00
|
|
|
})
|
2020-04-12 14:45:34 -04:00
|
|
|
|
|
|
|
export { StatusRouter }
|