mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Add delete option to File node
to replace msg.delete option - now deprecated but not removed. Addresses some of the confusion for Issue #399
This commit is contained in:
parent
8b7e367416
commit
5da45b404c
@ -20,14 +20,17 @@
|
|||||||
<input type="text" id="node-input-filename" placeholder="Filename">
|
<input type="text" id="node-input-filename" placeholder="Filename">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
|
<label for="node-input-overwriteFile"><i class="fa fa-random"></i> Action</label>
|
||||||
|
<select type="text" id="node-input-overwriteFile" style="display: inline-block; width: 250px; vertical-align: top;">
|
||||||
|
<option value="false">append to file</option>
|
||||||
|
<option value="true">overwrite file</option>
|
||||||
|
<option value="delete">delete file</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="form-row" id="node-appline">
|
||||||
<label> </label>
|
<label> </label>
|
||||||
<input type="checkbox" id="node-input-appendNewline" placeholder="Name" style="display: inline-block; width: auto; vertical-align: top;">
|
<input type="checkbox" id="node-input-appendNewline" placeholder="Name" style="display: inline-block; width: auto; vertical-align: top;">
|
||||||
<label for="node-input-appendNewline" style="width: 70%;">Append newline ?</label>
|
<label for="node-input-appendNewline" style="width: 70%;">Add newline (\n) to each payload ?</label>
|
||||||
</div>
|
|
||||||
<div class="form-row">
|
|
||||||
<label> </label>
|
|
||||||
<input type="checkbox" id="node-input-overwriteFile" placeholder="Name" style="display: inline-block; width: auto; vertical-align: top;">
|
|
||||||
<label for="node-input-overwriteFile" style="width: 70%;">Overwrite complete file ?</label>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||||
@ -40,7 +43,7 @@
|
|||||||
<p>The filename can be configured in the node, if left blank it should be set in an incoming message on <b>msg.filename</b>.</p>
|
<p>The filename can be configured in the node, if left blank it should be set in an incoming message on <b>msg.filename</b>.</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 <b>msg.delete</b> property exists then the file will be deleted instead.</p>
|
<p>This node can also be configured to delete a file if required. <i>Note:</i> Using msg.delete is now deprecated.</p>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/x-red" data-template-name="file in">
|
<script type="text/x-red" data-template-name="file in">
|
||||||
@ -73,7 +76,7 @@
|
|||||||
name: {value:""},
|
name: {value:""},
|
||||||
filename: {value:""},
|
filename: {value:""},
|
||||||
appendNewline: {value:true},
|
appendNewline: {value:true},
|
||||||
overwriteFile: {value:false}
|
overwriteFile: {value:"false"}
|
||||||
},
|
},
|
||||||
color:"BurlyWood",
|
color:"BurlyWood",
|
||||||
inputs:1,
|
inputs:1,
|
||||||
@ -81,10 +84,17 @@
|
|||||||
icon: "file.png",
|
icon: "file.png",
|
||||||
align: "right",
|
align: "right",
|
||||||
label: function() {
|
label: function() {
|
||||||
return this.name||this.filename;
|
if (this.overwriteFile === "delete") { return this.name||"delete "+this.filename; }
|
||||||
|
else { return this.name||this.filename; }
|
||||||
},
|
},
|
||||||
labelStyle: function() {
|
labelStyle: function() {
|
||||||
return this.name?"node_label_italic":"";
|
return this.name?"node_label_italic":"";
|
||||||
|
},
|
||||||
|
oneditprepare: function() {
|
||||||
|
$("#node-input-overwriteFile").on("change",function() {
|
||||||
|
if (this.value === "delete") { $("#node-appline").hide(); }
|
||||||
|
else { $("#node-appline").show(); }
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ module.exports = function(RED) {
|
|||||||
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.toString();
|
||||||
var node = this;
|
var node = this;
|
||||||
this.on("input",function(msg) {
|
this.on("input",function(msg) {
|
||||||
var filename;
|
var filename;
|
||||||
@ -37,31 +37,37 @@ module.exports = function(RED) {
|
|||||||
if (filename === "") {
|
if (filename === "") {
|
||||||
node.warn('No filename specified');
|
node.warn('No filename specified');
|
||||||
} else if (msg.hasOwnProperty('delete')) {
|
} else if (msg.hasOwnProperty('delete')) {
|
||||||
|
node.warn("Deprecated: please use specific delete option in config dialog.");
|
||||||
fs.unlink(filename, function (err) {
|
fs.unlink(filename, function (err) {
|
||||||
if (err) { node.warn('Failed to delete file : '+err); }
|
if (err) { node.warn('Failed to delete file : '+err); }
|
||||||
//console.log('Deleted file",filename);
|
|
||||||
});
|
});
|
||||||
} else if (typeof msg.payload != "undefined") {
|
} else if (typeof msg.payload != "undefined") {
|
||||||
var data = msg.payload;
|
var data = msg.payload;
|
||||||
if (typeof data === "object") {
|
if ((typeof data === "object")&&(!Buffer.isBuffer(data))) {
|
||||||
if (!Buffer.isBuffer(data)) {
|
|
||||||
data = JSON.stringify(data);
|
data = JSON.stringify(data);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (typeof data === "boolean") { data = data.toString(); }
|
if (typeof data === "boolean") { data = data.toString(); }
|
||||||
if ((this.appendNewline)&&(!Buffer.isBuffer(data))) { data += "\n"; }
|
if ((this.appendNewline)&&(!Buffer.isBuffer(data))) { data += "\n"; }
|
||||||
if (this.overwriteFile) {
|
if (this.overwriteFile === "true") {
|
||||||
// using "binary" not {encoding:"binary"} to be 0.8 compatible for a while
|
// using "binary" not {encoding:"binary"} to be 0.8 compatible for a while
|
||||||
|
//fs.writeFile(filename, data, {encoding:"binary"}, function (err) {
|
||||||
fs.writeFile(filename, data, "binary", function (err) {
|
fs.writeFile(filename, data, "binary", function (err) {
|
||||||
if (err) { node.warn('Failed to write to file : '+err); }
|
if (err) { node.warn('Failed to write to file : '+err); }
|
||||||
//console.log('Message written to file',filename);
|
else if (RED.settings.verbose) { node.log('wrote to file: '+filename); }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if (this.overwriteFile === "delete") {
|
||||||
|
fs.unlink(filename, function (err) {
|
||||||
|
if (err) { node.warn('Failed to delete file : '+err); }
|
||||||
|
else if (RED.settings.verbose) { node.log("deleted file: "+filename); }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// using "binary" not {encoding:"binary"} to be 0.8 compatible for a while
|
// using "binary" not {encoding:"binary"} to be 0.8 compatible for a while longer
|
||||||
|
//fs.appendFile(filename, data, {encoding:"binary"}, function (err) {
|
||||||
fs.appendFile(filename, data, "binary", function (err) {
|
fs.appendFile(filename, data, "binary", function (err) {
|
||||||
if (err) { node.warn('Failed to append to file : '+err); }
|
if (err) { node.warn('Failed to append to file : '+err); }
|
||||||
//console.log('Message appended to file',filename);
|
else if (RED.settings.verbose) { node.log('appended to file: '+filename); }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -69,6 +75,7 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
RED.nodes.registerType("file",FileNode);
|
RED.nodes.registerType("file",FileNode);
|
||||||
|
|
||||||
|
|
||||||
function FileInNode(n) {
|
function FileInNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
|
|
||||||
@ -108,4 +115,4 @@ module.exports = function(RED) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("file in",FileInNode);
|
RED.nodes.registerType("file in",FileInNode);
|
||||||
};
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user