|
|
@ -1,11 +1,16 @@ |
|
|
|
var utils = require('../utils') |
|
|
|
var utils = require('../utils') |
|
|
|
var isbinaryfile = require('isbinaryfile') |
|
|
|
var isbinaryfile = require('isbinaryfile') |
|
|
|
var fs = require('fs-extra') |
|
|
|
var fs = require('fs-extra') |
|
|
|
var watch = require('watch') |
|
|
|
var chokidar = require('chokidar') |
|
|
|
|
|
|
|
|
|
|
|
module.exports = { |
|
|
|
module.exports = { |
|
|
|
monitors: [], |
|
|
|
|
|
|
|
trackDownStreamUpdate: {}, |
|
|
|
trackDownStreamUpdate: {}, |
|
|
|
|
|
|
|
websocket: null, |
|
|
|
|
|
|
|
alreadyNotified: {}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setWebSocket: function (websocket) { |
|
|
|
|
|
|
|
this.websocket = websocket |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
sharedFolder: function (sharedFolder) { |
|
|
|
sharedFolder: function (sharedFolder) { |
|
|
|
this.sharedFolder = sharedFolder |
|
|
|
this.sharedFolder = sharedFolder |
|
|
@ -22,6 +27,10 @@ module.exports = { |
|
|
|
resolveDirectory: function (args, cb) { |
|
|
|
resolveDirectory: function (args, cb) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
var path = utils.absolutePath(args.path, this.sharedFolder) |
|
|
|
var path = utils.absolutePath(args.path, this.sharedFolder) |
|
|
|
|
|
|
|
if (this.websocket && !this.alreadyNotified[path]) { |
|
|
|
|
|
|
|
this.alreadyNotified[path] = 1 |
|
|
|
|
|
|
|
this.setupNotifications(path) |
|
|
|
|
|
|
|
} |
|
|
|
cb(null, utils.resolveDirectory(path, this.sharedFolder)) |
|
|
|
cb(null, utils.resolveDirectory(path, this.sharedFolder)) |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
cb(e.message) |
|
|
|
cb(e.message) |
|
|
@ -87,30 +96,32 @@ module.exports = { |
|
|
|
}) |
|
|
|
}) |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
setupNotifications: function (websocket, path) { |
|
|
|
setupNotifications: function (path) { |
|
|
|
if (!isRealPath(path)) return |
|
|
|
if (!isRealPath(path)) return |
|
|
|
watch.createMonitor(path, (monitor) => { |
|
|
|
var watcher = chokidar.watch(path, {depth: 0, ignorePermissionErrors: true}) |
|
|
|
this.monitors.push(monitor) |
|
|
|
console.log('setup ntifications for ' + path) |
|
|
|
monitor.on('created', (f, stat) => { |
|
|
|
watcher.on('add', (f, stat) => { |
|
|
|
isbinaryfile(f, (error, isBinary) => { |
|
|
|
isbinaryfile(f, (error, isBinary) => { |
|
|
|
if (error) console.log(error) |
|
|
|
if (error) console.log(error) |
|
|
|
if (stat.isDirectory()) { |
|
|
|
if (this.websocket.connection) this.websocket.send(message('created', { path: utils.relativePath(f, this.sharedFolder), isReadOnly: isBinary, isFolder: false })) |
|
|
|
this.setupNotifications(websocket, f) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (websocket.connection) websocket.send(message('created', { path: utils.relativePath(f, this.sharedFolder), isReadOnly: isBinary, isFolder: stat.isDirectory() })) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
monitor.on('changed', (f, curr, prev) => { |
|
|
|
|
|
|
|
if (this.trackDownStreamUpdate[f]) { |
|
|
|
|
|
|
|
delete this.trackDownStreamUpdate[f] |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (websocket.connection) websocket.send(message('changed', utils.relativePath(f, this.sharedFolder))) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
monitor.on('removed', (f, stat) => { |
|
|
|
|
|
|
|
if (websocket.connection) websocket.send(message('removed', { path: utils.relativePath(f, this.sharedFolder), isFolder: stat.isDirectory() })) |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
watcher.on('addDir', (f, stat) => { |
|
|
|
|
|
|
|
if (this.websocket.connection) this.websocket.send(message('created', { path: utils.relativePath(f, this.sharedFolder), isReadOnly: false, isFolder: true })) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
watcher.on('change', (f, curr, prev) => { |
|
|
|
|
|
|
|
if (this.trackDownStreamUpdate[f]) { |
|
|
|
|
|
|
|
delete this.trackDownStreamUpdate[f] |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (this.websocket.connection) this.websocket.send(message('changed', utils.relativePath(f, this.sharedFolder))) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
watcher.on('unlink', (f) => { |
|
|
|
|
|
|
|
if (this.websocket.connection) this.websocket.send(message('removed', { path: utils.relativePath(f, this.sharedFolder), isFolder: false })) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
watcher.on('unlinkDir', (f) => { |
|
|
|
|
|
|
|
if (this.websocket.connection) this.websocket.send(message('removed', { path: utils.relativePath(f, this.sharedFolder), isFolder: true })) |
|
|
|
|
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|