2017-04-11 21:39:41 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Router = require('express').Router
|
|
|
|
|
|
|
|
const response = require('../response')
|
|
|
|
const realtime = require('../realtime')
|
|
|
|
const config = require('../config')
|
|
|
|
const models = require('../models')
|
|
|
|
const logger = require('../logger')
|
|
|
|
|
|
|
|
const {urlencodedParser} = require('./utils')
|
|
|
|
|
|
|
|
const statusRouter = module.exports = Router()
|
|
|
|
|
|
|
|
// get status
|
|
|
|
statusRouter.get('/status', function (req, res, next) {
|
|
|
|
realtime.getStatus(function (data) {
|
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private', // only cache by client
|
2018-06-24 11:59:18 +00:00
|
|
|
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
2017-04-11 21:39:41 +00:00
|
|
|
})
|
|
|
|
res.send(data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
// get status
|
|
|
|
statusRouter.get('/temp', function (req, res) {
|
|
|
|
var host = req.get('host')
|
2018-03-07 14:17:35 +00:00
|
|
|
if (config.allowOrigin.indexOf(host) === -1) {
|
2017-04-11 21:39:41 +00:00
|
|
|
response.errorForbidden(res)
|
|
|
|
} else {
|
|
|
|
var tempid = req.query.tempid
|
|
|
|
if (!tempid) {
|
|
|
|
response.errorForbidden(res)
|
|
|
|
} else {
|
|
|
|
models.Temp.findOne({
|
|
|
|
where: {
|
|
|
|
id: tempid
|
|
|
|
}
|
|
|
|
}).then(function (temp) {
|
|
|
|
if (!temp) {
|
|
|
|
response.errorNotFound(res)
|
|
|
|
} 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)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// post status
|
|
|
|
statusRouter.post('/temp', urlencodedParser, function (req, res) {
|
|
|
|
var host = req.get('host')
|
2018-03-07 14:17:35 +00:00
|
|
|
if (config.allowOrigin.indexOf(host) === -1) {
|
2017-04-11 21:39:41 +00:00
|
|
|
response.errorForbidden(res)
|
|
|
|
} else {
|
|
|
|
var data = req.body.data
|
|
|
|
if (!data) {
|
|
|
|
response.errorForbidden(res)
|
|
|
|
} else {
|
|
|
|
if (config.debug) {
|
|
|
|
logger.info('SERVER received temp from [' + host + ']: ' + req.body.data)
|
|
|
|
}
|
|
|
|
models.Temp.create({
|
|
|
|
data: data
|
|
|
|
}).then(function (temp) {
|
|
|
|
if (temp) {
|
|
|
|
res.header('Access-Control-Allow-Origin', '*')
|
|
|
|
res.send({
|
|
|
|
status: 'ok',
|
|
|
|
id: temp.id
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
response.errorInternalError(res)
|
|
|
|
}
|
|
|
|
}).catch(function (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return response.errorInternalError(res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2018-06-23 22:06:03 +00:00
|
|
|
|
|
|
|
statusRouter.get('/config', function (req, res) {
|
|
|
|
var data = {
|
|
|
|
domain: config.domain,
|
|
|
|
urlpath: config.urlPath,
|
|
|
|
debug: config.debug,
|
|
|
|
version: config.version,
|
|
|
|
DROPBOX_APP_KEY: config.dropbox.appKey,
|
|
|
|
allowedUploadMimeTypes: config.allowedUploadMimeTypes
|
|
|
|
}
|
|
|
|
res.set({
|
|
|
|
'Cache-Control': 'private', // only cache by client
|
2018-06-24 11:59:18 +00:00
|
|
|
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
|
2018-06-23 22:06:03 +00:00
|
|
|
})
|
|
|
|
res.render(config.constantsPath, data)
|
|
|
|
})
|