2019-02-23 17:50:03 +00:00
|
|
|
var fs = require('fs')
|
2019-02-23 06:59:06 +00:00
|
|
|
module.exports = function(s,config,lang,app,io){
|
|
|
|
if(config.dropInEventServer === true){
|
|
|
|
if(config.dropInEventDeleteFileAfterTrigger === undefined)config.dropInEventDeleteFileAfterTrigger = true
|
|
|
|
var beforeMonitorsLoadedOnStartup = function(){
|
|
|
|
if(!config.dropInEventsDir){
|
|
|
|
config.dropInEventsDir = s.dir.streams + 'dropInEvents/'
|
|
|
|
}
|
|
|
|
s.dir.dropInEvents = s.checkCorrectPathEnding(config.dropInEventsDir)
|
|
|
|
//dropInEvents dir
|
|
|
|
if(!fs.existsSync(s.dir.dropInEvents)){
|
|
|
|
fs.mkdirSync(s.dir.dropInEvents)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var onMonitorInit = function(monitorConfig){
|
|
|
|
var ke = monitorConfig.ke
|
|
|
|
var mid = monitorConfig.mid
|
|
|
|
var groupEventDropDir = s.dir.dropInEvents + ke
|
|
|
|
|
|
|
|
if(!fs.existsSync(groupEventDropDir)){
|
|
|
|
fs.mkdirSync(groupEventDropDir)
|
|
|
|
}
|
|
|
|
var monitorEventDropDir = groupEventDropDir + '/' + mid + '/'
|
|
|
|
if(!fs.existsSync(monitorEventDropDir)){
|
|
|
|
fs.mkdirSync(monitorEventDropDir)
|
|
|
|
}
|
|
|
|
if(s.group[monitorConfig.ke].mon[monitorConfig.mid].dropInEventWatcher){
|
|
|
|
s.group[monitorConfig.ke].mon[monitorConfig.mid].dropInEventWatcher.close()
|
|
|
|
delete(s.group[monitorConfig.ke].mon[monitorConfig.mid].dropInEventWatcher)
|
|
|
|
}
|
|
|
|
var fileQueue = {}
|
|
|
|
s.group[monitorConfig.ke].mon[monitorConfig.mid].dropInEventFileQueue = fileQueue
|
|
|
|
var eventTrigger = function(eventType,filename){
|
|
|
|
var filePath = monitorEventDropDir + filename
|
|
|
|
if(filename.indexOf('.jpg') > -1){
|
|
|
|
var snapPath = s.dir.streams + ke + '/' + mid + '/s.jpg'
|
|
|
|
fs.unlink(snapPath,function(err){
|
|
|
|
fs.createReadStream(filePath).pipe(fs.createWriteStream(snapPath))
|
|
|
|
s.triggerEvent({
|
|
|
|
id: mid,
|
|
|
|
ke: ke,
|
|
|
|
details: {
|
|
|
|
confidence: 100,
|
|
|
|
name: filename,
|
|
|
|
plug: "dropInEvent",
|
|
|
|
reason: "dropInEvent"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}else{
|
|
|
|
s.triggerEvent({
|
|
|
|
id: mid,
|
|
|
|
ke: ke,
|
|
|
|
details: {
|
|
|
|
confidence: 100,
|
|
|
|
name: filename,
|
|
|
|
plug: "dropInEvent",
|
|
|
|
reason: "dropInEvent"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if(config.dropInEventDeleteFileAfterTrigger){
|
|
|
|
setTimeout(function(){
|
2019-02-23 07:04:52 +00:00
|
|
|
fs.unlink(filePath,function(err){
|
|
|
|
|
|
|
|
})
|
2019-02-23 06:59:06 +00:00
|
|
|
},1000 * 60 * 5)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var directoryWatch = fs.watch(monitorEventDropDir,function(eventType,filename){
|
2019-02-24 06:01:53 +00:00
|
|
|
if(fs.existsSync(monitorEventDropDir + filename)){
|
|
|
|
clearTimeout(fileQueue[filename])
|
|
|
|
fileQueue[filename] = setTimeout(function(){
|
|
|
|
eventTrigger(eventType,filename)
|
|
|
|
},3000)
|
|
|
|
}
|
2019-02-23 06:59:06 +00:00
|
|
|
})
|
|
|
|
s.group[monitorConfig.ke].mon[monitorConfig.mid].dropInEventWatcher = directoryWatch
|
|
|
|
}
|
2019-02-23 17:50:03 +00:00
|
|
|
if(config.ftpServer === true){
|
2019-02-24 06:01:53 +00:00
|
|
|
if(!config.ftpServerPort)config.ftpServerPort = 21
|
|
|
|
if(!config.ftpServerUrl)config.ftpServerUrl = `ftp://0.0.0.0:${config.ftpServerPort}`
|
|
|
|
config.ftpServerUrl = config.ftpServerUrl.replace('{{PORT}}',config.ftpServerPort)
|
2019-02-23 17:50:03 +00:00
|
|
|
const FtpSrv = require('ftp-srv');
|
2019-02-24 06:01:53 +00:00
|
|
|
const ftpServer = new FtpSrv({
|
|
|
|
url: config.ftpServerUrl,
|
|
|
|
// log:{trace:function(){},error:function(){},child:function(){},info:function(){},warn:function(){}
|
|
|
|
})
|
2019-02-23 17:50:03 +00:00
|
|
|
|
|
|
|
ftpServer.on('login', (data, resolve, reject) => {
|
|
|
|
var username = data.username
|
|
|
|
var password = data.password
|
|
|
|
s.sqlQuery('SELECT * FROM Users WHERE mail=? AND (pass=? OR pass=?)',[username,password,s.createHash(password)],function(err,r){
|
|
|
|
if(r && r[0]){
|
|
|
|
var user = r[0]
|
|
|
|
resolve({root: s.dir.dropInEvents + user.ke})
|
|
|
|
}else{
|
2019-02-24 06:01:53 +00:00
|
|
|
// reject(new Error('Failed Authorization'))
|
2019-02-23 17:50:03 +00:00
|
|
|
}
|
|
|
|
})
|
2019-02-24 06:01:53 +00:00
|
|
|
})
|
2019-02-23 17:50:03 +00:00
|
|
|
|
|
|
|
ftpServer.listen().then(() => {
|
2019-02-24 06:01:53 +00:00
|
|
|
s.systemLog(`FTP Server running on port ${config.ftpServerPort}...`)
|
|
|
|
}).catch(function(err){
|
|
|
|
console.log(err)
|
2019-02-23 17:50:03 +00:00
|
|
|
})
|
|
|
|
}
|
2019-02-23 06:59:06 +00:00
|
|
|
//add extensions
|
|
|
|
s.beforeMonitorsLoadedOnStartup(beforeMonitorsLoadedOnStartup)
|
|
|
|
s.onMonitorInit(onMonitorInit)
|
|
|
|
}
|
|
|
|
}
|