|
@@ -4,6 +4,7 @@ const utils = require('./utils');
|
|
|
|
|
|
const FileDownloader = require('./FileDownloader');
|
|
const FileDownloader = require('./FileDownloader');
|
|
const WebSocketConnection = require('./WebSocketConnection');
|
|
const WebSocketConnection = require('./WebSocketConnection');
|
|
|
|
+const InpxHashCreator = require('./InpxHashCreator');
|
|
const log = new (require('./AppLogger'))().log;//singleton
|
|
const log = new (require('./AppLogger'))().log;//singleton
|
|
|
|
|
|
//singleton
|
|
//singleton
|
|
@@ -20,10 +21,9 @@ class RemoteLib {
|
|
|
|
|
|
this.remoteHost = config.remoteLib.url.replace(/^ws:\/\//, 'http://').replace(/^wss:\/\//, 'https://');
|
|
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.down = new FileDownloader(config.maxPayloadSize*1024*1024);
|
|
|
|
+ this.inpxHashCreator = new InpxHashCreator(config);
|
|
|
|
+ this.inpxFileHash = '';
|
|
|
|
|
|
instance = this;
|
|
instance = this;
|
|
}
|
|
}
|
|
@@ -46,17 +46,16 @@ class RemoteLib {
|
|
return response;
|
|
return response;
|
|
}
|
|
}
|
|
|
|
|
|
- async downloadInpxFile(getPeriod = 0) {
|
|
|
|
- if (getPeriod && Date.now() - this.lastUpdateTime < getPeriod)
|
|
|
|
- return this.inpxFile;
|
|
|
|
-
|
|
|
|
- const response = await this.wsRequest({action: 'get-inpx-file'});
|
|
|
|
-
|
|
|
|
- await fs.writeFile(this.inpxFile, response.data, 'base64');
|
|
|
|
|
|
+ async downloadInpxFile() {
|
|
|
|
+ if (!this.inpxFileHash)
|
|
|
|
+ this.inpxFileHash = await this.inpxHashCreator.getInpxFileHash();
|
|
|
|
|
|
- this.lastUpdateTime = Date.now();
|
|
|
|
|
|
+ const response = await this.wsRequest({action: 'get-inpx-file', inpxFileHash: this.inpxFileHash});
|
|
|
|
|
|
- return this.inpxFile;
|
|
|
|
|
|
+ if (response.data) {
|
|
|
|
+ await fs.writeFile(this.config.inpxFile, response.data, 'base64');
|
|
|
|
+ this.inpxFileHash = '';
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
async downloadBook(bookPath, downFileName) {
|
|
async downloadBook(bookPath, downFileName) {
|
|
@@ -64,17 +63,11 @@ class RemoteLib {
|
|
const response = await await this.wsRequest({action: 'get-book-link', bookPath, downFileName});
|
|
const response = await await this.wsRequest({action: 'get-book-link', bookPath, downFileName});
|
|
const link = response.link;
|
|
const link = response.link;
|
|
|
|
|
|
- const buf = await this.down.load(`${this.remoteHost}${link}`);
|
|
|
|
|
|
+ const buf = await this.down.load(`${this.remoteHost}${link}`, {decompress: false});
|
|
|
|
|
|
- const tmpFile = `${this.config.tempDir}/${utils.randomHexString(30)}`;
|
|
|
|
- const tmpFile2 = `${this.config.tempDir}/${utils.randomHexString(30)}`;
|
|
|
|
const publicPath = `${this.config.publicDir}${link}`;
|
|
const publicPath = `${this.config.publicDir}${link}`;
|
|
|
|
|
|
- await fs.writeFile(tmpFile, buf);
|
|
|
|
-
|
|
|
|
- await utils.gzipFile(tmpFile, tmpFile2, 4);
|
|
|
|
- await fs.remove(tmpFile);
|
|
|
|
- await fs.move(tmpFile2, publicPath, {overwrite: true});
|
|
|
|
|
|
+ await fs.writeFile(publicPath, buf);
|
|
|
|
|
|
return path.basename(link);
|
|
return path.basename(link);
|
|
} catch (e) {
|
|
} catch (e) {
|