Browse Source

Улучшено распознавание xml-формата

Book Pauk 4 years ago
parent
commit
8ef6551560

+ 5 - 0
server/core/Reader/BookConverter/ConvertBase.js

@@ -103,6 +103,11 @@ class ConvertBase {
         return he.escape(he.decode(text.replace(/ /g, ' ')));
     }
 
+    isDataXml(data) {
+        const str = data.toString().trim();
+        return (str.indexOf('<?xml version="1.0"') == 0 || str.indexOf('<?xml version=\'1.0\'') == 0 );
+    }
+
     formatFb2(fb2) {
         const out = xmlParser.formatXml({
             FictionBook: {

+ 4 - 1
server/core/Reader/BookConverter/ConvertFb2.js

@@ -6,7 +6,10 @@ class ConvertFb2 extends ConvertBase {
     check(data, opts) {
         const {dataType} = opts;
 
-        return (dataType && dataType.ext == 'xml' && data.toString().indexOf('<FictionBook') >= 0);
+        return (
+            ( (dataType && dataType.ext == 'xml') || this.isDataXml(data) ) && 
+            data.toString().indexOf('<FictionBook') >= 0
+        );
     }
 
     async run(data, opts) {

+ 1 - 1
server/core/Reader/BookConverter/ConvertHtml.js

@@ -7,7 +7,7 @@ class ConvertHtml extends ConvertBase {
         const {dataType} = opts;
 
         //html?
-        if (dataType && (dataType.ext == 'html' || dataType.ext == 'xml')) 
+        if ( ( (dataType && (dataType.ext == 'html' || dataType.ext == 'xml')) ) || this.isDataXml(data) )
             return {isText: false};
 
         //может это чистый текст?