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

Add .delete property to File output node

Fix to close #162
This commit is contained in:
Dave C-J 2014-02-20 21:00:54 +00:00
parent 0bc4a3bbb1
commit 58774c366d
2 changed files with 34 additions and 26 deletions

View File

@ -36,11 +36,12 @@
</script> </script>
<script type="text/x-red" data-help-name="file"> <script type="text/x-red" data-help-name="file">
<p>Writes <b>msg.payload</b> to the file specified, e.g. to create a log.</p> <p>Writes <b>msg.payload</b> to the file specified, e.g. to create a log.</p>
<p>The filename can be overridden by the <code>filename</code> property <p>The filename can be overridden by the <code>.filename</code> property
of the incoming message.</p> of the incoming message.</p>
<p>A newline is added to every message. But this can be turned off if required, for example, to allow binary files to be written.</p> <p>A newline is added to every message. But this can be turned off if required, for example, to allow binary files to be written.</p>
<p>The default behaviour is to append to the file. This can be changed to overwrite the file each time, for example if you want to output a "static" web page or report.</p> <p>The default behaviour is to append to the file. This can be changed to overwrite the file each time, for example if you want to output a "static" web page or report.</p>
<p>If a <code>.delete</code> property exists then the file will be deleted instead.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -18,36 +18,43 @@ var RED = require(process.env.NODE_RED_HOME+"/red/red");
var fs = require("fs"); var fs = require("fs");
function FileNode(n) { function FileNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.filename = n.filename; this.filename = n.filename;
this.appendNewline = n.appendNewline; this.appendNewline = n.appendNewline;
this.overwriteFile = n.overwriteFile; this.overwriteFile = n.overwriteFile;
var node = this; var node = this;
this.on("input",function(msg) { this.on("input",function(msg) {
var filename = msg.filename || this.filename; var filename = msg.filename || this.filename;
if (filename == "") { if (filename == "") {
node.warn('No filename specified'); node.warn('No filename specified');
} else { } else {
var data = msg.payload; var data = msg.payload;
if (this.appendNewline) { if (this.appendNewline) {
data += "\n"; data += "\n";
} }
if (this.overwriteFile) { if (msg.hasOwnProperty('delete')) {
fs.writeFile(filename, data, function (err) { fs.unlink(filename, function (err) {
if (err) node.warn('Failed to write to file : '+err); if (err) node.warn('Failed to delete file : '+err);
//console.log('Message written to file',this.filename); //console.log('Deleted file",filename);
}); });
} }
else { else {
fs.appendFile(filename, data, function (err) { if (this.overwriteFile) {
if (err) node.warn('Failed to append to file : '+err); fs.writeFile(filename, data, function (err) {
//console.log('Message appended to file',this.filename); if (err) node.warn('Failed to write to file : '+err);
}); //console.log('Message written to file',filename);
});
}
else {
fs.appendFile(filename, data, function (err) {
if (err) node.warn('Failed to append to file : '+err);
//console.log('Message appended to file',filename);
});
}
} }
} }
}); });
} }
RED.nodes.registerType("file",FileNode); RED.nodes.registerType("file",FileNode);