浏览代码

Работа над карточкой "Информация о книге"

Book Pauk 2 年之前
父节点
当前提交
02f276ca6b
共有 2 个文件被更改,包括 63 次插入5 次删除
  1. 26 5
      server/core/WebWorker.js
  2. 37 0
      server/core/utils.js

+ 26 - 5
server/core/WebWorker.js

@@ -15,6 +15,7 @@ const ayncExit = new (require('./AsyncExit'))();
 const log = new (require('./AppLogger'))().log;//singleton
 const log = new (require('./AppLogger'))().log;//singleton
 const utils = require('./utils');
 const utils = require('./utils');
 const genreTree = require('./genres');
 const genreTree = require('./genres');
+const Fb2Parser = require('./xml/Fb2Parser');
 
 
 //server states
 //server states
 const ssNormal = 'normal';
 const ssNormal = 'normal';
@@ -44,6 +45,7 @@ class WebWorker {
             }
             }
             
             
             this.inpxHashCreator = new InpxHashCreator(config);
             this.inpxHashCreator = new InpxHashCreator(config);
+            this.fb2Parser = new Fb2Parser();
             this.inpxFileHash = '';
             this.inpxFileHash = '';
 
 
             this.wState = this.workerState.getControl('server_state');
             this.wState = this.workerState.getControl('server_state');
@@ -462,12 +464,31 @@ class WebWorker {
         try {
         try {
             const db = this.db;
             const db = this.db;
 
 
-            const bookInfo = await this.getBookLink(bookId);
+            let bookInfo = await this.getBookLink(bookId);
+            const hash = path.basename(bookInfo.link);
+            const bookFile = `${this.config.filesDir}/${hash}`;
+            const bookFileInfo = `${bookFile}.info`;
 
 
-            const rows = await db.select({table: 'book', where: `@@id(${db.esc(bookId)})`});
-            bookInfo.book = rows[0];
-            bookInfo.fb2 = {};
-            bookInfo.cover = '';
+            if (!await fs.pathExists(bookFileInfo)) {
+                const rows = await db.select({table: 'book', where: `@@id(${db.esc(bookId)})`});
+                const book = rows[0];
+
+                let fb2 = false;
+                if (book.ext == 'fb2') {
+                    const parsedFb2 = await this.fb2Parser.getDescAndCover(bookFile);
+                    fb2 = parsedFb2;
+                }
+
+                bookInfo.book = book;
+                bookInfo.fb2 = fb2;
+                bookInfo.cover = '';
+
+                await fs.writeFile(bookFileInfo, JSON.stringify(bookInfo, null, 2));
+            } else {
+                await utils.touchFile(bookFileInfo);
+                const info = fs.readFile(bookFileInfo, 'utf-8');
+                bookInfo = JSON.parse(info);
+            }
 
 
             return {bookInfo};
             return {bookInfo};
         } catch(e) {
         } catch(e) {

+ 37 - 0
server/core/utils.js

@@ -115,6 +115,40 @@ function gzipFile(inputFile, outputFile, level = 1) {
     });
     });
 }
 }
 
 
+function gunzipFile(inputFile, outputFile) {
+    return new Promise((resolve, reject) => {
+        const gzip = zlib.createGunzip();
+        const input = fs.createReadStream(inputFile);
+        const output = fs.createWriteStream(outputFile);
+
+        input.on('error', reject)
+            .pipe(gzip).on('error', reject)
+            .pipe(output).on('error', reject)
+            .on('finish', (err) => {
+            if (err) reject(err);
+            else resolve();
+        });
+    });
+}
+
+function gzipBuffer(buf) {
+    return new Promise((resolve, reject) => {
+        zlib.gzip(buf, {level: 1}, (err, result) => {
+            if (err) reject(err);
+            resolve(result);
+        });
+    });
+}
+
+function gunzipBuffer(buf) {
+    return new Promise((resolve, reject) => {
+        zlib.gunzip(buf, (err, result) => {
+            if (err) reject(err);
+            resolve(result);
+        });
+    });
+}
+
 function toUnixPath(dir) {
 function toUnixPath(dir) {
     return dir.replace(/\\/g, '/');
     return dir.replace(/\\/g, '/');
 }
 }
@@ -153,6 +187,9 @@ module.exports = {
     intersectSet,
     intersectSet,
     randomHexString,
     randomHexString,
     gzipFile,
     gzipFile,
+    gunzipFile,
+    gzipBuffer,
+    gunzipBuffer,
     toUnixPath,
     toUnixPath,
     makeValidFileName,
     makeValidFileName,
     makeValidFileNameOrEmpty,
     makeValidFileNameOrEmpty,