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

Fix File node check of msg.payload to close #700

Also add feature to allow creation of directory(ies) if path to file
does not exist.
This commit is contained in:
Dave Conway-Jones 2015-07-10 14:59:23 +01:00
parent e5a0f25d94
commit 60539d890b
4 changed files with 186 additions and 54 deletions

View File

@ -582,6 +582,7 @@
"filename": "Filename", "filename": "Filename",
"action": "Action", "action": "Action",
"addnewline": "Add newline (\\n) to each payload ?", "addnewline": "Add newline (\\n) to each payload ?",
"createdir": "Create directory if it doesn't exist ?",
"outputas": "Output as", "outputas": "Output as",
"filelabel": "file", "filelabel": "file",
"deletelabel": "delete __file__" "deletelabel": "delete __file__"
@ -605,7 +606,8 @@
"invaliddelete": "Warning: Invalid delete. Please use specific delete option in config dialog.", "invaliddelete": "Warning: Invalid delete. Please use specific delete option in config dialog.",
"deletefail": "failed to delete file: __error__", "deletefail": "failed to delete file: __error__",
"writefail": "failed to write to file: __error__", "writefail": "failed to write to file: __error__",
"appendfail": "failed to append to file: __error__" "appendfail": "failed to append to file: __error__",
"createfail": "failed to create file: __error__"
} }
} }
} }

View File

