mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
add encoding support to file in/out node (#2066)
* add encoding support to file in/out node * update package.json * change default encoding label: 'none' -> 'utf8[default]' * add a missing message catalogue entry * change default encoding label
This commit is contained in:
committed by
Dave Conway-Jones
parent
3c013b3533
commit
dc89218702
@@ -22,6 +22,11 @@
|
||||
<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">
|
||||
<label for="node-input-encoding"><i class="fa fa-flag"></i> <span data-i18n="file.label.encoding"></span></label>
|
||||
<select type="text" id="node-input-encoding" style="width: 250px;">
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<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">
|
||||
@@ -48,6 +53,11 @@
|
||||
<input type="checkbox" id="node-input-sendError" style="width:auto">
|
||||
<label style="width:auto; margin-bottom:0; vertical-align: middle;" for="node-input-sendError" data-i18n="file.label.sendError"></label>
|
||||
</div>
|
||||
<div class="form-row" id="encoding-spec">
|
||||
<label for="node-input-encoding"><i class="fa fa-flag"></i> <span data-i18n="file.label.encoding"></span></label>
|
||||
<select type="text" id="node-input-encoding" style="width: 250px;">
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<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">
|
||||
@@ -56,6 +66,132 @@
|
||||
</script>
|
||||
|
||||
<script type="text/javascript">
|
||||
(function(){
|
||||
var encodings = [
|
||||
[ "file.encoding.native",
|
||||
"utf8",
|
||||
"ucs2",
|
||||
"utf-16le",
|
||||
"ascii",
|
||||
"binary",
|
||||
"base64",
|
||||
"hex"
|
||||
],
|
||||
[ "file.encoding.unicode",
|
||||
"utf-16be",
|
||||
],
|
||||
[ "file.encoding.japanese",
|
||||
"Shift_JIS",
|
||||
"Windows-31j",
|
||||
"Windows932",
|
||||
"EUC-JP"
|
||||
],
|
||||
[ "file.encoding.chinese",
|
||||
"GB2312",
|
||||
"GBK",
|
||||
"GB18030",
|
||||
"Windows936",
|
||||
"EUC-CN"
|
||||
],
|
||||
[ "file.encoding.korean",
|
||||
"KS_C_5601",
|
||||
"Windows949",
|
||||
"EUC-KR"
|
||||
],
|
||||
[ "file.encoding.taiwan",
|
||||
"Big5",
|
||||
"Big5-HKSCS",
|
||||
"Windows950"
|
||||
],
|
||||
[ "file.encoding.windows",
|
||||
"cp874",
|
||||
"cp1250",
|
||||
"cp1251",
|
||||
"cp1252",
|
||||
"cp1253",
|
||||
"cp1254",
|
||||
"cp1255",
|
||||
"cp1256",
|
||||
"cp1257",
|
||||
"cp1258"
|
||||
],
|
||||
[ "file.encoding.iso",
|
||||
"ISO-8859-1",
|
||||
"ISO-8859-2",
|
||||
"ISO-8859-3",
|
||||
"ISO-8859-4",
|
||||
"ISO-8859-5",
|
||||
"ISO-8859-6",
|
||||
"ISO-8859-7",
|
||||
"ISO-8859-8",
|
||||
"ISO-8859-9",
|
||||
"ISO-8859-10",
|
||||
"ISO-8859-11",
|
||||
"ISO-8859-12",
|
||||
"ISO-8859-13",
|
||||
"ISO-8859-14",
|
||||
"ISO-8859-15",
|
||||
"ISO-8859-16"
|
||||
],
|
||||
[ "file.encoding.ibm",
|
||||
"cp437",
|
||||
"cp737",
|
||||
"cp775",
|
||||
"cp808",
|
||||
"cp850",
|
||||
"cp852",
|
||||
"cp855",
|
||||
"cp856",
|
||||
"cp857",
|
||||
"cp858",
|
||||
"cp860",
|
||||
"cp861",
|
||||
"cp866",
|
||||
"cp869",
|
||||
"cp922",
|
||||
"cp1046",
|
||||
"cp1124",
|
||||
"cp1125",
|
||||
"cp1129",
|
||||
"cp1133",
|
||||
"cp1161",
|
||||
"cp1162",
|
||||
"cp1163"
|
||||
],
|
||||
[ "file.encoding.mac",
|
||||
"maccroatian",
|
||||
"maccyrillic",
|
||||
"macgreek",
|
||||
"maciceland",
|
||||
"macroman",
|
||||
"macromania",
|
||||
"macthai",
|
||||
"macturkish",
|
||||
"macukraine",
|
||||
"maccenteuro",
|
||||
"macintosh"
|
||||
],
|
||||
[ "file.encoding.koi8",
|
||||
"koi8-r",
|
||||
"koi8-u",
|
||||
"koi8-ru",
|
||||
"koi8-t"
|
||||
],
|
||||
[ "file.encoding.misc",
|
||||
"armscii8",
|
||||
"rk1048",
|
||||
"tcvn",
|
||||
"georgianacademy",
|
||||
"georgianps",
|
||||
"pt154",
|
||||
"viscii",
|
||||
"iso646cn",
|
||||
"iso646jp",
|
||||
"hproman8",
|
||||
"tis620"
|
||||
]
|
||||
];
|
||||
|
||||
RED.nodes.registerType('file',{
|
||||
category: 'storage-output',
|
||||
defaults: {
|
||||
@@ -63,7 +199,8 @@
|
||||
filename: {value:""},
|
||||
appendNewline: {value:true},
|
||||
createDir: {value:false},
|
||||
overwriteFile: {value:"false"}
|
||||
overwriteFile: {value:"false"},
|
||||
encoding: {value:"none"}
|
||||
},
|
||||
color:"BurlyWood",
|
||||
inputs:1,
|
||||
@@ -80,6 +217,33 @@
|
||||
return this.name?"node_label_italic":"";
|
||||
},
|
||||
oneditprepare: function() {
|
||||
var node = this;
|
||||
var encSel = $("#node-input-encoding");
|
||||
$("<option/>", {
|
||||
value: "none",
|
||||
label: node._("file.encoding.none")
|
||||
}).appendTo(encSel);
|
||||
encodings.forEach(function(item) {
|
||||
if(Array.isArray(item)) {
|
||||
var group = $("<optgroup/>", {
|
||||
label: node._(item[0])
|
||||
}).appendTo(encSel);
|
||||
for (var i = 1; i < item.length; i++) {
|
||||
var enc = item[i];
|
||||
$("<option/>", {
|
||||
value: enc,
|
||||
label: enc
|
||||
}).appendTo(group);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$("<option/>", {
|
||||
value: item,
|
||||
label: item
|
||||
}).appendTo(encSel);
|
||||
}
|
||||
});
|
||||
encSel.val(node.encoding);
|
||||
$("#node-input-overwriteFile").on("change",function() {
|
||||
if (this.value === "delete") { $(".form-row-file-write-options").hide(); }
|
||||
else { $(".form-row-file-write-options").show(); }
|
||||
@@ -94,7 +258,8 @@
|
||||
filename: {value:""},
|
||||
format: {value:"utf8"},
|
||||
chunk: {value:false},
|
||||
sendError: {value: false}
|
||||
sendError: {value: false},
|
||||
encoding: {value: "none"}
|
||||
},
|
||||
color:"BurlyWood",
|
||||
inputs:1,
|
||||
@@ -110,19 +275,46 @@
|
||||
return this.name?"node_label_italic":"";
|
||||
},
|
||||
oneditprepare: function() {
|
||||
var node = this;
|
||||
var encSel = $("#node-input-encoding");
|
||||
$("<option/>", {
|
||||
value: "none",
|
||||
label: node._("file.encoding.none")
|
||||
}).appendTo(encSel);
|
||||
encodings.forEach(function(item) {
|
||||
if(Array.isArray(item)) {
|
||||
var group = $("<optgroup/>", {
|
||||
label: node._(item[0])
|
||||
}).appendTo(encSel);
|
||||
for (var i = 1; i < item.length; i++) {
|
||||
var enc = item[i];
|
||||
$("<option/>", {
|
||||
value: enc,
|
||||
label: enc
|
||||
}).appendTo(group);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$("<option/>", {
|
||||
value: item,
|
||||
label: item
|
||||
}).appendTo(encSel);
|
||||
}
|
||||
});
|
||||
encSel.val(node.encoding);
|
||||
if (this.sendError === undefined) {
|
||||
$("#node-input-sendError").prop("checked",true);
|
||||
}
|
||||
$("#node-input-format").on("change",function() {
|
||||
if ($("#node-input-format").val() === "utf8") {
|
||||
$("#buffer-input-type").hide();
|
||||
$("#line-input-type").show();
|
||||
var format = $("#node-input-format").val();
|
||||
if ((format === "utf8") || (format === "lines")) {
|
||||
$("#encoding-spec").show();
|
||||
}
|
||||
else {
|
||||
$("#buffer-input-type").show();
|
||||
$("#line-input-type").hide();
|
||||
$("#encoding-spec").hide();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
|
@@ -19,13 +19,29 @@ module.exports = function(RED) {
|
||||
var fs = require("fs-extra");
|
||||
var os = require("os");
|
||||
var path = require("path");
|
||||
var iconv = require("iconv-lite")
|
||||
|
||||
function encode(data, enc) {
|
||||
if (enc !== "none") {
|
||||
return iconv.encode(data, enc);
|
||||
}
|
||||
return Buffer.from(data);
|
||||
}
|
||||
|
||||
function decode(data, enc) {
|
||||
if (enc !== "none") {
|
||||
return iconv.decode(data, enc);
|
||||
}
|
||||
return data.toString();
|
||||
}
|
||||
|
||||
function FileNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
this.filename = n.filename;
|
||||
this.appendNewline = n.appendNewline;
|
||||
this.overwriteFile = n.overwriteFile.toString();
|
||||
this.createDir = n.createDir || false;
|
||||
this.encoding = n.encoding || "none";
|
||||
var node = this;
|
||||
node.wstream = null;
|
||||
node.msgQueue = [];
|
||||
@@ -75,7 +91,7 @@ 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 = Buffer.from(data);
|
||||
var buf = encode(data, node.encoding);
|
||||
if (node.overwriteFile === "true") {
|
||||
var wstream = fs.createWriteStream(filename, { encoding:'binary', flags:'w', autoClose:true });
|
||||
node.wstream = wstream;
|
||||
@@ -222,6 +238,7 @@ module.exports = function(RED) {
|
||||
this.filename = n.filename;
|
||||
this.format = n.format;
|
||||
this.chunk = false;
|
||||
this.encoding = n.encoding || "none";
|
||||
if (n.sendError === undefined) {
|
||||
this.sendError = true;
|
||||
} else {
|
||||
@@ -261,7 +278,7 @@ module.exports = function(RED) {
|
||||
if (node.chunk === true) {
|
||||
getout = true;
|
||||
if (node.format === "lines") {
|
||||
spare += chunk.toString();
|
||||
spare += decode(chunk, node.encoding);
|
||||
var bits = spare.split("\n");
|
||||
for (var i=0; i < bits.length - 1; i++) {
|
||||
var m = {
|
||||
@@ -306,7 +323,9 @@ module.exports = function(RED) {
|
||||
})
|
||||
.on('end', function() {
|
||||
if (node.chunk === false) {
|
||||
if (node.format === "utf8") { msg.payload = lines.toString(); }
|
||||
if (node.format === "utf8") {
|
||||
msg.payload = decode(lines, node.encoding);
|
||||
}
|
||||
else { msg.payload = lines; }
|
||||
node.send(msg);
|
||||
}
|
||||
|
@@ -860,6 +860,7 @@
|
||||
"filelabel": "file",
|
||||
"sendError": "Send message on error (legacy mode)",
|
||||
"deletelabel": "delete __file__",
|
||||
"encoding": "Encoding"
|
||||
"utf8String": "UTF8 string",
|
||||
"binaryBuffer": "binary buffer"
|
||||
},
|
||||
@@ -879,6 +880,21 @@
|
||||
"deletedfile": "deleted file: __file__",
|
||||
"appendedfile": "appended to file: __file__"
|
||||
},
|
||||
"encoding": {
|
||||
"none": "default",
|
||||
"native": "Native",
|
||||
"unicode": "Unicode",
|
||||
"japanese": "Japanese",
|
||||
"chinese": "Chinese",
|
||||
"korean": "Korean",
|
||||
"taiwan": "Taiwan/Hong Kong",
|
||||
"windows": "Windows codepages",
|
||||
"iso": "ISO codepages",
|
||||
"ibm": "IBM codepages",
|
||||
"mac": "Mac codepages",
|
||||
"koi8": "KOI8 codepages",
|
||||
"misc": "Miscellaneous"
|
||||
},
|
||||
"errors": {
|
||||
"nofilename": "No filename specified",
|
||||
"invaliddelete": "Warning: Invalid delete. Please use specific delete option in config dialog.",
|
||||
|
@@ -32,6 +32,7 @@
|
||||
<p>It can be configured to overwrite the entire file rather than append. For example,
|
||||
when writing binary data to a file, such as an image, this option should be used
|
||||
and the option to append a newline should be disabled.</p>
|
||||
<p>Encoding of data written to a file can be specified from list of encodings.</p>
|
||||
<p>Alternatively, this node can be configured to delete the file.</p>
|
||||
</script>
|
||||
|
||||
@@ -63,6 +64,7 @@
|
||||
split into smaller buffer chunks - the chunk size being operating system dependant, but typically 64k (Linux/Mac) or 41k (Windows).</p>
|
||||
<p>When split into multiple messages, each message will have a <code>parts</code>
|
||||
property set, forming a complete message sequence.</p>
|
||||
<p>Encoding of input data can be specified from list of encodings if output format is string.</p>
|
||||
<h4>Legacy error handling</h4>
|
||||
<p>Before Node-RED 0.17, if this node hit an error whilst reading the file, it would
|
||||
send a message with no <code>msg.payload</code> and <code>msg.error</code> set to the
|
||||
|
@@ -858,6 +858,7 @@
|
||||
"filelabel": "file",
|
||||
"sendError": "エラーメッセージを送信(互換モード)",
|
||||
"deletelabel": "delete __file__",
|
||||
"encoding": "エンコーディング"
|
||||
"utf8String": "UTF8文字列",
|
||||
"binaryBuffer": "バイナリバッファ"
|
||||
},
|
||||
@@ -877,6 +878,21 @@
|
||||
"deletedfile": "ファイルを削除しました: __file__",
|
||||
"appendedfile": "ファイルへ追記しました: __file__"
|
||||
},
|
||||
"encoding": {
|
||||
"none": "デフォルト",
|
||||
"native": "ネイティブ",
|
||||
"unicode": "UNICODE",
|
||||
"japanese": "日本",
|
||||
"chinese": "中国",
|
||||
"korean": "韓国",
|
||||
"taiwan": "台湾/香港",
|
||||
"windows": "Windowsコードページ",
|
||||
"iso": "ISOコードページ",
|
||||
"ibm": "IBMコードページ",
|
||||
"mac": "Macコードページ",
|
||||
"koi8": "KOI8コードページ",
|
||||
"misc": "その他"
|
||||
},
|
||||
"errors": {
|
||||
"nofilename": "ファイル名が設定されていません",
|
||||
"invaliddelete": "警告: 削除が無効です。設定ダイアログで特定の削除設定を使用してください",
|
||||
|
@@ -27,6 +27,7 @@
|
||||
<p>入力メッセージのペイロードをファイルの最後に追記します。改行(\n)を各データの最後に追加することもできます。</p>
|
||||
<p><code>msg.filename</code>を使う場合、書き込みを行う毎にファイルをクローズします。より良い性能を得るためにはファイル名をノードに設定してください。</p>
|
||||
<p>追記を行う代わりに、ファイル全体を上書きするように設定することもできます。例えば、画像のようなバイナリデータをファイルに書き出す場合は、このオプションを指定し、改行を追記するオプションを指定しないようにします。</p>
|
||||
<p>ファイル出力の際のエンコーディングは、エンコーディングリストから選択できます。</p>
|
||||
<p>この他、ファイルの削除を行うことも可能です。</p>
|
||||
</script>
|
||||
|
||||
@@ -51,6 +52,7 @@
|
||||
<p>Windowsではパスの区切り文字を(例えば、<code>\\ユーザー\\名前</code>のように)エスケープする必要があります。</p>
|
||||
<p>テキストファイルの場合、行毎に分割して各々メッセージを送信することができます。また、バイナリファイルの場合、小さな塊のバッファに分割して送信できます。バッファの分割単位はオペレーティングシステム依存ですが、一般に64k(Linux/Mac)もしくは41k(Windows)です。</p>
|
||||
<p>複数のメッセージに分割する場合、各メッセージには<code>parts</code>プロパティが設定され、メッセージ列を構成します。</p>
|
||||
<p>主力形式が文字列の場合、入力データのエンコーディングをエンコーディングリストから選択できます。</p>
|
||||
<h4>旧式のエラー処理</h4>
|
||||
<p>Node-RED 0.17より前の版では、ファイルの読み込み時にエラーが発生すると<code>payload</code>を持たず<code>error</code>プロパティにエラーの詳細情報を設定したメッセージを送信します。この動作モードは非推奨であり、新しいノード実装ではデフォルトでは無効としています。ノードの設定により、必要に応じてこのモードを有効にできます。</p>
|
||||
<p>エラーはcatchノードで補足して処理することを推奨します。</p>
|
||||
|
@@ -36,7 +36,8 @@
|
||||
"on-headers": "1.0.2",
|
||||
"raw-body": "2.3.3",
|
||||
"request": "2.88.0",
|
||||
"ws": "6.1.4",
|
||||
"xml2js": "0.4.19"
|
||||
"ws": "6.1.3",
|
||||
"xml2js": "0.4.19",
|
||||
"iconv-lite": "0.4.24"
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user