ReaderWorker.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. const fs = require('fs-extra');
  2. const path = require('path');
  3. const LimitedQueue = require('../LimitedQueue');
  4. const WorkerState = require('../WorkerState');//singleton
  5. const FileDownloader = require('../FileDownloader');
  6. const FileDecompressor = require('../FileDecompressor');
  7. const BookConverter = require('./BookConverter');
  8. const RemoteStorage = require('../RemoteStorage');
  9. const JembaConnManager = require('../../db/JembaConnManager');//singleton
  10. const ayncExit = new (require('../AsyncExit'))();
  11. const utils = require('../utils');
  12. const log = new (require('../AppLogger'))().log;//singleton
  13. const cleanDirPeriod = 30*60*1000;//раз в полчаса
  14. const queue = new LimitedQueue(5, 100, 2*60*1000 + 15000);//2 минуты ожидание подвижек
  15. let instance = null;
  16. //singleton
  17. class ReaderWorker {
  18. constructor(config) {
  19. if (!instance) {
  20. this.config = Object.assign({}, config);
  21. this.config.tempDownloadDir = `${config.tempDir}/download`;
  22. fs.ensureDirSync(this.config.tempDownloadDir);
  23. this.config.tempPublicDir = `${config.publicDir}/tmp`;
  24. fs.ensureDirSync(this.config.tempPublicDir);
  25. this.workerState = new WorkerState();
  26. this.down = new FileDownloader(config.maxUploadFileSize);
  27. this.decomp = new FileDecompressor(3*config.maxUploadFileSize);
  28. this.bookConverter = new BookConverter(this.config);
  29. this.connManager = new JembaConnManager();
  30. this.appDb = this.connManager.db['app'];
  31. this.remoteStorage = false;
  32. if (config.remoteStorage) {
  33. this.remoteStorage = new RemoteStorage(
  34. Object.assign({maxContentLength: 3*config.maxUploadFileSize}, config.remoteStorage)
  35. );
  36. }
  37. this.remoteConfig = {
  38. '/tmp': {
  39. dir: this.config.tempPublicDir,
  40. maxSize: this.config.maxTempPublicDirSize,
  41. moveToRemote: true,
  42. },
  43. '/upload': {
  44. dir: this.config.uploadDir,
  45. maxSize: this.config.maxUploadPublicDirSize,
  46. moveToRemote: true,
  47. }
  48. };
  49. this.periodicCleanDir(this.remoteConfig);//no await
  50. instance = this;
  51. }
  52. return instance;
  53. }
  54. async loadBook(opts, wState) {
  55. const url = opts.url;
  56. let decompDir = '';
  57. let downloadedFilename = '';
  58. let isUploaded = false;
  59. let convertFilename = '';
  60. const overLoadMes = 'Слишком большая очередь загрузки. Пожалуйста, попробуйте позже.';
  61. const overLoadErr = new Error(overLoadMes);
  62. let q = null;
  63. try {
  64. wState.set({state: 'queue', step: 1, totalSteps: 1});
  65. try {
  66. let qSize = 0;
  67. q = await queue.get((place) => {
  68. wState.set({place, progress: (qSize ? Math.round((qSize - place)/qSize*100) : 0)});
  69. if (!qSize)
  70. qSize = place;
  71. });
  72. } catch (e) {
  73. throw overLoadErr;
  74. }
  75. wState.set({state: 'download', step: 1, totalSteps: 3, url});
  76. const tempFilename = utils.randomHexString(30);
  77. const tempFilename2 = utils.randomHexString(30);
  78. const decompDirname = utils.randomHexString(30);
  79. //download or use uploaded
  80. if (url.indexOf('disk://') != 0) {//download
  81. const downdata = await this.down.load(url, (progress) => {
  82. wState.set({progress});
  83. }, q.abort);
  84. downloadedFilename = `${this.config.tempDownloadDir}/${tempFilename}`;
  85. await fs.writeFile(downloadedFilename, downdata);
  86. } else {//uploaded file
  87. const fileHash = url.substr(7);
  88. downloadedFilename = `${this.config.uploadDir}/${fileHash}`;
  89. if (!await fs.pathExists(downloadedFilename)) {
  90. //если удалено из upload, попробуем восстановить из удаленного хранилища
  91. try {
  92. await this.restoreRemoteFile(fileHash, '/upload');
  93. } catch(e) {
  94. throw new Error('Файл не найден на сервере (возможно был удален как устаревший). Пожалуйста, загрузите файл с диска на сервер заново.');
  95. }
  96. }
  97. await utils.touchFile(downloadedFilename);
  98. isUploaded = true;
  99. }
  100. wState.set({progress: 100});
  101. if (q.abort())
  102. throw overLoadErr;
  103. q.resetTimeout();
  104. //decompress
  105. wState.set({state: 'decompress', step: 2, progress: 0});
  106. decompDir = `${this.config.tempDownloadDir}/${decompDirname}`;
  107. let decompFiles = {};
  108. try {
  109. decompFiles = await this.decomp.decompressNested(downloadedFilename, decompDir);
  110. } catch (e) {
  111. log(LM_ERR, e.stack);
  112. throw new Error('Ошибка распаковки');
  113. }
  114. wState.set({progress: 100});
  115. if (q.abort())
  116. throw overLoadErr;
  117. q.resetTimeout();
  118. //конвертирование в fb2
  119. wState.set({state: 'convert', step: 3, progress: 0});
  120. convertFilename = `${this.config.tempDownloadDir}/${tempFilename2}`;
  121. await this.bookConverter.convertToFb2(decompFiles, convertFilename, opts, progress => {
  122. wState.set({progress});
  123. if (queue.freed > 0)
  124. q.resetTimeout();
  125. }, q.abort);
  126. //сжимаем файл в tmp, если там уже нет с тем же именем-sha256
  127. const compFilename = await this.decomp.gzipFileIfNotExists(convertFilename, this.config.tempPublicDir);
  128. const stat = await fs.stat(compFilename);
  129. wState.set({progress: 100});
  130. //finish
  131. const finishFilename = path.basename(compFilename);
  132. wState.finish({path: `/tmp/${finishFilename}`, size: stat.size});
  133. } catch (e) {
  134. log(LM_ERR, e.stack);
  135. let mes = e.message.split('|FORLOG|');
  136. if (mes[1])
  137. log(LM_ERR, mes[0] + mes[1]);
  138. log(LM_ERR, `downloadedFilename: ${downloadedFilename}`);
  139. mes = mes[0];
  140. if (mes == 'abort')
  141. mes = overLoadMes;
  142. wState.set({state: 'error', error: mes});
  143. } finally {
  144. //clean
  145. if (q)
  146. q.ret();
  147. if (decompDir)
  148. await fs.remove(decompDir);
  149. if (downloadedFilename && !isUploaded)
  150. await fs.remove(downloadedFilename);
  151. if (convertFilename)
  152. await fs.remove(convertFilename);
  153. }
  154. }
  155. loadBookUrl(opts) {
  156. const workerId = this.workerState.generateWorkerId();
  157. const wState = this.workerState.getControl(workerId);
  158. wState.set({state: 'start'});
  159. this.loadBook(opts, wState);
  160. return workerId;
  161. }
  162. async saveFile(file) {
  163. const hash = await utils.getFileHash(file.path, 'sha256', 'hex');
  164. const outFilename = `${this.config.uploadDir}/${hash}`;
  165. if (!await fs.pathExists(outFilename)) {
  166. await fs.move(file.path, outFilename);
  167. } else {
  168. await utils.touchFile(outFilename);
  169. await fs.remove(file.path);
  170. }
  171. return `disk://${hash}`;
  172. }
  173. async saveFileBuf(buf) {
  174. const hash = await utils.getBufHash(buf, 'sha256', 'hex');
  175. const outFilename = `${this.config.uploadDir}/${hash}`;
  176. if (!await fs.pathExists(outFilename)) {
  177. await fs.writeFile(outFilename, buf);
  178. } else {
  179. await utils.touchFile(outFilename);
  180. }
  181. return `disk://${hash}`;
  182. }
  183. async uploadFileTouch(url) {
  184. const outFilename = `${this.config.uploadDir}/${url.replace('disk://', '')}`;
  185. await utils.touchFile(outFilename);
  186. return url;
  187. }
  188. async restoreRemoteFile(filename, remoteDir) {
  189. let targetDir = '';
  190. if (this.remoteConfig[remoteDir])
  191. targetDir = this.remoteConfig[remoteDir].dir;
  192. else
  193. throw new Error(`restoreRemoteFile: unknown remoteDir value (${remoteDir})`);
  194. const basename = path.basename(filename);
  195. const targetName = `${targetDir}/${basename}`;
  196. if (!await fs.pathExists(targetName)) {
  197. let found = false;
  198. if (this.remoteStorage) {
  199. found = await this.remoteStorage.getFileSuccess(targetName, remoteDir);
  200. }
  201. if (!found) {
  202. throw new Error('404 Файл не найден');
  203. }
  204. }
  205. return targetName;
  206. }
  207. async cleanDir(dir, remoteDir, maxSize, moveToRemote) {
  208. const sent = this.remoteSent;
  209. const list = await fs.readdir(dir);
  210. let size = 0;
  211. let files = [];
  212. for (const filename of list) {
  213. const filePath = `${dir}/${filename}`;
  214. const stat = await fs.stat(filePath);
  215. if (!stat.isDirectory()) {
  216. size += stat.size;
  217. files.push({name: filePath, stat});
  218. }
  219. }
  220. log(`clean dir ${dir}, maxSize=${maxSize}, found ${files.length} files, total size=${size}`);
  221. files.sort((a, b) => a.stat.mtimeMs - b.stat.mtimeMs);
  222. //удаленное хранилище
  223. if (moveToRemote && this.remoteStorage) {
  224. const foundFiles = new Set();
  225. for (const file of files) {
  226. foundFiles.add(file.name);
  227. if (sent[file.name])
  228. continue;
  229. //отправляем в remoteStorage
  230. try {
  231. log(`remoteStorage.putFile ${remoteDir}/${path.basename(file.name)}`);
  232. await this.remoteStorage.putFile(file.name, remoteDir);
  233. sent[file.name] = true;
  234. await this.appDb.insert({table: 'remote_sent', ignore: true, rows: [{id: file.name, remoteDir}]});
  235. } catch (e) {
  236. log(LM_ERR, e.stack);
  237. }
  238. }
  239. //почистим remoteSent и БД
  240. //несколько неоптимально, таскает все записи из БД
  241. const rows = await this.appDb.select({table: 'remote_sent'});
  242. for (const row of rows) {
  243. if (row.remoteDir === remoteDir && !foundFiles.has(row.id)) {
  244. delete sent[row.id];
  245. await this.appDb.delete({table: 'remote_sent', where: `@@id(${this.appDb.esc(row.id)})`});
  246. }
  247. }
  248. }
  249. let i = 0;
  250. let j = 0;
  251. while (i < files.length && size > maxSize) {
  252. const file = files[i];
  253. const oldFile = file.name;
  254. //реально удаляем только если сохранили в хранилище или размер dir увеличен в 1.5 раза
  255. if (!(moveToRemote && this.remoteStorage)
  256. || (moveToRemote && this.remoteStorage && sent[oldFile])
  257. || size > maxSize*1.5) {
  258. await fs.remove(oldFile);
  259. j++;
  260. }
  261. size -= file.stat.size;
  262. i++;
  263. }
  264. log(`removed ${j} files`);
  265. }
  266. async periodicCleanDir(cleanConfig) {
  267. try {
  268. if (!this.remoteSent)
  269. this.remoteSent = {};
  270. //инициализация this.remoteSent
  271. if (this.remoteStorage) {
  272. const rows = await this.appDb.select({table: 'remote_sent'});
  273. for (const row of rows) {
  274. this.remoteSent[row.id] = true;
  275. }
  276. }
  277. while (1) {// eslint-disable-line no-constant-condition
  278. for (const [remoteDir, config] of Object.entries(cleanConfig)) {
  279. try {
  280. await this.cleanDir(config.dir, remoteDir, config.maxSize, config.moveToRemote);
  281. } catch(e) {
  282. log(LM_ERR, e.stack);
  283. }
  284. }
  285. await utils.sleep(cleanDirPeriod);
  286. }
  287. } catch (e) {
  288. log(LM_FATAL, e.message);
  289. ayncExit.exit(1);
  290. }
  291. }
  292. }
  293. module.exports = ReaderWorker;