Procházet zdrojové kódy

Добавил распознавание кодировки fb2 и конвертирование в utf8

Book Pauk před 6 roky
rodič
revize
9a44f53e5f
1 změnil soubory, kde provedl 22 přidání a 2 odebrání
  1. 22 2
      server/core/BookConverter/index.js

+ 22 - 2
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('<FictionBook') >= 0) {            
-                await fs.writeFile(outputFile, data);
+            if (data.toString().indexOf('<FictionBook') >= 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('<?xml version="1.0"');
+        if (left >= 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};