From 9a44f53e5fd2ba0b0b90da1427ad5befd97652bc Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Wed, 13 Feb 2019 13:39:53 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=80=D0=B0=D1=81=D0=BF=D0=BE=D0=B7=D0=BD=D0=B0=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=B4=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D0=B8=20fb2=20=D0=B8=20=D0=BA=D0=BE=D0=BD=D0=B2=D0=B5?= =?UTF-8?q?=D1=80=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B2=20utf8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/BookConverter/index.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/server/core/BookConverter/index.js b/server/core/BookConverter/index.js index 1a6adaf9..a87bbdbe 100644 --- a/server/core/BookConverter/index.js +++ b/server/core/BookConverter/index.js @@ -22,8 +22,8 @@ class BookConverter { callback(100); if (fileType && (fileType.ext == 'html' || fileType.ext == 'xml')) { - if (data.toString().indexOf('= 0) { - await fs.writeFile(outputFile, data); + if (data.toString().indexOf('= 0) { + await fs.writeFile(outputFile, this.checkEncoding(data)); return; } @@ -69,6 +69,26 @@ class BookConverter { return iconv.decode(data, selected); } + checkEncoding(data) { + let result = data; + + const left = data.indexOf('= 0) { + const right = data.indexOf('?>', left); + if (right >= 0) { + const head = data.slice(left, right + 2).toString(); + const m = head.match(/encoding="(.*)"/); + if (m) { + let encoding = m[1].toLowerCase(); + if (encoding != 'utf-8') + result = iconv.decode(data, encoding); + } + } + } + + return result; + } + convertHtml(data, isText) { let titleInfo = {}; let desc = {_n: 'description', 'title-info': titleInfo};