@ -29,9 +29,14 @@
</div> </div>
<div class="form-row" id="node-appline"> <div class="form-row" id="node-appline">
<label>&nbsp;</label> <label>&nbsp;</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" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-appendNewline" style="width: 70%;"><span data-i18n="file.label.addnewline"></span></label> <label for="node-input-appendNewline" style="width: 70%;"><span data-i18n="file.label.addnewline"></span></label>
</div> </div>
<div class="form-row">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-createDir" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-createDir" style="width: 70%;"><span data-i18n="file.label.createdir"></span></label>
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label> <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name"> <input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
@ -76,6 +81,7 @@
name: {value:""}, name: {value:""},
filename: {value:""}, filename: {value:""},
appendNewline: {value:true}, appendNewline: {value:true},
createDir: {value:false},
overwriteFile: {value:"false"} overwriteFile: {value:"false"}
}, },
color:"BurlyWood", color:"BurlyWood",

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2013, 2014 IBM Corp. * Copyright 2013, 2015 IBM Corp.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,37 +17,48 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
var fs = require("fs-extra"); var fs = require("fs-extra");
var os = require("os");
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.toString(); this.overwriteFile = n.overwriteFile.toString();
this.createDir = n.createDir || false;
var node = this; var node = this;
this.on("input",function(msg) { this.on("input",function(msg) {
var filename = this.filename || msg.filename || ""; var filename = node.filename || msg.filename || "";
if (msg.filename && n.filename && (n.filename !== msg.filename)) { if (!node.filename) {
node.warn(RED._("common.errors.nooverride"));
}
if (!this.filename) {
node.status({fill:"grey",shape:"dot",text:filename}); node.status({fill:"grey",shape:"dot",text:filename});
} }
if (filename === "") { if (filename === "") {
node.warn(RED._("file.errors.nofilename")); node.warn(RED._("file.errors.nofilename"));
} else if (msg.hasOwnProperty('delete')) { // remove warning at some point in future } else if (msg.hasOwnProperty("payload") && (typeof msg.payload !== "undefined")) {
node.warn(RED._("file.errors.invaliddelete"));
} else if (msg.payload && (typeof msg.payload != "undefined")) {
var data = msg.payload; var data = msg.payload;
if ((typeof data === "object") && (!Buffer.isBuffer(data))) { if ((typeof data === "object") && (!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 (typeof data === "number") { data = data.toString(); }
if ((this.appendNewline) && (!Buffer.isBuffer(data))) { data += os.EOL; }
if (this.overwriteFile === "true") { 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, "binary", function (err) {
//fs.writeFile(filename, data, {encoding:"binary"}, function (err) { //fs.writeFile(filename, data, {encoding:"binary"}, function (err) {
if (err) {
if ((err.code === "ENOENT") && node.createDir) {
fs.ensureFile(filename, function (err) {
if (err) { node.error(RED._("file.errors.createfail",{error:err.toString()}),msg); }
else {
fs.writeFile(filename, data, "binary", function (err) { fs.writeFile(filename, data, "binary", function (err) {
if (err) { node.error(RED._("file.errors.writefail",{error:err.toString()}),msg); } if (err) { node.error(RED._("file.errors.writefail",{error:err.toString()}),msg); }
});
}
});
}
else { node.error(RED._("file.errors.writefail",{error:err.toString()}),msg); }
}
else if (RED.settings.verbose) { node.log(RED._("file.status.wrotefile",{file:filename})); } else if (RED.settings.verbose) { node.log(RED._("file.status.wrotefile",{file:filename})); }
}); });
} }
@ -59,9 +70,21 @@ module.exports = function(RED) {
} }
else { else {
// using "binary" not {encoding:"binary"} to be 0.8 compatible for a while longer // using "binary" not {encoding:"binary"} to be 0.8 compatible for a while longer
fs.appendFile(filename, data, "binary", function (err) {
//fs.appendFile(filename, data, {encoding:"binary"}, function (err) { //fs.appendFile(filename, data, {encoding:"binary"}, function (err) {
if (err) {
if ((err.code === "ENOENT") && node.createDir) {
fs.ensureFile(filename, function (err) {
if (err) { node.error(RED._("file.errors.createfail",{error:err.toString()}),msg); }
else {
fs.appendFile(filename, data, "binary", function (err) { fs.appendFile(filename, data, "binary", function (err) {
if (err) { node.error(RED._("file.errors.appendfail",{error:err.toString()}),msg); } if (err) { node.error(RED._("file.errors.appendfail",{error:err.toString()}),msg); }
});
}
});
}
else { node.error(RED._("file.errors.appendfail",{error:err.toString()}),msg); }
}
else if (RED.settings.verbose) { node.log(RED._("file.status.appendedfile",{file:filename})); } else if (RED.settings.verbose) { node.log(RED._("file.status.appendedfile",{file:filename})); }
}); });
} }
@ -82,11 +105,8 @@ module.exports = function(RED) {
options['encoding'] = this.format; options['encoding'] = this.format;
} }
this.on("input",function(msg) { this.on("input",function(msg) {
var filename = this.filename || msg.filename || ""; var filename = node.filename || msg.filename || "";
if (msg.filename && n.filename && (n.filename !== msg.filename)) { if (!node.filename) {
node.warn(RED._("file.errors.nooverride"));
}
if (!this.filename) {
node.status({fill:"grey",shape:"dot",text:filename}); node.status({fill:"grey",shape:"dot",text:filename});
} }
if (filename === "") { if (filename === "") {

View File

@ -240,6 +240,109 @@ describe('file Nodes', function() {
}); });
}); });
it('should fail to create a new directory if not asked to do so (append)', function(done) {
// Stub file write so we can make writes fail
var fileToTest2 = path.join(resourcesDir,"a","50-file-test-file.txt");
//var spy = sinon.stub(fs, 'appendFile', function(arg,arg2,arg3,arg4){ arg4(new Error("Stub error message")); });
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest2, "appendNewline":true, "overwriteFile":false}];
helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileNode1");
setTimeout(function() {
try {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "file";
});
//console.log(logEvents);
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("file.errors.appendfail");
done();
}
catch(e) { done(e); }
//finally { fs.appendFile.restore(); }
},wait);
n1.receive({payload:"test2"});
});
});
it('should try to create a new directory if asked to do so (append)', function(done) {
// Stub file write so we can make writes fail
var fileToTest2 = path.join(resourcesDir,"a","50-file-test-file.txt");
var spy = sinon.stub(fs, "ensureFile", function(arg1,arg2,arg3,arg4) { arg2(null); });
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest2, "appendNewline":true, "overwriteFile":false, "createDir":true}];
helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileNode1");
setTimeout(function() {
try {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "file";
});
//console.log(logEvents);
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("file.errors.appendfail");
done();
}
catch(e) { done(e); }
finally { fs.ensureFile.restore(); }
},wait);
n1.receive({payload:"test2"});
});
});
it('should fail to create a new directory if not asked to do so (overwrite)', function(done) {
// Stub file write so we can make writes fail
var fileToTest2 = path.join(resourcesDir,"a","50-file-test-file.txt");
//var spy = sinon.stub(fs, 'appendFile', function(arg,arg2,arg3,arg4){ arg4(new Error("Stub error message")); });
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest2, "appendNewline":false, "overwriteFile":true}];
helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileNode1");
setTimeout(function() {
try {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "file";
});
//console.log(logEvents);
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("file.errors.writefail");
done();
}
catch(e) { done(e); }
//finally { fs.appendFile.restore(); }
},wait);
n1.receive({payload:"test2"});
});
});
it('should try to create a new directory if asked to do so (overwrite)', function(done) {
// Stub file write so we can make writes fail
var fileToTest2 = path.join(resourcesDir,"a","50-file-test-file.txt");
var spy = sinon.stub(fs, "ensureFile", function(arg1,arg2,arg3,arg4){ arg2(null); });
var flow = [{id:"fileNode1", type:"file", name: "fileNode", "filename":fileToTest2, "appendNewline":true, "overwriteFile":true, "createDir":true}];
helper.load(fileNode, flow, function() {
var n1 = helper.getNode("fileNode1");
setTimeout(function() {
try {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "file";
});
//console.log(logEvents);
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("file.errors.writefail");
done();
}
catch(e) { done(e); }
finally { fs.ensureFile.restore(); }
},wait);
n1.receive({payload:"test2"});
});
});
}); });
@ -303,27 +406,28 @@ describe('file Nodes', function() {
//}); //});
//}); //});
it('should warn if msg.props try to overide', function(done) { // Commented out as we no longer need to warn of the very old deprecated behaviour
var flow = [{id:"fileInNode1", type:"file in", name: "fileInNode", "filename":fileToTest, "format":"", wires:[["n2"]]}, //it('should warn if msg.props try to overide', function(done) {
{id:"n2", type:"helper"}]; //var flow = [{id:"fileInNode1", type:"file in", name: "fileInNode", "filename":fileToTest, "format":"", wires:[["n2"]]},
helper.load(fileNode, flow, function() { //{id:"n2", type:"helper"}];
var n1 = helper.getNode("fileInNode1"); //helper.load(fileNode, flow, function() {
var n2 = helper.getNode("n2"); //var n1 = helper.getNode("fileInNode1");
n2.on("input", function(msg) { //var n2 = helper.getNode("n2");
msg.should.have.property('payload'); //n2.on("input", function(msg) {
msg.payload.should.have.length(39).and.be.a.Buffer; //msg.should.have.property('payload');
msg.payload.toString().should.equal("File message line 1\File message line 2\n"); //msg.payload.should.have.length(39).and.be.a.Buffer;
var logEvents = helper.log().args.filter(function(evt) { //msg.payload.toString().should.equal("File message line 1\File message line 2\n");
return evt[0].type == "file in"; //var logEvents = helper.log().args.filter(function(evt) {
}); //return evt[0].type == "file in";
logEvents.should.have.length(1); //});
logEvents[0][0].should.have.a.property('msg'); //logEvents.should.have.length(1);
logEvents[0][0].msg.toString().should.startWith("file.errors.nooverride"); //logEvents[0][0].should.have.a.property('msg');
done(); //logEvents[0][0].msg.toString().should.startWith("file.errors.nooverride");
}); //done();
n1.receive({payload:"",filename:"foo.txt"}); //});
}); //n1.receive({payload:"",filename:"foo.txt"});
}); //});
//});
it('should warn if no filename set', function(done) { it('should warn if no filename set', function(done) {
var flow = [{id:"fileInNode1", type:"file in", name: "fileInNode", "format":""}]; var flow = [{id:"fileInNode1", type:"file in", name: "fileInNode", "format":""}];