mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	File out node - fix timing of msg.send to be after close., and...
allow msg.encoding to set encoding if desired. To close #2921
This commit is contained in:
		| @@ -219,6 +219,10 @@ | ||||
|                 value: "none", | ||||
|                 label: label | ||||
|             }).text(label).appendTo(encSel); | ||||
|             $("<option/>", { | ||||
|                 value: "setbymsg", | ||||
|                 label: node._("file.encoding.setbymsg") | ||||
|             }).text(label).appendTo(encSel); | ||||
|             encodings.forEach(function(item) { | ||||
|                 if(Array.isArray(item)) { | ||||
|                     var group = $("<optgroup/>", { | ||||
|   | ||||
| @@ -61,11 +61,13 @@ module.exports = function(RED) { | ||||
|             if (filename === "") { | ||||
|                 node.warn(RED._("file.errors.nofilename")); | ||||
|                 done(); | ||||
|             } else if (node.overwriteFile === "delete") { | ||||
|             } | ||||
|             else if (node.overwriteFile === "delete") { | ||||
|                 fs.unlink(filename, function (err) { | ||||
|                     if (err) { | ||||
|                         node.error(RED._("file.errors.deletefail",{error:err.toString()}),msg); | ||||
|                     } else { | ||||
|                     } | ||||
|                     else { | ||||
|                         if (RED.settings.verbose) { | ||||
|                             node.log(RED._("file.status.deletedfile",{file:filename})); | ||||
|                         } | ||||
| @@ -73,12 +75,14 @@ module.exports = function(RED) { | ||||
|                     } | ||||
|                     done(); | ||||
|                 }); | ||||
|             } 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 { | ||||
|                         fs.ensureDirSync(dir); | ||||
|                     } catch(err) { | ||||
|                     } | ||||
|                     catch(err) { | ||||
|                         node.error(RED._("file.errors.createfail",{error:err.toString()}),msg); | ||||
|                         done(); | ||||
|                         return; | ||||
| @@ -92,7 +96,11 @@ 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; } | ||||
|                 var buf = encode(data, node.encoding); | ||||
|                 var buf; | ||||
|                 if (node.encoding === "setbymsg") { | ||||
|                     buf = encode(data, msg.encoding || "none"); | ||||
|                 } | ||||
|                 else { buf = encode(data, node.encoding); } | ||||
|                 if (node.overwriteFile === "true") { | ||||
|                     var wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'w', autoClose:true }); | ||||
|                     node.wstream = wstream; | ||||
| @@ -101,10 +109,11 @@ module.exports = function(RED) { | ||||
|                         done(); | ||||
|                     }); | ||||
|                     wstream.on("open", function() { | ||||
|                         wstream.end(buf, function() { | ||||
|                         wstream.once("close", function() { | ||||
|                             nodeSend(msg); | ||||
|                             done(); | ||||
|                         }); | ||||
|                         wstream.end(buf); | ||||
|                     }) | ||||
|                     return; | ||||
|                 } | ||||
| @@ -126,7 +135,8 @@ module.exports = function(RED) { | ||||
|                                 delete node.wstream; | ||||
|                                 delete node.wstreamIno; | ||||
|                             } | ||||
|                         } catch(err) { | ||||
|                         } | ||||
|                         catch(err) { | ||||
|                             // File does not exist | ||||
|                             recreateStream = true; | ||||
|                             node.wstream.end(); | ||||
| @@ -154,14 +164,16 @@ module.exports = function(RED) { | ||||
|                             nodeSend(msg); | ||||
|                             done(); | ||||
|                         }); | ||||
|                     } else { | ||||
|                     } | ||||
|                     else { | ||||
|                         // Dynamic filename - write and close the stream | ||||
|                         node.wstream.end(buf, function() { | ||||
|                         node.wstream.once("close", function() { | ||||
|                             nodeSend(msg); | ||||
|                             delete node.wstream; | ||||
|                             delete node.wstreamIno; | ||||
|                             done(); | ||||
|                         }); | ||||
|                         node.wstream.end(buf); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
| @@ -276,7 +288,6 @@ module.exports = function(RED) { | ||||
|                     ch = "\n"; | ||||
|                     type = "string"; | ||||
|                 } | ||||
|                 var hwm; | ||||
|                 var getout = false; | ||||
|  | ||||
|                 var rs = fs.createReadStream(filename) | ||||
| @@ -340,16 +351,17 @@ module.exports = function(RED) { | ||||
|                             nodeSend(msg); | ||||
|                         } | ||||
|                         else if (node.format === "lines") { | ||||
|                             var m = { payload: spare, | ||||
|                                       topic:msg.topic, | ||||
|                                       parts: { | ||||
|                                           index: count, | ||||
|                                           count: count+1, | ||||
|                                           ch: ch, | ||||
|                                           type: type, | ||||
|                                           id: msg._msgid | ||||
|                                       } | ||||
|                                     }; | ||||
|                             var m = { | ||||
|                                 payload: spare, | ||||
|                                 topic:msg.topic, | ||||
|                                 parts: { | ||||
|                                     index: count, | ||||
|                                     count: count+1, | ||||
|                                     ch: ch, | ||||
|                                     type: type, | ||||
|                                     id: msg._msgid | ||||
|                                 } | ||||
|                             }; | ||||
|                             nodeSend(m); | ||||
|                         } | ||||
|                         else if (getout) { // last chunk same size as high water mark - have to send empty extra packet. | ||||
|   | ||||
| @@ -877,6 +877,7 @@ | ||||
|         }, | ||||
|         "encoding": { | ||||
|             "none": "default", | ||||
|             "setbymsg": "set by msg.encoding", | ||||
|             "native": "Native", | ||||
|             "unicode": "Unicode", | ||||
|             "japanese": "Japanese", | ||||
|   | ||||
| @@ -21,6 +21,8 @@ | ||||
|     <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> | ||||
|     <h3>Output</h3> | ||||
|     <p>On completion of write, input message is sent to output port.</p> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user