|
@@ -1,7 +1,10 @@
|
|
const fs = require('fs-extra');
|
|
const fs = require('fs-extra');
|
|
|
|
+const path = require('path');
|
|
const utils = require('./utils');
|
|
const utils = require('./utils');
|
|
|
|
|
|
|
|
+const FileDownloader = require('./FileDownloader');
|
|
const WebSocketConnection = require('./WebSocketConnection');
|
|
const WebSocketConnection = require('./WebSocketConnection');
|
|
|
|
+const log = new (require('./AppLogger'))().log;//singleton
|
|
|
|
|
|
//singleton
|
|
//singleton
|
|
let instance = null;
|
|
let instance = null;
|
|
@@ -15,9 +18,13 @@ class RemoteLib {
|
|
if (config.remoteLib.accessPassword)
|
|
if (config.remoteLib.accessPassword)
|
|
this.accessToken = utils.getBufHash(config.remoteLib.accessPassword, 'sha256', 'hex');
|
|
this.accessToken = utils.getBufHash(config.remoteLib.accessPassword, 'sha256', 'hex');
|
|
|
|
|
|
|
|
+ this.remoteHost = config.remoteLib.url.replace(/^ws:\/\//, 'http://').replace(/^wss:\/\//, 'https://');
|
|
|
|
+
|
|
this.inpxFile = `${config.tempDir}/${utils.randomHexString(20)}`;
|
|
this.inpxFile = `${config.tempDir}/${utils.randomHexString(20)}`;
|
|
this.lastUpdateTime = 0;
|
|
this.lastUpdateTime = 0;
|
|
|
|
|
|
|
|
+ this.down = new FileDownloader(config.maxPayloadSize*1024*1024);
|
|
|
|
+
|
|
instance = this;
|
|
instance = this;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -29,8 +36,8 @@ class RemoteLib {
|
|
query.accessToken = this.accessToken;
|
|
query.accessToken = this.accessToken;
|
|
|
|
|
|
const response = await this.wsc.message(
|
|
const response = await this.wsc.message(
|
|
- await this.wsc.send(query, 60),
|
|
|
|
- 60
|
|
|
|
|
|
+ await this.wsc.send(query),
|
|
|
|
+ 120
|
|
);
|
|
);
|
|
|
|
|
|
if (response.error)
|
|
if (response.error)
|
|
@@ -39,7 +46,7 @@ class RemoteLib {
|
|
return response;
|
|
return response;
|
|
}
|
|
}
|
|
|
|
|
|
- async getInpxFile(getPeriod = 0) {
|
|
|
|
|
|
+ async downloadInpxFile(getPeriod = 0) {
|
|
if (getPeriod && Date.now() - this.lastUpdateTime < getPeriod)
|
|
if (getPeriod && Date.now() - this.lastUpdateTime < getPeriod)
|
|
return this.inpxFile;
|
|
return this.inpxFile;
|
|
|
|
|
|
@@ -51,6 +58,23 @@ class RemoteLib {
|
|
|
|
|
|
return this.inpxFile;
|
|
return this.inpxFile;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ async downloadBook(bookPath, downFileName) {
|
|
|
|
+ try {
|
|
|
|
+ const response = await await this.wsRequest({action: 'get-book-link', bookPath, downFileName});
|
|
|
|
+ const link = response.link;
|
|
|
|
+
|
|
|
|
+ const buf = await this.down.load(`${this.remoteHost}${link}`);
|
|
|
|
+
|
|
|
|
+ const publicPath = `${this.config.publicDir}${link}`;
|
|
|
|
+ await fs.writeFile(publicPath, buf);
|
|
|
|
+
|
|
|
|
+ return path.basename(link);
|
|
|
|
+ } catch (e) {
|
|
|
|
+ log(LM_ERR, `RemoteLib.downloadBook: ${e.message}`);
|
|
|
|
+ throw new Error('502 Bad Gateway');
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
module.exports = RemoteLib;
|
|
module.exports = RemoteLib;
|