diff --git a/editor/icons/file-in.png b/editor/icons/file-in.png new file mode 100644 index 000000000..d3e5cd7fe Binary files /dev/null and b/editor/icons/file-in.png differ diff --git a/editor/icons/file-out.png b/editor/icons/file-out.png new file mode 100644 index 000000000..051f819c8 Binary files /dev/null and b/editor/icons/file-out.png differ diff --git a/nodes/core/storage/50-file.html b/nodes/core/storage/50-file.html index cdb001eb6..eabf56a76 100644 --- a/nodes/core/storage/50-file.html +++ b/nodes/core/storage/50-file.html @@ -123,9 +123,8 @@ }, color:"BurlyWood", inputs:1, - outputs:0, - icon: "file.png", - align: "right", + outputs:1, + icon: "file-out.png", label: function() { if (this.overwriteFile === "delete") { return this.name||this._("file.label.deletelabel",{file:this.filename}); @@ -159,7 +158,7 @@ outputLabels: function(i) { return (this.format === "utf8") ? "UTF8 string" : "binary buffer"; }, - icon: "file.png", + icon: "file-in.png", label: function() { return this.name||this.filename||this._("file.label.filelabel"); }, diff --git a/nodes/core/storage/50-file.js b/nodes/core/storage/50-file.js index a0e49f6fc..76cedb136 100644 --- a/nodes/core/storage/50-file.js +++ b/nodes/core/storage/50-file.js @@ -39,14 +39,20 @@ module.exports = function(RED) { node.tout = null; },333); } - if (filename === "") { node.warn(RED._("file.errors.nofilename")); } - else if (node.overwriteFile === "delete") { + if (filename === "") { + node.warn(RED._("file.errors.nofilename")); + } else if (node.overwriteFile === "delete") { fs.unlink(filename, function (err) { - if (err) { node.error(RED._("file.errors.deletefail",{error:err.toString()}),msg); } - else if (RED.settings.verbose) { node.log(RED._("file.status.deletedfile",{file:filename})); } + if (err) { + node.error(RED._("file.errors.deletefail",{error:err.toString()}),msg); + } else { + if (RED.settings.verbose) { + node.log(RED._("file.status.deletedfile",{file:filename})); + } + node.send(msg); + } }); - } - else if (msg.hasOwnProperty("payload") && (typeof msg.payload !== "undefined")) { + } else if (msg.hasOwnProperty("payload") && (typeof msg.payload !== "undefined")) { var dir = path.dirname(filename); if (node.createDir) { try { @@ -64,15 +70,21 @@ module.exports = function(RED) { if (typeof data === "boolean") { data = data.toString(); } if (typeof data === "number") { data = data.toString(); } if ((node.appendNewline) && (!Buffer.isBuffer(data))) { data += os.EOL; } - node.data.push(Buffer.from(data)); + node.data.push({msg:msg,data:Buffer.from(data)}); while (node.data.length > 0) { if (node.overwriteFile === "true") { - node.wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'w', autoClose:true }); - node.wstream.on("error", function(err) { - node.error(RED._("file.errors.writefail",{error:err.toString()}),msg); - }); - node.wstream.end(node.data.shift()); + (function(packet) { + node.wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'w', autoClose:true }); + node.wstream.on("error", function(err) { + node.error(RED._("file.errors.writefail",{error:err.toString()}),msg); + }); + node.wstream.on("open", function() { + node.wstream.end(packet.data, function() { + node.send(packet.msg); + }); + }) + })(node.data.shift()); } else { // Append mode @@ -115,10 +127,17 @@ module.exports = function(RED) { } if (node.filename) { // Static filename - write and reuse the stream next time - node.wstream.write(node.data.shift()); + var packet = node.data.shift() + node.wstream.write(packet.data, function() { + node.send(packet.msg); + }); + } else { // Dynamic filename - write and close the stream - node.wstream.end(node.data.shift()); + var packet = node.data.shift() + node.wstream.end(packet.data, function() { + node.send(packet.msg); + }); delete node.wstream; delete node.wstreamIno; }