WebWorker.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. const os = require('os');
  2. const path = require('path');
  3. const fs = require('fs-extra');
  4. const _ = require('lodash');
  5. const iconv = require('iconv-lite');
  6. const ZipReader = require('./ZipReader');
  7. const WorkerState = require('./WorkerState');//singleton
  8. const { JembaDb, JembaDbThread } = require('jembadb');
  9. const DbCreator = require('./DbCreator');
  10. const DbSearcher = require('./DbSearcher');
  11. const InpxHashCreator = require('./InpxHashCreator');
  12. const RemoteLib = require('./RemoteLib');//singleton
  13. const FileDownloader = require('./FileDownloader');
  14. const asyncExit = new (require('./AsyncExit'))();
  15. const log = new (require('./AppLogger'))().log;//singleton
  16. const utils = require('./utils');
  17. const genreTree = require('./genres');
  18. const Fb2Helper = require('./fb2/Fb2Helper');
  19. //server states
  20. const ssNormal = 'normal';
  21. const ssDbLoading = 'db_loading';
  22. const ssDbCreating = 'db_creating';
  23. const stateToText = {
  24. [ssNormal]: '',
  25. [ssDbLoading]: 'Загрузка поисковой базы',
  26. [ssDbCreating]: 'Создание поисковой базы',
  27. };
  28. const cleanDirInterval = 60*60*1000;//каждый час
  29. const checkReleaseInterval = 7*60*60*1000;//каждые 7 часов
  30. //singleton
  31. let instance = null;
  32. class WebWorker {
  33. constructor(config) {
  34. if (!instance) {
  35. this.config = config;
  36. this.workerState = new WorkerState();
  37. this.remoteLib = null;
  38. if (config.remoteLib) {
  39. this.remoteLib = new RemoteLib(config);
  40. }
  41. this.inpxHashCreator = new InpxHashCreator(config);
  42. this.fb2Helper = new Fb2Helper();
  43. this.inpxFileHash = '';
  44. this.wState = this.workerState.getControl('server_state');
  45. this.myState = '';
  46. this.db = null;
  47. this.dbSearcher = null;
  48. asyncExit.add(this.closeDb.bind(this));
  49. this.loadOrCreateDb();//no await
  50. this.periodicLogServerStats();//no await
  51. const dirConfig = [
  52. {
  53. dir: config.bookDir,
  54. maxSize: config.maxFilesDirSize,
  55. },
  56. ];
  57. this.periodicCleanDir(dirConfig);//no await
  58. this.periodicCheckInpx();//no await
  59. this.periodicCheckNewRelease();//no await
  60. instance = this;
  61. }
  62. return instance;
  63. }
  64. checkMyState() {
  65. if (this.myState != ssNormal)
  66. throw new Error('server_busy');
  67. }
  68. setMyState(newState, workerState = {}) {
  69. this.myState = newState;
  70. this.wState.set(Object.assign({}, workerState, {
  71. state: newState,
  72. serverMessage: stateToText[newState]
  73. }));
  74. }
  75. async closeDb() {
  76. if (this.db) {
  77. await this.db.unlock();
  78. this.db = null;
  79. }
  80. }
  81. async createDb(dbPath) {
  82. this.setMyState(ssDbCreating);
  83. log('Searcher DB create start');
  84. const config = this.config;
  85. if (await fs.pathExists(dbPath))
  86. throw new Error(`createDb.pathExists: ${dbPath}`);
  87. const db = new JembaDbThread();
  88. await db.lock({
  89. dbPath,
  90. create: true,
  91. softLock: true,
  92. tableDefaults: {
  93. cacheSize: config.dbCacheSize,
  94. },
  95. });
  96. try {
  97. const dbCreator = new DbCreator(config);
  98. await dbCreator.run(db, (state) => {
  99. this.setMyState(ssDbCreating, state);
  100. if (state.fileName)
  101. log(` load ${state.fileName}`);
  102. if (state.recsLoaded)
  103. log(` processed ${state.recsLoaded} records`);
  104. if (state.job)
  105. log(` ${state.job}`);
  106. });
  107. log('Searcher DB successfully created');
  108. } finally {
  109. await db.unlock();
  110. }
  111. }
  112. async loadOrCreateDb(recreate = false, iteration = 0) {
  113. this.setMyState(ssDbLoading);
  114. try {
  115. const config = this.config;
  116. const dbPath = `${config.dataDir}/db`;
  117. this.inpxFileHash = await this.inpxHashCreator.getInpxFileHash();
  118. //проверим полный InxpHash (включая фильтр и версию БД)
  119. //для этого заглянем в конфиг внутри БД, если он есть
  120. if (!(config.recreateDb || recreate) && await fs.pathExists(dbPath)) {
  121. const newInpxHash = await this.inpxHashCreator.getHash();
  122. const tmpDb = new JembaDb();
  123. await tmpDb.lock({dbPath, softLock: true});
  124. try {
  125. await tmpDb.open({table: 'config'});
  126. const rows = await tmpDb.select({table: 'config', where: `@@id('inpxHash')`});
  127. if (!rows.length || newInpxHash !== rows[0].value)
  128. throw new Error('inpx file: changes found on start, recreating DB');
  129. } catch (e) {
  130. log(LM_WARN, e.message);
  131. recreate = true;
  132. } finally {
  133. await tmpDb.unlock();
  134. }
  135. }
  136. //удалим БД если нужно
  137. if (config.recreateDb || recreate)
  138. await fs.remove(dbPath);
  139. //пересоздаем БД из INPX если нужно
  140. if (!await fs.pathExists(dbPath)) {
  141. await this.createDb(dbPath);
  142. utils.freeMemory();
  143. }
  144. //загружаем БД
  145. this.setMyState(ssDbLoading);
  146. log('Searcher DB loading');
  147. const db = new JembaDbThread();//в отдельном потоке
  148. await db.lock({
  149. dbPath,
  150. softLock: true,
  151. tableDefaults: {
  152. cacheSize: config.dbCacheSize,
  153. },
  154. });
  155. try {
  156. //открываем таблицы
  157. await db.openAll({exclude: ['author_id', 'series_id', 'title_id', 'book']});
  158. const bookCacheSize = 500;
  159. await db.open({
  160. table: 'book',
  161. cacheSize: (config.lowMemoryMode || config.dbCacheSize > bookCacheSize ? config.dbCacheSize : bookCacheSize)
  162. });
  163. } catch(e) {
  164. log(LM_ERR, `Database error: ${e.message}`);
  165. if (iteration < 1) {
  166. log('Recreating DB');
  167. await this.loadOrCreateDb(true, iteration + 1);
  168. } else
  169. throw e;
  170. return;
  171. }
  172. //поисковый движок
  173. this.dbSearcher = new DbSearcher(config, db);
  174. await this.dbSearcher.init();
  175. //stuff
  176. db.wwCache = {};
  177. this.db = db;
  178. this.setMyState(ssNormal);
  179. log('Searcher DB ready');
  180. this.logServerStats();
  181. } catch (e) {
  182. log(LM_FATAL, e.message);
  183. asyncExit.exit(1);
  184. }
  185. }
  186. async recreateDb() {
  187. this.setMyState(ssDbCreating);
  188. if (this.dbSearcher) {
  189. await this.dbSearcher.close();
  190. this.dbSearcher = null;
  191. }
  192. await this.closeDb();
  193. await this.loadOrCreateDb(true);
  194. }
  195. async dbConfig() {
  196. this.checkMyState();
  197. const db = this.db;
  198. if (!db.wwCache.config) {
  199. const rows = await db.select({table: 'config'});
  200. const config = {};
  201. for (const row of rows) {
  202. config[row.id] = row.value;
  203. }
  204. db.wwCache.config = config;
  205. }
  206. return db.wwCache.config;
  207. }
  208. async search(from, query) {
  209. this.checkMyState();
  210. const result = await this.dbSearcher.search(from, query);
  211. const config = await this.dbConfig();
  212. result.inpxHash = (config.inpxHash ? config.inpxHash : '');
  213. return result;
  214. }
  215. async bookSearch(query) {
  216. this.checkMyState();
  217. const result = await this.dbSearcher.bookSearch(query);
  218. const config = await this.dbConfig();
  219. result.inpxHash = (config.inpxHash ? config.inpxHash : '');
  220. return result;
  221. }
  222. async opdsQuery(from, query) {
  223. this.checkMyState();
  224. return await this.dbSearcher.opdsQuery(from, query);
  225. }
  226. async getAuthorBookList(authorId, author) {
  227. this.checkMyState();
  228. return await this.dbSearcher.getAuthorBookList(authorId, author);
  229. }
  230. async getAuthorSeriesList(authorId) {
  231. this.checkMyState();
  232. return await this.dbSearcher.getAuthorSeriesList(authorId);
  233. }
  234. async getSeriesBookList(series) {
  235. this.checkMyState();
  236. return await this.dbSearcher.getSeriesBookList(series);
  237. }
  238. async getGenreTree() {
  239. this.checkMyState();
  240. const config = await this.dbConfig();
  241. let result;
  242. const db = this.db;
  243. if (!db.wwCache.genres) {
  244. const genres = _.cloneDeep(genreTree);
  245. const last = genres[genres.length - 1];
  246. const genreValues = new Set();
  247. for (const section of genres) {
  248. for (const g of section.value)
  249. genreValues.add(g.value);
  250. }
  251. //добавим к жанрам те, что нашлись при парсинге
  252. const genreParsed = new Set();
  253. let rows = await db.select({table: 'genre', map: `(r) => ({value: r.value})`});
  254. for (const row of rows) {
  255. genreParsed.add(row.value);
  256. if (!genreValues.has(row.value))
  257. last.value.push({name: row.value, value: row.value});
  258. }
  259. //уберем те, которые не нашлись при парсинге
  260. for (let j = 0; j < genres.length; j++) {
  261. const section = genres[j];
  262. for (let i = 0; i < section.value.length; i++) {
  263. const g = section.value[i];
  264. if (!genreParsed.has(g.value))
  265. section.value.splice(i--, 1);
  266. }
  267. if (!section.value.length)
  268. genres.splice(j--, 1);
  269. }
  270. // langs
  271. rows = await db.select({table: 'lang', map: `(r) => ({value: r.value})`});
  272. const langs = rows.map(r => r.value);
  273. // exts
  274. rows = await db.select({table: 'ext', map: `(r) => ({value: r.value})`});
  275. const exts = rows.map(r => r.value);
  276. result = {
  277. genreTree: genres,
  278. langList: langs,
  279. extList: exts,
  280. inpxHash: (config.inpxHash ? config.inpxHash : ''),
  281. };
  282. db.wwCache.genres = result;
  283. } else {
  284. result = db.wwCache.genres;
  285. }
  286. return result;
  287. }
  288. async extractBook(libFolder, libFile) {
  289. const outFile = `${this.config.tempDir}/${utils.randomHexString(30)}`;
  290. libFolder = libFolder.replace(/\\/g, '/').replace(/\/\//g, '/');
  291. const folder = `${this.config.libDir}/${libFolder}`;
  292. const file = libFile;
  293. const fullPath = `${folder}/${file}`;
  294. if (!file || await fs.pathExists(fullPath)) {// файл есть на диске
  295. await fs.copy(fullPath, outFile);
  296. return outFile;
  297. } else {// файл в zip-архиве
  298. const zipReader = new ZipReader();
  299. await zipReader.open(folder);
  300. try {
  301. await zipReader.extractToFile(file, outFile);
  302. if (!await fs.pathExists(outFile)) {//не удалось найти в архиве, попробуем имя файла в кодировке cp866
  303. await zipReader.extractToFile(iconv.encode(file, 'cp866').toString(), outFile);
  304. }
  305. return outFile;
  306. } finally {
  307. await zipReader.close();
  308. }
  309. }
  310. }
  311. async restoreBook(bookUid, libFolder, libFile, downFileName) {
  312. const db = this.db;
  313. let extractedFile = '';
  314. let hash = '';
  315. if (!this.remoteLib) {
  316. extractedFile = await this.extractBook(libFolder, libFile);
  317. hash = await utils.getFileHash(extractedFile, 'sha256', 'hex');
  318. } else {
  319. hash = await this.remoteLib.downloadBook(bookUid);
  320. }
  321. const link = `${this.config.bookPathStatic}/${hash}`;
  322. const bookFile = `${this.config.bookDir}/${hash}`;
  323. const bookFileDesc = `${bookFile}.d.json`;
  324. if (!await fs.pathExists(bookFile) || !await fs.pathExists(bookFileDesc)) {
  325. if (!await fs.pathExists(bookFile) && extractedFile) {
  326. const tmpFile = `${this.config.tempDir}/${utils.randomHexString(30)}`;
  327. await utils.gzipFile(extractedFile, tmpFile, 4);
  328. await fs.remove(extractedFile);
  329. await fs.move(tmpFile, bookFile, {overwrite: true});
  330. } else {
  331. await utils.touchFile(bookFile);
  332. }
  333. await fs.writeFile(bookFileDesc, JSON.stringify({libFolder, libFile, downFileName}));
  334. } else {
  335. if (extractedFile)
  336. await fs.remove(extractedFile);
  337. await utils.touchFile(bookFile);
  338. await utils.touchFile(bookFileDesc);
  339. }
  340. await db.insert({
  341. table: 'file_hash',
  342. replace: true,
  343. rows: [
  344. {id: bookUid, hash},
  345. ]
  346. });
  347. return link;
  348. }
  349. async getBookLink(bookUid) {
  350. this.checkMyState();
  351. try {
  352. const db = this.db;
  353. let link = '';
  354. //найдем downFileName, libFolder, libFile
  355. let rows = await db.select({table: 'book', where: `@@hash('_uid', ${db.esc(bookUid)})`});
  356. if (!rows.length)
  357. throw new Error('404 Файл не найден');
  358. const book = rows[0];
  359. let downFileName = book.file;
  360. const authors = book.author.split(',');
  361. let author = authors[0];
  362. author = author.split(' ').filter(r => r.trim());
  363. for (let i = 1; i < author.length; i++)
  364. author[i] = `${(i === 1 ? ' ' : '')}${author[i][0]}.`;
  365. if (authors.length > 1)
  366. author.push(' и др.');
  367. const at = [author.join(''), (book.title ? `_${book.title}` : '')];
  368. downFileName = utils.makeValidFileNameOrEmpty(at.filter(r => r).join(''))
  369. || utils.makeValidFileNameOrEmpty(at[0])
  370. || utils.makeValidFileNameOrEmpty(at[1])
  371. || downFileName;
  372. if (downFileName.length > 50)
  373. downFileName = `${downFileName.substring(0, 50)}_`;
  374. const ext = `.${book.ext}`;
  375. if (downFileName.substring(downFileName.length - ext.length) != ext)
  376. downFileName += ext;
  377. const libFolder = book.folder;
  378. const libFile = `${book.file}${ext}`;
  379. //найдем хеш
  380. rows = await db.select({table: 'file_hash', where: `@@id(${db.esc(bookUid)})`});
  381. if (rows.length) {//хеш найден по bookUid
  382. const hash = rows[0].hash;
  383. const bookFile = `${this.config.bookDir}/${hash}`;
  384. const bookFileDesc = `${bookFile}.d.json`;
  385. if (await fs.pathExists(bookFile) && await fs.pathExists(bookFileDesc)) {
  386. link = `${this.config.bookPathStatic}/${hash}`;
  387. }
  388. }
  389. if (!link) {
  390. link = await this.restoreBook(bookUid, libFolder, libFile, downFileName);
  391. }
  392. if (!link)
  393. throw new Error('404 Файл не найден');
  394. return {link, libFolder, libFile, downFileName};
  395. } catch(e) {
  396. log(LM_ERR, `getBookLink error: ${e.message}`);
  397. if (e.message.indexOf('ENOENT') >= 0)
  398. throw new Error('404 Файл не найден');
  399. throw e;
  400. }
  401. }
  402. async getBookInfo(bookUid) {
  403. this.checkMyState();
  404. try {
  405. const db = this.db;
  406. let bookInfo = await this.getBookLink(bookUid);
  407. const hash = path.basename(bookInfo.link);
  408. const bookFile = `${this.config.bookDir}/${hash}`;
  409. const bookFileInfo = `${bookFile}.i.json`;
  410. let rows = await db.select({table: 'book', where: `@@hash('_uid', ${db.esc(bookUid)})`});
  411. if (!rows.length)
  412. throw new Error('404 Файл не найден');
  413. const book = rows[0];
  414. const restoreBookInfo = async(info) => {
  415. const result = {};
  416. result.book = book;
  417. result.cover = '';
  418. result.fb2 = false;
  419. let parser = null;
  420. if (book.ext == 'fb2') {
  421. const {fb2, cover, coverExt} = await this.fb2Helper.getDescAndCover(bookFile);
  422. parser = fb2;
  423. result.fb2 = fb2.rawNodes;
  424. if (cover) {
  425. result.cover = `${this.config.bookPathStatic}/${hash}${coverExt}`;
  426. await fs.writeFile(`${bookFile}${coverExt}`, cover);
  427. }
  428. }
  429. Object.assign(info, result);
  430. await fs.writeFile(bookFileInfo, JSON.stringify(info));
  431. if (this.config.branch === 'development') {
  432. await fs.writeFile(`${bookFile}.dev`, `${JSON.stringify(info, null, 2)}\n\n${parser ? parser.toString({format: true}) : ''}`);
  433. }
  434. };
  435. if (!await fs.pathExists(bookFileInfo)) {
  436. await restoreBookInfo(bookInfo);
  437. } else {
  438. await utils.touchFile(bookFileInfo);
  439. const info = await fs.readFile(bookFileInfo, 'utf-8');
  440. const tmpInfo = JSON.parse(info);
  441. //проверим существование файла обложки, восстановим если нету
  442. let coverFile = '';
  443. if (tmpInfo.cover)
  444. coverFile = `${this.config.publicFilesDir}${tmpInfo.cover}`;
  445. if (book.id != tmpInfo.book.id || (coverFile && !await fs.pathExists(coverFile))) {
  446. await restoreBookInfo(bookInfo);
  447. } else {
  448. bookInfo = tmpInfo;
  449. }
  450. }
  451. return {bookInfo};
  452. } catch(e) {
  453. log(LM_ERR, `getBookInfo error: ${e.message}`);
  454. if (e.message.indexOf('ENOENT') >= 0)
  455. throw new Error('404 Файл не найден');
  456. throw e;
  457. }
  458. }
  459. async getInpxFile(params) {
  460. let data = null;
  461. if (params.inpxFileHash && this.inpxFileHash && params.inpxFileHash === this.inpxFileHash) {
  462. data = false;
  463. }
  464. if (data === null)
  465. data = await fs.readFile(this.config.inpxFile, 'base64');
  466. return {data};
  467. }
  468. logServerStats() {
  469. try {
  470. const memUsage = process.memoryUsage().rss/(1024*1024);//Mb
  471. let loadAvg = os.loadavg();
  472. loadAvg = loadAvg.map(v => v.toFixed(2));
  473. log(`Server info [ memUsage: ${memUsage.toFixed(2)}MB, loadAvg: (${loadAvg.join(', ')}) ]`);
  474. if (this.config.server.ready)
  475. log(`Server accessible at http://127.0.0.1:${this.config.server.port} (listening on ${this.config.server.host}:${this.config.server.port})`);
  476. } catch (e) {
  477. log(LM_ERR, e.message);
  478. }
  479. }
  480. async periodicLogServerStats() {
  481. while (1) {// eslint-disable-line
  482. this.logServerStats();
  483. await utils.sleep(60*1000);
  484. }
  485. }
  486. async cleanDir(config) {
  487. const {dir, maxSize} = config;
  488. const list = await fs.readdir(dir);
  489. let size = 0;
  490. let files = [];
  491. //формируем список
  492. for (const filename of list) {
  493. const filePath = `${dir}/${filename}`;
  494. const stat = await fs.stat(filePath);
  495. if (!stat.isDirectory()) {
  496. size += stat.size;
  497. files.push({name: filePath, stat});
  498. }
  499. }
  500. log(LM_WARN, `clean dir ${dir}, maxSize=${maxSize}, found ${files.length} files, total size=${size}`);
  501. files.sort((a, b) => a.stat.mtimeMs - b.stat.mtimeMs);
  502. let i = 0;
  503. //удаляем
  504. while (i < files.length && size > maxSize) {
  505. const file = files[i];
  506. const oldFile = file.name;
  507. await fs.remove(oldFile);
  508. size -= file.stat.size;
  509. i++;
  510. }
  511. log(LM_WARN, `removed ${i} files`);
  512. }
  513. async periodicCleanDir(dirConfig) {
  514. try {
  515. for (const config of dirConfig)
  516. await fs.ensureDir(config.dir);
  517. let lastCleanDirTime = 0;
  518. while (1) {// eslint-disable-line no-constant-condition
  519. //чистка папок
  520. if (Date.now() - lastCleanDirTime >= cleanDirInterval) {
  521. for (const config of dirConfig) {
  522. try {
  523. await this.cleanDir(config);
  524. } catch(e) {
  525. log(LM_ERR, e.stack);
  526. }
  527. }
  528. lastCleanDirTime = Date.now();
  529. }
  530. await utils.sleep(60*1000);//интервал проверки 1 минута
  531. }
  532. } catch (e) {
  533. log(LM_FATAL, e.message);
  534. asyncExit.exit(1);
  535. }
  536. }
  537. async periodicCheckInpx() {
  538. const inpxCheckInterval = this.config.inpxCheckInterval;
  539. if (!inpxCheckInterval)
  540. return;
  541. while (1) {// eslint-disable-line no-constant-condition
  542. try {
  543. while (this.myState != ssNormal)
  544. await utils.sleep(1000);
  545. if (this.remoteLib) {
  546. await this.remoteLib.downloadInpxFile();
  547. }
  548. const newInpxHash = await this.inpxHashCreator.getHash();
  549. const dbConfig = await this.dbConfig();
  550. const currentInpxHash = (dbConfig.inpxHash ? dbConfig.inpxHash : '');
  551. if (newInpxHash !== currentInpxHash) {
  552. log('inpx file: changes found, recreating DB');
  553. await this.recreateDb();
  554. } else {
  555. log('inpx file: no changes');
  556. }
  557. } catch(e) {
  558. log(LM_ERR, `periodicCheckInpx: ${e.message}`);
  559. }
  560. await utils.sleep(inpxCheckInterval*60*1000);
  561. }
  562. }
  563. async periodicCheckNewRelease() {
  564. const checkReleaseLink = this.config.checkReleaseLink;
  565. if (!checkReleaseLink)
  566. return;
  567. const down = new FileDownloader(1024*1024);
  568. while (1) {// eslint-disable-line no-constant-condition
  569. try {
  570. let release = await down.load(checkReleaseLink);
  571. release = JSON.parse(release.toString());
  572. if (release.tag_name)
  573. this.config.latestVersion = release.tag_name;
  574. } catch(e) {
  575. log(LM_ERR, `periodicCheckNewRelease: ${e.message}`);
  576. }
  577. await utils.sleep(checkReleaseInterval);
  578. }
  579. }
  580. }
  581. module.exports = WebWorker;