final updates to file node filename typedInput

- ensure node settings are auto updated to correct typedInput type/value
- improve label
- ensure str and env are considered "static" (keep stream open)
This commit is contained in:
Steve-Mcl 2022-04-16 14:25:37 +01:00
parent 6e35a9f682
commit 99b049fe2d
3 changed files with 71 additions and 29 deletions

View File

@ -211,7 +211,8 @@
icon: "file-out.svg",
label: function() {
var fn = this.filename;
if(this.filenameType != "str") { fn = ""; }
if(this.filenameType != "str" && this.filenameType != "env" ) { fn = ""; }
if(this.filenameType === "env") { fn = "env."+fn; }
if (this.overwriteFile === "delete") {
return this.name||this._("file.label.deletelabel",{file:fn});
} else {
@ -239,11 +240,25 @@
types:[{ value: "str", label:"", icon:"red/images/typedInput/az.svg"}, "msg", "jsonata", "env"],
typeField: $("#node-input-filenameType")
});
if(!node.filename && !node.filenameType) {
node.filename = "filename"
node.filenameType = "msg"
$("#node-input-filename").typedInput("type", node.filenameType);
$("#node-input-filename").typedInput("value", node.filename);
if(typeof node.filenameType == 'undefined') {
//existing node AND filenameType is not set - inplace (compatible) upgrade to new typedInput
if(node.filename == "") { //was using empty value to denote msg.filename - set typedInput to match
node.filename = "filename";
node.filenameType = "msg";
$("#node-input-filename").typedInput("type", node.filenameType);
$("#node-input-filename").typedInput("value", node.filename);
} else if(/^\${[^}]+}$/.test(node.filename)) { //was using an ${ENV_VAR}
node.filenameType = "env";
node.filename = node.filename.replace(/\${([^}]+)}/g, function(match, name) {
return (name === undefined)?"":name;
});
$("#node-input-filename").typedInput("type", node.filenameType);
$("#node-input-filename").typedInput("value", node.filename);
} else { //was using a static filename - set typedInput type to str
node.filenameType = "str";
$("#node-input-filename").typedInput("type", node.filenameType);
$("#node-input-filename").typedInput("value", node.filename);
}
}
encodings.forEach(function(item) {
if(Array.isArray(item)) {
@ -309,7 +324,8 @@
icon: "file-in.svg",
label: function() {
var fn = this.filename;
if(this.filenameType != "str") { fn = ""; }
if(this.filenameType != "str" && this.filenameType != "env" ) { fn = ""; }
if(this.filenameType === "env") { fn = "env."+fn; }
return this.name||fn||this._("file.label.read");
},
paletteLabel: RED._("node-red:file.label.read"),
@ -329,11 +345,25 @@
types:[{ value: "str", label:"", icon:"red/images/typedInput/az.svg"}, "msg", "jsonata", "env"],
typeField: $("#node-input-filenameType")
});
if(!node.filename && !node.filenameType) {
node.filename = "filename"
node.filenameType = "msg"
$("#node-input-filename").typedInput("type", node.filenameType);
$("#node-input-filename").typedInput("value", node.filename);
if(typeof node.filenameType == 'undefined') {
//existing node AND filenameType is not set - inplace (compatible) upgrade to new typedInput
if(node.filename == "") { //was using empty value to denote msg.filename - set typedInput to match
node.filename = "filename";
node.filenameType = "msg";
$("#node-input-filename").typedInput("type", node.filenameType);
$("#node-input-filename").typedInput("value", node.filename);
} else if(/^\${[^}]+}$/.test(node.filename)) { //was using an ${ENV_VAR}
node.filenameType = "env";
node.filename = node.filename.replace(/\${([^}]+)}/g, function(match, name) {
return (name === undefined)?"":name;
});
$("#node-input-filename").typedInput("type", node.filenameType);
$("#node-input-filename").typedInput("value", node.filename);
} else { //was using a static filename - set typedInput type to str
node.filenameType = "str";
$("#node-input-filename").typedInput("type", node.filenameType);
$("#node-input-filename").typedInput("value", node.filename);
}
}
encodings.forEach(function(item) {
if(Array.isArray(item)) {

View File

@ -52,10 +52,20 @@ module.exports = function(RED) {
function processMsg(msg,nodeSend, done) {
var filename = node.filename || "";
//Pre V3 compatibility - if filename and filenameType are empty, check msg.filename
if(!node.filenameType && !node.filename) {
msg.filename = "filename";
node.filenameType = "msg";
//Pre V3 compatibility - if filenameType is empty, do in place upgrade
if(typeof node.filenameType == 'undefined' || node.filenameType == "") {
//existing node AND filenameType is not set - inplace (compatible) upgrade
if(filename == "") { //was using empty value to denote msg.filename
node.filename = "filename";
node.filenameType = "msg";
} else if(/^\${[^}]+}$/.test(filename)) { //was using an ${ENV_VAR}
node.filenameType = "env";
node.filename = filename.replace(/\${([^}]+)}/g, function(match, name) {
return (name === undefined)?"":name;
});
} else { //was using a static filename - set typedInput type to str
node.filenameType = "str";
}
}
RED.util.evaluateNodeProperty(node.filename,node.filenameType,node,msg,(err,value) => {
@ -174,7 +184,7 @@ module.exports = function(RED) {
done();
});
}
if (node.filenameType === "str") {
if (node.filenameType === "str" || node.filenameType === "env") {
// Static filename - write and reuse the stream next time
node.wstream.write(buf, function() {
nodeSend(msg);
@ -288,12 +298,21 @@ module.exports = function(RED) {
this.on("input",function(msg, nodeSend, nodeDone) {
var filename = node.filename || "";
//Pre V3 compatibility - if filename and filenameType are empty, check msg.filename
if(!node.filenameType && !node.filename) {
msg.filename = "filename";
node.filenameType = "msg";
//Pre V3 compatibility - if filenameType is empty, do in place upgrade
if(typeof node.filenameType == 'undefined' || node.filenameType == "") {
//existing node AND filenameType is not set - inplace (compatible) upgrade
if(filename == "") { //was using empty value to denote msg.filename
node.filename = "filename";
node.filenameType = "msg";
} else if(/^\${[^}]+}$/.test(filename)) { //was using an ${ENV_VAR}
node.filenameType = "env";
node.filename = filename.replace(/\${([^}]+)}/g, function(match, name) {
return (name === undefined)?"":name;
});
} else { //was using a static filename - set typedInput type to str
node.filenameType = "str";
}
}
RED.util.evaluateNodeProperty(node.filename,node.filenameType,node,msg,(err,value) => {
if (err) {
node.error(err,msg);

View File

@ -19,8 +19,6 @@
Alternatively, it can delete the file.</p>
<h3>Inputs</h3>
<dl class="message-properties">
<dt class="optional">filename <span class="property-type">string</span></dt>
<dd>If not configured in the node, this optional property sets the name of the file to be updated.</dd>
<dt class="optional">encoding <span class="property-type">string</span></dt>
<dd>If encoding is configured to be set by msg, then this optional property can set the encoding.</dt>
</dl>
@ -40,11 +38,6 @@
<script type="text/html" data-help-name="file in">
<p>Reads the contents of a file as either a string or binary buffer.</p>
<h3>Inputs</h3>
<dl class="message-properties">
<dt class="optional">filename <span class="property-type">string</span></dt>
<dd>if not set in the node configuration, this property sets the filename to read.</dd>
</dl>
<h3>Outputs</h3>
<dl class="message-properties">
<dt>payload <span class="property-type">string | buffer</span></dt>