2017-03-27 06:53:34 +00:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const rt = require('../ResTool');
|
|
|
|
const config = require('../../config.json');
|
|
|
|
const fs = require('fs');
|
2017-03-27 08:27:35 +00:00
|
|
|
const mysql = require('../../libs/mysql_pool');
|
2017-03-27 06:53:34 +00:00
|
|
|
const tool = require('../../includes/apiTool');
|
|
|
|
const exec = require('child_process').exec;
|
|
|
|
const so = require('../../includes/storeObject');
|
|
|
|
const crypt = require('../../libs/crypto');
|
2017-05-04 06:36:29 +00:00
|
|
|
const path = require('path');
|
2017-03-27 06:53:34 +00:00
|
|
|
|
|
|
|
router
|
|
|
|
.get('/', (req, res, n) => {
|
|
|
|
res.send({ name: 'WebIO IPCam API' });
|
|
|
|
})
|
2017-05-03 03:16:01 +00:00
|
|
|
.post('*', async(req, res, n) => {
|
|
|
|
try {
|
2017-03-28 08:34:50 +00:00
|
|
|
res.db = await mysql.getConn();
|
2017-05-03 03:16:01 +00:00
|
|
|
} catch (e) {
|
2017-03-28 08:34:50 +00:00
|
|
|
console.log(`Get DB Connection ERROR ${e}`);
|
|
|
|
return n('ERR8100');
|
|
|
|
}
|
|
|
|
n();
|
|
|
|
})
|
2017-05-03 03:16:01 +00:00
|
|
|
.post('/getipcamlist', (req, res, n) => {
|
|
|
|
if (!config.permission.ipcam) return n('ERR9000');
|
2017-03-27 09:32:25 +00:00
|
|
|
|
2017-05-04 06:36:29 +00:00
|
|
|
let query = "select * from ??.?? where `delete` = 0 order by `ctime`";
|
2017-05-03 03:16:01 +00:00
|
|
|
let param = [config.db.db1, 'jcioccamset'];
|
2017-03-27 09:32:25 +00:00
|
|
|
res.db.query(query, param, (err, row) => {
|
2017-05-03 03:16:01 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8000');
|
2017-03-27 09:32:25 +00:00
|
|
|
|
|
|
|
res.api_res = {
|
|
|
|
record: tool.checkArray(row)
|
|
|
|
}
|
|
|
|
|
|
|
|
n();
|
|
|
|
});
|
2017-03-27 08:27:35 +00:00
|
|
|
})
|
2017-05-03 03:16:01 +00:00
|
|
|
.post('/addipcam', (req, res, n) => {
|
|
|
|
if (!config.permission.ipcam) return n('ERR9000');
|
|
|
|
if (!tool.checkPermission(req)) return n('ERR9000');
|
2017-03-27 08:27:35 +00:00
|
|
|
let arr = req.body;
|
2017-05-03 03:16:01 +00:00
|
|
|
if (!arr.data) return n('ERR0000');
|
|
|
|
if (!arr.data.ip) return n("ERR0010");
|
|
|
|
if (!arr.data.name) return n('ERR0026');
|
|
|
|
if (!arr.data.model) return n('ERR0067');
|
|
|
|
if (!arr.data.maxevents) return n('ERR0068');
|
|
|
|
if (!arr.data.maximg) return n('ERR0069');
|
|
|
|
if (!/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(arr.data.ip)) return n('ERR0025');
|
|
|
|
|
|
|
|
let u = '';
|
|
|
|
let obj = so.get(req.headers['x-auth-token'] || '');
|
|
|
|
if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account;
|
|
|
|
|
|
|
|
let active = arr.data.active == 1 ? 1 : 0;
|
2017-03-27 08:27:35 +00:00
|
|
|
|
2017-05-03 09:15:08 +00:00
|
|
|
!async function runSync() {
|
|
|
|
// data flag 0 is insert, 1 is update
|
|
|
|
let flag = 0;
|
|
|
|
let idx = 1;
|
|
|
|
// query db data
|
|
|
|
try {
|
|
|
|
let query = "select `uid` from ??.?? order by `uid` desc limit 1";
|
|
|
|
let param = [config.db.db1, 'jcioccamset'];
|
|
|
|
let id = await tool.promiseQuery(res, query, param);
|
2017-05-03 03:16:01 +00:00
|
|
|
|
2017-05-03 09:15:08 +00:00
|
|
|
if (id.data.length > 0 && id.data[0].uid) idx = id.data[0].uid + 1;
|
|
|
|
if (idx > config.ipcam_limit) {
|
|
|
|
query = "select `uid` from ??.?? where `delete` = 1 limit 1";
|
|
|
|
flag = 1;
|
|
|
|
let id = await tool.promiseQuery(res, query, param);
|
|
|
|
if (id.data.length > 0 && id.data[0].uid) idx = id.data[0].uid;
|
|
|
|
else return n('ERR0070');
|
|
|
|
}
|
2017-05-03 03:16:01 +00:00
|
|
|
|
2017-05-03 09:15:08 +00:00
|
|
|
query = "select count(*) as c from ??.?? where `ip` = ?";
|
|
|
|
param = [config.db.db1, 'jcioccamset', arr.data.ip];
|
|
|
|
let count = await tool.promiseQuery(res, query, param);
|
|
|
|
if (count.data.length == 0) return n('ERR8000');
|
|
|
|
if (count.data[0].c >= 1) return n('ERR0027');
|
|
|
|
} catch (e) {
|
|
|
|
return rt.err(res, e, n, 'ERR8000');
|
|
|
|
}
|
|
|
|
if (flag == 1) {
|
|
|
|
try {
|
|
|
|
let query = "update ??.?? set \
|
|
|
|
`ip` = ?, \
|
|
|
|
`name` = ?, \
|
|
|
|
`model` = ?, \
|
|
|
|
`maxevents` = ?, \
|
|
|
|
`maximg` = ?, \
|
|
|
|
`active` = ?, \
|
|
|
|
`delete` = 0, \
|
2017-05-04 06:36:29 +00:00
|
|
|
`cuser` = ?, \
|
|
|
|
`ctime` = unix_timestamp(), \
|
2017-05-03 09:15:08 +00:00
|
|
|
`muser` = ?, \
|
|
|
|
`mtime` = unix_timestamp() \
|
|
|
|
where \
|
|
|
|
`uid` = ?";
|
2017-05-04 06:36:29 +00:00
|
|
|
let param = [config.db.db1, 'jcioccamset', arr.data.ip, arr.data.name, arr.data.model, arr.data.maxevents, arr.data.maximg, active, u, u, idx];
|
2017-05-03 09:15:08 +00:00
|
|
|
await tool.promiseQuery(res, query, param);
|
|
|
|
} catch (e) {
|
|
|
|
return rt.err(res, e, n, 'ERR8002');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
let query = "insert into ??.?? (`uid`, `ip`, `name`, `model`, `maxevents`, `maximg`, `active`, `cuser`, `muser`, `ctime`, `mtime` ) \
|
|
|
|
values (?, ?, ?, ?, ?, ?, ?, ?, ?, unix_timestamp(), unix_timestamp())";
|
|
|
|
let param = [config.db.db1, 'jcioccamset', idx, arr.data.ip, arr.data.name, arr.data.model, arr.data.maxevents, arr.data.maximg, active, u, u];
|
|
|
|
await tool.promiseQuery(res, query, param);
|
|
|
|
} catch (e) {
|
|
|
|
return rt.err(res, e, n, 'ERR8001');
|
|
|
|
}
|
|
|
|
}
|
2017-03-27 10:00:21 +00:00
|
|
|
|
2017-05-03 09:15:08 +00:00
|
|
|
res.api_res = {
|
|
|
|
record: []
|
|
|
|
}
|
|
|
|
return n();
|
|
|
|
}()
|
2017-05-03 03:16:01 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
.post('/editipcam', (req, res, n) => {
|
|
|
|
if (!config.permission.ipcam) return n('ERR9000');
|
|
|
|
if (!tool.checkPermission(req)) return n('ERR9000');
|
|
|
|
let arr = req.body;
|
|
|
|
if (!arr.data) return n('ERR0000');
|
|
|
|
if (!arr.data.id) return n('ERR0028');
|
|
|
|
if (!arr.data.ip) return n("ERR0010");
|
|
|
|
if (!arr.data.name) return n('ERR0026');
|
|
|
|
if (!arr.data.model) return n('ERR0067');
|
|
|
|
if (!arr.data.maxevents) return n('ERR0068');
|
|
|
|
if (!arr.data.maximg) return n('ERR0069');
|
|
|
|
if (!/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(arr.data.ip)) return n('ERR0025');
|
|
|
|
|
|
|
|
let u = '';
|
|
|
|
let obj = so.get(req.headers['x-auth-token'] || '');
|
|
|
|
if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account;
|
|
|
|
|
|
|
|
let active = arr.data.active == 1 ? 1 : 0;
|
|
|
|
|
|
|
|
let query = "select count(*) as c from ??.?? where `ip` = ? and `uid` != ?";
|
|
|
|
let param = [config.db.db1, 'jcioccamset', arr.data.ip, arr.data.id];
|
|
|
|
|
|
|
|
res.db.query(query, param, (err, row) => {
|
|
|
|
if (err || row.length == 0) return rt.err(res, err, n, 'ERR8000');
|
|
|
|
|
|
|
|
if (row[0].c >= 1) return n('ERR0027');
|
|
|
|
|
|
|
|
let query = "update ??.?? set \
|
|
|
|
`ip` = ?, \
|
|
|
|
`name` = ?, \
|
|
|
|
`model` = ?, \
|
|
|
|
`maxevents` = ?, \
|
|
|
|
`maximg` = ?, \
|
|
|
|
`active` = ?, \
|
|
|
|
`muser` = ?, \
|
|
|
|
`mtime` = unix_timestamp() \
|
|
|
|
where \
|
|
|
|
`uid` = ?";
|
|
|
|
let param = [config.db.db1, 'jcioccamset', arr.data.ip, arr.data.name, arr.data.model, arr.data.maxevents, arr.data.maximg, active, u, arr.data.id];
|
|
|
|
res.db.query(query, param, (err, row) => {
|
|
|
|
if (err) return rt.err(res, err, n, 'ERR8002');
|
|
|
|
|
|
|
|
res.api_res = {
|
|
|
|
record: []
|
|
|
|
}
|
|
|
|
n();
|
|
|
|
})
|
2017-03-27 10:00:21 +00:00
|
|
|
});
|
|
|
|
})
|
2017-05-03 03:16:01 +00:00
|
|
|
.post('/delipcam', (req, res, n) => {
|
|
|
|
if (!config.permission.ipcam) return n('ERR9000');
|
|
|
|
if (!tool.checkPermission(req)) return n('ERR9000');
|
2017-03-27 10:00:21 +00:00
|
|
|
let arr = req.body;
|
2017-05-03 03:16:01 +00:00
|
|
|
if (!arr.data) return n('ERR0000');
|
|
|
|
if (!arr.data.id) return n('ERR0028');
|
|
|
|
|
2017-05-04 06:36:29 +00:00
|
|
|
let u = '';
|
|
|
|
let obj = so.get(req.headers['x-auth-token'] || '');
|
|
|
|
if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account;
|
|
|
|
|
2017-05-03 03:16:01 +00:00
|
|
|
res.db.query(`use ${config.db.db1}`);
|
2017-05-04 06:36:29 +00:00
|
|
|
let query = "update ??.?? set \
|
|
|
|
`delete` = 1, \
|
|
|
|
`muser` = ?, \
|
|
|
|
`mtime` = unix_timestamp() \
|
2017-05-03 03:16:01 +00:00
|
|
|
where `uid` = ?";
|
2017-05-04 06:36:29 +00:00
|
|
|
let param = [config.db.db1, 'jcioccamset', u, arr.data.id];
|
2017-03-27 10:00:21 +00:00
|
|
|
|
|
|
|
res.db.query(query, param, (err, row) => {
|
2017-05-03 03:16:01 +00:00
|
|
|
if (err) return rt.err(res, err, n, 'ERR8003');
|
|
|
|
|
2017-03-27 10:00:21 +00:00
|
|
|
res.api_res = {
|
|
|
|
record: []
|
2017-05-03 03:16:01 +00:00
|
|
|
}
|
2017-03-27 10:00:21 +00:00
|
|
|
n();
|
|
|
|
})
|
|
|
|
})
|
2017-05-03 03:16:01 +00:00
|
|
|
.post('/swipcamactive', (req, res, n) => {
|
|
|
|
if (!config.permission.ipcam) return n('ERR9000');
|
|
|
|
if (!tool.checkPermission(req)) return n('ERR9000');
|
2017-03-27 10:00:21 +00:00
|
|
|
let arr = req.body;
|
2017-05-03 03:16:01 +00:00
|
|
|
if (!arr.data) return n('ERR0000');
|
|
|
|
if (!arr.data.id) return n('ERR0028');
|
|
|
|
|
|
|
|
let query = "update ??.?? set \
|
|
|
|
`active` = case when `active` = 1 then 0 else 1 end \
|
|
|
|
where \
|
|
|
|
`uid` = ?";
|
|
|
|
let param = [config.db.db1, 'jcioccamset', arr.data.id];
|
|
|
|
|
|
|
|
res.db.query(query, param, (err, row) => {
|
|
|
|
if (err) return rt.err(res, err, n, 'ERR8002');
|
|
|
|
|
2017-03-27 10:00:21 +00:00
|
|
|
res.api_res = {
|
|
|
|
record: []
|
|
|
|
}
|
|
|
|
n();
|
|
|
|
})
|
2017-03-27 08:27:35 +00:00
|
|
|
})
|
2017-05-04 06:36:29 +00:00
|
|
|
.post('/getevents', async(req, res, n) => {
|
|
|
|
if (!config.permission.ipcam) return n('ERR9000');
|
|
|
|
if (!tool.checkPermission(req)) return n('ERR9000');
|
|
|
|
let arr = req.body;
|
|
|
|
if (!arr.data) return n('ERR0000');
|
|
|
|
if (!arr.data.id) return n('ERR0028');
|
|
|
|
|
|
|
|
try {
|
|
|
|
let query = "select * from ??.?? where `uid` = ? and `delete` = 0";
|
|
|
|
let param = [config.db.db1, 'jcioccamset', arr.data.id];
|
|
|
|
let cam = await tool.promiseQuery(res, query, param);
|
|
|
|
if (cam.length == 0) return n('ERR0071');
|
|
|
|
} catch (e) {
|
|
|
|
return rt.err(res, e, n, 'ERR8000');
|
|
|
|
}
|
|
|
|
let rp = config.cmdpath.ipcamsave;
|
|
|
|
let dirs = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
let dirls = await new Promise((resolve, reject) => {
|
|
|
|
fs.readdir(rp, (err, list) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
return resolve(list);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
for (let i of dirls) {
|
|
|
|
let tmp = i.split('-');
|
|
|
|
if (tmp.length != 2) continue;
|
|
|
|
if (tmp[0] == arr.data.id) dirs.push({
|
|
|
|
name: i,
|
|
|
|
files: {
|
|
|
|
img: [],
|
|
|
|
video: []
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i in dirs) {
|
|
|
|
let p = path.resolve(rp, dirs[i].name);
|
|
|
|
let idx = i;
|
|
|
|
let files = await new Promise((resolve, reject) => {
|
|
|
|
fs.readdir(p, (err, list) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
return resolve(list);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
for (let j of files) {
|
|
|
|
if (/^img/.test(j)) {
|
|
|
|
dirs[idx].files.img.push(j);
|
|
|
|
} else if (/^vid/.test(j)) {
|
|
|
|
dirs[idx].files.video.push(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
return rt.err(res, e, n, 'ERR0072');
|
|
|
|
}
|
|
|
|
|
|
|
|
res.api_res = {
|
|
|
|
record: tool.checkArray(dirs)
|
|
|
|
}
|
|
|
|
return n();
|
|
|
|
})
|
2017-05-05 09:24:45 +00:00
|
|
|
.post('/delevent', async(req, res, n) => {
|
|
|
|
if (!config.permission.ipcam) return n('ERR9000');
|
|
|
|
if (!tool.checkPermission(req)) return n('ERR9000');
|
|
|
|
let arr = req.body;
|
|
|
|
if (!arr.data) return n('ERR0000');
|
|
|
|
if (!arr.data.dir) return n('ERR0073');
|
|
|
|
|
|
|
|
let rp = config.cmdpath.ipcamsave;
|
|
|
|
let dp = path.resolve(rp, arr.data.dir);
|
|
|
|
try {
|
|
|
|
let stat = await new Promise((resolve, reject) => {
|
|
|
|
fs.stat(dp, (err, stats) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
return resolve(stats);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
if (dp.split(' ')[0] == '/') return n('ERR0073');
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
exec(`rm -rf ${dp}`, (err, sout, serr) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
return resolve(null);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
return rt.err(res, e, n, 'ERR0072');
|
|
|
|
}
|
|
|
|
|
|
|
|
res.api_res = {
|
|
|
|
record: []
|
|
|
|
}
|
|
|
|
return n();
|
|
|
|
})
|
2017-03-27 06:53:34 +00:00
|
|
|
.all('*', rt.send);
|
|
|
|
|
|
|
|
module.exports = router;
|