diff --git a/server/controllers/WebSocketController.js b/server/controllers/WebSocketController.js index 8f0c4e8..6c2d338 100644 --- a/server/controllers/WebSocketController.js +++ b/server/controllers/WebSocketController.js @@ -1,4 +1,3 @@ -const fs = require('fs-extra'); const _ = require('lodash'); const WebSocket = require ('ws'); @@ -182,9 +181,9 @@ class WebSocketController { if (!this.config.allowRemoteLib) throw new Error('Remote lib access disabled'); - const data = await fs.readFile(this.config.inpxFile, 'base64'); + const result = await this.webWorker.getInpxFile(req); - this.send({data}, req, ws); + this.send(result, req, ws); } } diff --git a/server/core/InpxHashCreator.js b/server/core/InpxHashCreator.js index 8bda1d9..26a9b5f 100644 --- a/server/core/InpxHashCreator.js +++ b/server/core/InpxHashCreator.js @@ -23,6 +23,10 @@ class InpxHashCreator { return utils.getBufHash(joinedHash, 'sha256', 'hex'); } + + async getInpxFileHash() { + return await utils.getFileHash(this.config.inpxFile, 'sha256', 'hex'); + } } module.exports = InpxHashCreator; \ No newline at end of file diff --git a/server/core/RemoteLib.js b/server/core/RemoteLib.js index e5cd9da..5bd0238 100644 --- a/server/core/RemoteLib.js +++ b/server/core/RemoteLib.js @@ -4,6 +4,7 @@ const utils = require('./utils'); const FileDownloader = require('./FileDownloader'); const WebSocketConnection = require('./WebSocketConnection'); +const InpxHashCreator = require('./InpxHashCreator'); const log = new (require('./AppLogger'))().log;//singleton //singleton @@ -20,10 +21,9 @@ class RemoteLib { this.remoteHost = config.remoteLib.url.replace(/^ws:\/\//, 'http://').replace(/^wss:\/\//, 'https://'); - this.inpxFile = `${config.tempDir}/${utils.randomHexString(20)}`; - this.lastUpdateTime = 0; - this.down = new FileDownloader(config.maxPayloadSize*1024*1024); + this.inpxHashCreator = new InpxHashCreator(config); + this.inpxFileHash = ''; instance = this; } @@ -46,17 +46,16 @@ class RemoteLib { return response; } - async downloadInpxFile(getPeriod = 0) { - if (getPeriod && Date.now() - this.lastUpdateTime < getPeriod) - return this.inpxFile; + async downloadInpxFile() { + if (!this.inpxFileHash) + this.inpxFileHash = await this.inpxHashCreator.getInpxFileHash(); - const response = await this.wsRequest({action: 'get-inpx-file'}); + const response = await this.wsRequest({action: 'get-inpx-file', inpxFileHash: this.inpxFileHash}); - await fs.writeFile(this.inpxFile, response.data, 'base64'); - - this.lastUpdateTime = Date.now(); - - return this.inpxFile; + if (response.data) { + await fs.writeFile(this.config.inpxFile, response.data, 'base64'); + this.inpxFileHash = ''; + } } async downloadBook(bookPath, downFileName) { diff --git a/server/core/WebWorker.js b/server/core/WebWorker.js index 759ae6e..41677cf 100644 --- a/server/core/WebWorker.js +++ b/server/core/WebWorker.js @@ -43,6 +43,9 @@ class WebWorker { this.remoteLib = new RemoteLib(config); } + this.inpxHashCreator = new InpxHashCreator(config); + this.inpxFileHash = ''; + this.wState = this.workerState.getControl('server_state'); this.myState = ''; this.db = null; @@ -136,6 +139,8 @@ class WebWorker { const config = this.config; const dbPath = `${config.dataDir}/db`; + this.inpxFileHash = await this.inpxHashCreator.getInpxFileHash(); + //пересоздаем БД из INPX если нужно if (config.recreateDb || recreate) await fs.remove(dbPath); @@ -427,6 +432,18 @@ class WebWorker { } } + async getInpxFile(params) { + let data = null; + if (params.inpxFileHash && this.inpxFileHash && params.inpxFileHash === this.inpxFileHash) { + data = false; + } + + if (data === null) + data = await fs.readFile(this.config.inpxFile, 'base64'); + + return {data}; + } + logServerStats() { try { const memUsage = process.memoryUsage().rss/(1024*1024);//Mb @@ -516,18 +533,16 @@ class WebWorker { if (!inpxCheckInterval) return; - const inpxHashCreator = new InpxHashCreator(this.config); - while (1) {// eslint-disable-line no-constant-condition try { while (this.myState != ssNormal) await utils.sleep(1000); if (this.remoteLib) { - await this.remoteLib.downloadInpxFile(60*1000); + await this.remoteLib.downloadInpxFile(); } - const newInpxHash = await inpxHashCreator.getHash(); + const newInpxHash = await this.inpxHashCreator.getHash(); const dbConfig = await this.dbConfig(); const currentInpxHash = (dbConfig.inpxHash ? dbConfig.inpxHash : ''); diff --git a/server/index.js b/server/index.js index 9f49286..5eeb084 100644 --- a/server/index.js +++ b/server/index.js @@ -114,9 +114,10 @@ async function init() { } } } else { + config.inpxFile = `${config.tempDir}/${utils.randomHexString(20)}`; const RemoteLib = require('./core/RemoteLib');//singleton const remoteLib = new RemoteLib(config); - config.inpxFile = await remoteLib.downloadInpxFile(); + await remoteLib.downloadInpxFile(); } config.recreateDb = argv.recreate || false;