1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add type and size reporting to the file watch node.

This commit is contained in:
dceejay 2015-03-16 20:21:05 +00:00
parent 8fea443e71
commit cd5eac2cbb
2 changed files with 16 additions and 4 deletions

View File

@ -33,7 +33,9 @@
<p>On Windows you must use double back-slashes \\ in any directory names.</p> <p>On Windows you must use double back-slashes \\ in any directory names.</p>
<p>The full filename of the file that actually changed is put into <b>msg.payload</b>, <p>The full filename of the file that actually changed is put into <b>msg.payload</b>,
while a stringified version of the watch list is returned in <b>msg.topic</b>.</p> while a stringified version of the watch list is returned in <b>msg.topic</b>.</p>
<p><b>msg.file</b> contains just the short filename of the file that changed.</p> <p><b>msg.file</b> contains just the short filename of the file that changed.
<b>msg.type</b> has the type of thing changed, usually <i>file</i> or <i>directory</i>,
while <b>msg.size</b> holds the file size in bytes.</p>
<p>Of course in Linux, <i>everything</i> is a file and thus can be watched...</p> <p>Of course in Linux, <i>everything</i> is a file and thus can be watched...</p>
<p><b>Note: </b>The directory or file must exist in order to be watched. If the file <p><b>Note: </b>The directory or file must exist in order to be watched. If the file
or directory gets deleted it may no longer be monitored even if it gets re-created.</p> or directory gets deleted it may no longer be monitored even if it gets re-created.</p>

View File

@ -24,7 +24,7 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.files = n.files.split(","); this.files = n.files.split(",");
for (var f =0; f < this.files.length; f++) { for (var f=0; f < this.files.length; f++) {
this.files[f] = this.files[f].trim(); this.files[f] = this.files[f].trim();
} }
this.p = (this.files.length == 1) ? this.files[0] : JSON.stringify(this.files); this.p = (this.files.length == 1) ? this.files[0] : JSON.stringify(this.files);
@ -32,15 +32,25 @@ module.exports = function(RED) {
var notifications = new Notify(node.files); var notifications = new Notify(node.files);
notifications.on('change', function (file, event, path) { notifications.on('change', function (file, event, path) {
var stat;
try { try {
if (fs.statSync(path).isDirectory()) { path = path + sep + file; } if (fs.statSync(path).isDirectory()) { path = path + sep + file; }
stat = fs.statSync(path);
} catch(e) { } } catch(e) { }
var msg = { payload: path, topic: node.p, file: file }; var type = "other";
if (stat.isFile()) { type = "file"; }
if (stat.isDirectory()) { type = "directory"; }
if (stat.isBlockDevice()) { type = "blockdevice"; }
if (stat.isCharacterDevice()) { type = "characterdevice"; }
if (stat.isSocket()) { type = "socket"; }
if (stat.isFIFO()) { type = "fifo"; }
var msg = { payload:path, topic:node.p, file:file, type:type, size:stat.size };
node.send(msg); node.send(msg);
}); });
notifications.on('error', function (error, path) { notifications.on('error', function (error, path) {
node.warn(error); var msg = { payload:path };
node.error(error,msg);
}); });
this.close = function() { this.close = function() {