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

file node - close files when required

This commit is contained in:
Dave Conway-Jones 2017-06-28 17:36:04 +01:00
parent a6a9025bab
commit 160c27c15a
No known key found for this signature in database
GPG Key ID: 81B04231572A9A2D

View File

@ -59,24 +59,27 @@ module.exports = function(RED) {
while (node.data.length > 0) { while (node.data.length > 0) {
if (this.overwriteFile === "true") { if (this.overwriteFile === "true") {
node.wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'w' }); node.wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'w', autoClose:true });
node.wstream.on("error", function(err) { node.wstream.on("error", function(err) {
node.error(RED._("file.errors.writefail",{error:err.toString()}),msg); node.error(RED._("file.errors.writefail",{error:err.toString()}),msg);
}); });
node.wstream.write(node.data.shift(), function() { node.wstream.end(); });
} }
else { else {
if (!node.wstream) { if ((!node.wstream) || (!node.filename)) {
node.wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'a' }); node.wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'a', autoClose:true });
node.wstream.on("error", function(err) { node.wstream.on("error", function(err) {
node.error(RED._("file.errors.appendfail",{error:err.toString()}),msg); node.error(RED._("file.errors.appendfail",{error:err.toString()}),msg);
}); });
} }
if (node.filename) { node.wstream.write(node.data.shift()); }
else { node.wstream.write(node.data.shift(), function() { node.wstream.end(); }); }
} }
node.wstream.write(node.data.shift());
} }
} }
}); });
this.on('close', function() { this.on('close', function() {
if (node.wstream) { node.wstream.end(); }
node.status({}); node.status({});
}); });
} }