diff --git a/server/core/WebWorker.js b/server/core/WebWorker.js index 28459e2..0cb502f 100644 --- a/server/core/WebWorker.js +++ b/server/core/WebWorker.js @@ -15,6 +15,7 @@ const ayncExit = new (require('./AsyncExit'))(); const log = new (require('./AppLogger'))().log;//singleton const utils = require('./utils'); const genreTree = require('./genres'); +const Fb2Parser = require('./xml/Fb2Parser'); //server states const ssNormal = 'normal'; @@ -44,6 +45,7 @@ class WebWorker { } this.inpxHashCreator = new InpxHashCreator(config); + this.fb2Parser = new Fb2Parser(); this.inpxFileHash = ''; this.wState = this.workerState.getControl('server_state'); @@ -462,12 +464,31 @@ class WebWorker { try { 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}; } catch(e) { diff --git a/server/core/utils.js b/server/core/utils.js index 2b81ab6..856d334 100644 --- a/server/core/utils.js +++ b/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) { return dir.replace(/\\/g, '/'); } @@ -153,6 +187,9 @@ module.exports = { intersectSet, randomHexString, gzipFile, + gunzipFile, + gzipBuffer, + gunzipBuffer, toUnixPath, makeValidFileName, makeValidFileNameOrEmpty,