const so = require('./storeObject'); const config = require('../config'); const fs = require('fs'); const checkPermission = (req) => { let id = req.headers['x-auth-token']; if (id) { let obj = so.get(id); if (config.uni_token.length > 0 && id == config.uni_token) return true; if (obj != null) { if ('user' in obj && obj.user.write_privilege == '1') return true; } } return false; } const checkArray = (obj) => { if (Array.isArray(obj)) return obj; return []; } const getLeoneRT = (cb) => { if (!cb || typeof cb != 'function') return; fs.exists(config.cmdpath.leonert, exists => { if (!exists) return cb([]); fs.readFile(config.cmdpath.leonert, (err, data) => { if (err) return cb([]); let str = data.toString(); let tmp = str.split(/\n/); let rt = []; for (var i in tmp) { if (!tmp[i].trim()) continue; let arr = tmp[i].split(' '); if (arr.length != 5) continue; let [ip, ts, hs, mode, mtime] = arr; rt.push({ ip, ts, hs, mode, mtime }); } return cb(rt); }); }); } const promiseQuery = (res, query, param, key = '') => { return new Promise((resolve, reject) => { res.db.query(query, param, (err, row) => { if (err) return reject(err); resolve({ data: row, key }); }); }); } const getMode = (req) => { let lngs = req.headers['accept-language'].split(','); let lng = null; if (lngs.length > 0) { lng = lngs[0].substring(0, 2); } else { lng = 'zh'; } if (!fs.existsSync(`../public/locales/${lng}.json`)) lng = 'zh'; let json = require(`../public/locales/${lng}.json`); return json[lng].translation.leone_stats; } const getCmd = (req) => { let lngs = req.headers['accept-language'].split(','); let lng = null; if (lngs.length > 0) { lng = lngs[0].substring(0, 2); } else { lng = 'zh'; } if (!fs.existsSync(`../public/locales/${lng}.json`)) lng = 'zh'; let json = require(`../public/locales/${lng}.json`); return json[lng].translation.action_list; } module.exports = { checkPermission, checkArray, getLeoneRT, promiseQuery, getCmd, getMode }