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 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>The filename can be overridden by the <code>filename</code> property
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>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>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
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>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 type="text/javascript">

View File

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